摘要:查询模块是数据库管理系统中不可缺少的部分。本文介绍在vb5.0环境下四种数据库查询的实现方法,并主要介绍了使用select-sql语句来实现数据库的查询功能
关键字:vb5.0 数据库 表 查询 sql
vb全称visual basic,是微软公司推出的基于windows的可视化编程环境,以其简单易学、编程简洁、程序集成化高、功能强大而倍受程序员及广大电脑爱好者的青睐。它在数据库应用方面也有相当强大的功能。
查询模块是数据库管理系统中不可缺少的部分。在vb中进行数据库记录查询操作,根据打开数据库的方式来确定。大概有四种查询方法:seek方法查询、filter 属性查询、find 方法查询、sql查询。本文对前三种方法只作简单说明,着重介绍第四种sql查询方法。
1、seek方法查询、filter 属性查询、find 方法查询的简单说明。
用seek方法查询:
这种方法只使用于以opentable 方式打开的数据表,而且在查询之前必须要对查询字段建立索引文件,由于已建立了索引文件,所以查询速度快,这种方式结果是将指针移到符合条件的第一个记录。例如:
set tb=db.opentable(“input”)
tb.index=”nameindex”
tb.seek ”=”, “石脑油”
用filter 属性查询:
filter属性查询是用来过滤数据的,只要我们给定过滤条件就可以将所需的记录筛选出来。需要说明的是,我们需要将以filter属性筛选出来的数据集打开才能对其进行操作。例如:
set dy1=db.createdynaset (“input”)
dy1.filter=”物资名称 like ‘石*’”
set dy2.dy1.createdynaset ()
用find 方法查询。
find查询有findfirst findnext 两个方法,每次查询到一个记录。例如:
set dy=db.createdynaset (“input”)
s=”到货数量>100 and 物资名称like ‘石脑油’”
dy.findfirst s
dy.findnext s
2、用sql查询。
2.1、select-sql查询语句的格式:
sql(structure query language)即结构化查询语言,是查询关系型数据库的常用语言。由于sql语言使用方便、功能丰富、简单易学得到很快的应用和推广,是各种关系型数据库的公用语言。使用sql查询可以从一个表或多个表或视图中对数据库进行查询(有关sql的更多信息,请参阅相关书籍,本文不多介绍)。它的核心语句是select- sql语句。
select-sql查询语句的格式:
select [dictinct/all]----查询目标列
from tableexpression ----------表名/视图名
[where]---------------------------条件
[group by... ]--------------------将查询结果的记录分组
[having... ]------------------------满足条件的分组
[order by... ]---------------------对查询结果进行排序
下面应用笔者完成的《供应处物资管理软件》为例,。说明使用select-sql查询语句实现查询模块具体方法。
2.2单项查询模块的实现:
2.2.1应用的数据库in_db.mdb中包含表:input 字段名:物资名称、供货单位、供货日期、到货数量、总金额…….等等 。
2.2.2定义窗体及控件:如下表
主要控件及名称
属性
设置
说明
frame(frame1)
caption
字段选择
optionbutton(option1)
caption
物资名称
字段选择
optionbutton(option2)
caption
供货单位
optionbutton(option3)
caption
供货日期
optionbutton(option4)
caption
到货数量
optionbutton(option5)
caption
总金额
textbox(text1)
text
为空
查询值
textbox(text2)
text
为空
commandbutton(command1)
caption
确定
确定本次查询
commandbutton(command2)
caption
取消
取消本次查询
commandbutton(command3)
caption
结束
结束查询
data(data1)
caption
数据浏览
databasename
d:\zu_vb\in_db.mdb
dbgrid(dbgrid1)
datasource
data1
查询结果显示
如图(1):单项查询模块窗体图
2.2.3编写程序代码:
上述设计完成后,可以对窗体及控件的事件编写代码:
‘变量定义
dim my_db as database dim my_dr as recordset dim field_val1 as string dim field_val3 as date
dim field_val32 as date
dim field_val4 as integer
dim field_val42 as integer
dim search_txt as integer
private sub form_load() search_txt = 1 text1.text = "" text2.text = ""label2.caption = ""end sub
private sub command1_click() ‘确定按钮
select case search_txt
case 1 ‘若选择“物资名称”
field_val1 = text1.text
set my_db = opendatabase("d:\zu_vb\in_db.mdb")
set my_dr = my_db.openrecordset("input")
ss1 = "select * from input where (物资名称=" & "'" & field_val1 & "')"
data1.recordsource = ss1
data1.refresh
case 2 ‘若选择“供货单位”
. . .
case 3 ‘若选择“供货日期”
field_val3 = text1.text
field_val32 = text2.text
if val(datediff("d", (text1.text), (text2.text))) >= 0 then
set my_db = opendatabase("d:\zu_vb\in_db.mdb")
set my_dr = my_db.openrecordset("input")
ss1 = "select * from input where 供货日期 between " & "#" _
& field_val3 & "#" _ & " and " & "#" & field_val32 & "#" data1.recordsource = ss1 data1.refresh else zz = msgbox("您输入的起始日期比终止日期大,请重新输入!", vbcritical, "严重警告,输入无效!")
end if
case 4 ‘若选择“到货数量”
field_val4 = text1.text
field_val42 = text2.text
set my_db = opendatabase("d:\zu_vb\in_db.mdb")
set my_dr = my_db.openrecordset("input")
ss1 = "select * from input where 到货数量 between " & field_val4 _
& " and " & field_val42
data1.recordsource = ss1
data1.refresh
case 5
. . .
end select
end sub
private sub command2_click() ‘取消查询
text1.text = ""
text2.text = ""
end sub
private sub command3_click() ‘ 结束查询
unload me
end sub
private sub option1_click() ‘选定“物资名称”字段
search_txt = 1
text1.text = ""
label2.caption = ""
text2.enabled = false ‘text2 设为无效
text2.visible = false ‘text2 设为不显示
text1.setfocus
end sub
private sub option2_click() ‘选定“供货单位”字段
(略)
end sub
private sub option3_click() ‘选定“供货日期”字段search_txt = 3text1.text = ""text1.text = date ‘起始日期text2.text = date ‘终止日期
label2.caption = "至"
text2.enabled = true
text2.visible = true
text1.setfocus
end sub
private sub option4_click() ‘选定“到货数量”字段
search_txt = 4
text1.text = ""
text2.text = ""
text1.text = format(text1.text, "###,###,##0.00")‘设置字段格式为数值型
text2.text = format(text2.text, "###,###,##0.00")‘设置字段格式为数值型
label2.caption = "至"
text2.enabled = true
text2.visible = true
text1.setfocus
end sub
private sub option5_click() ‘选定“总金额”字段
(略)
end sub
private sub text1_lostfocus()
‘判断如果选定的是“供货日期”字段,text1的输入值必须是日期型的 if search_txt = 3 then
if not isdate(text1.text) then
z = msgbox(" 非法日期!请重新输入 !", vbcritical, "严重警告,输入无效 !")
text1.setfocus
end if
end if
end sub
private sub text2_lostfocus()
‘判断如果选定的是“供货日期”字段,text2的输入值必须是日期型的
if search_txt = 3 then
text2.text = format(text2.text, "yyyy-mm-dd")
if not isdate(text2.text) then
z = msgbox(" 非法日期!请重新输入 !", vbcritical, "严重警告,输入无效 !")
text2.setfocus
end if
end if
end sub
2.3多项复合查查询模块的实现:
2.3.1应用的数据库in_db.mdb中包含表:input 字段名:物资名称、供货单位、供货日期、到货数量、总金额…….等等 。
2.3.2定义窗体及控件:如下表
主要控件及名称
属性
设置
说明
frame(frame1)
caption
查询条件选择
combobox
caption
为空
物资名称选择
textbox(text1)
text
为空
起始日期
textbox(text2)
text
为空
终止日期
commandbutton(command1)
caption
确定
确定本次查询
commandbutton(command2)
caption
结束
结束查询
data(data1)
caption
数据浏览
databasename
d:\zu_vb\in_db.mdb
dbbrid(dbgrid1)
datasource
data1
查询结果显示
如图(2)多项查询模块窗体图
2.3.3编写程序代码:
上述设计完成后,可以对窗体及控件的事件编写代码:
下面程序可实现“物资名称”为某值,供货日期在一定范围的采购明细的查询。
‘定义变量
dim my_db as database dim my_dr as recordset dim com_txt as string dim txt1 as date dim txt2 as date private sub command1_click()
‘判断如果输入值不是日期型的,是无效值 if not isdate(text1.text) then z = msgbox("非法起始日期,请重新输入!", vbcritical, "严重警告,输入无效!")
text1.setfocus
else
if not isdate(text2.text) then
z = msgbox("非法终止日期,请重新输入!", vbcritical, "严重警告,输入无效!")
text2.setfocus
else
if val(datediff("d", (text1.text), (text2.text))) >= 0 then
'''*****设置条件*********
com_txt = form5.combo1.text
txt1 = form5.text1.text
txt2 = form5.text2.text
set my_db = opendatabase("d:\zu_vb\in_db.mdb")
set my_dr = my_db.openrecordset("input")
ww1 = "select * from input where (物资名称=" & "'" & com_txt & "'" _
… & " and (供货日期 between " & "#" & txt1 & "#" _
& " and " & "#" & txt2 & "#))"
data1.recordsource = ww1
data1.refresh
else
zz = msgbox("您输入的起始日期比终止日期大,请重新输入!", vbcritical, "严重警告,输入无效!")
end if
end if
end if
end sub
private sub form_load()
‘将项目“石脑油”、轻烃、纯苯………添加到combo1控件中
combo1.additem "石脑油"
combo1.additem "轻烃"
combo1.additem "纯苯"
combo1.additem "丙烯腈"
combo1.additem "甲基丙烯酸甲脂"
combo1.additem "聚丁二烯乳胶"
combo1.additem "c2"
combo1.additem "c3/c4"
combo1.additem "c5"
combo1.additem "盐酸"
combo1.additem "液碱"
combo1.text = "石脑油" ‘设置 combo1的初始值
text1.text = date ‘设置text1 text2 为日期形式,执行时并显示当前日期。
text2.text = date
end sub
3、结束语:
本文对vb数据库查询功能做了简单的说明,特别对select-sql语句进行了详细的介绍,为了节省篇幅,以上只给出了窗体控件的属性和代码的核心部分,并去掉了许多修饰性的内容,感兴趣的读者在此基础上稍加扩充或修改,便可得到更完善的通用查询模块。(以上程序均在windows98环境下vb5.0中运行通过。)