VB中复合查询的实现

时间:2022-09-12 06:15:21

摘要:VB中的复选框可以实现多项选择的功能。实际工作中,我们经常需要用到复选框来实现复杂的查询功能。该文以实例探讨了VB中利用复选框实现复合查询的方法。

关键词:VB;复选框;复合查询

中图分类号:TP311文献标识码:A文章编号:1009-3044(2012)02-0361-03

Approach for Complex Query on VB

XU Bin, LI Cheng

(Department of Information Engineering,Nanjing Yingtian College,Nanjing 210046, China)

Abstract: Check box can accomplish the function of multiple choice in VB.In practical work,we frequently use check box to complete the function of complex query. This paper gives example to discuss the methods of use check box to achieve complex query.

Key words: VB; check box; complex query

在VB中,复选框控件能够实现多重选择的功能。利用该控件,结合数据库,可以实现对数据表的复合查询。本文通过具体实例讨论了VB中如何利用复选框控件来实现对数据库中数据的复合查询。

1开发工具

软件开发工具为VB,数据库为Access。

VB是微软公司开发的编程语言。它采用了面向对象的程序设计思想、事件驱动的编程机制。它具备强大的数据库访问功能,提供了对多种数据库的访问方法,可以方便地访问Oracle、SQL Server、Access等数据库。

Access是一个功能强大、方便灵活、适用于单机环境下使用的关系型数据库管理系统,它同时提供了一个完整的数据库应用开发工具,可用于开发适合于特定的数据库管理的Windows应用程序。

2实现方法

在VB中,单击“工程”菜单下的“部件”,勾选“Microsoft Hierarchical FlexGrid Control 6.0(OLEDB)”,即在工具箱中添加MSH? FlexGrid控件,用以显示查询结果;单击“工程”菜单下的“引用”,勾选“Microsoft ActiveX Data Objects 2.8 Library”,即引用ADO对象。

Access数据库名为“加分管理”,在“加分管理”数据库中有一个名为“总表”的数据表,该数据表结构如表1所示。

表1“总表”结构

查询界面中的复选框主要设计了“按性别”、“按专业”、“按科类”、“按总分”进行复合查询,查询设计界面如图1所示。“查询”按钮代码如下:

Private Sub Command1_Click()

Dim s, s1, s2 As String

Dim str_text As String

Dim count,num As Integer

Dim i, j, k As Integer

Dim Mydb As New ADODB.Recordset

’找出第一个被选中的复选框对应的i值

For i = 0 To 3

If Check1(i).Value = 1 Then

Exit For

End If

Next i

Select Case i

Case 0

s = "xb=’" & Combo1.Text & "’"

Case 1

s = "zydh1 like ’" & Text6.Text & "%’"

Case 2

s = "category like ’" & Combo2.Text & "%’"

Case 3

s = "total between " & Val(Text7.Text) & " and " & Val(Text8.Text)

End Select

’统计被选中的复选框数

For j = 0 To 3

If Check1(j).Value = 1 Then

count = count + 1

End If

Next j

If count = 1 Then

s = "select ksh as考生号,xm as姓名,xb as性别,total as总分,zydh1 as报考第一专业,category as类别from zong where " & s Set Mydb = ExeCutesql(s, str_text)

Set MSHFlexGrid1.DataSource = Mydb

Label4.Caption = str_text

ElseIf count > 1 Then

num = 1

For k = i + 1 To 3

If Check1(k).Value = 1 Then

Select Case k

Case 0

s1 = "xb=’" & Combo1.Text & "’"

s2 = s2 & " and " & s1

Case 1

s1 = "zydh1 like ’" & Text6.Text & "%’"

s2 = s2 & " and " & s1

Case 2

s1 = "category like ’" & Combo2.Text & "%’"

s2 = s2 & " and " & s1

Case 3

s1 = "total between " & Val(Text7.Text) & " and " & Val(Text8.Text)

s2 = s2 & " and " & s1

End Select

num = num + 1

If num = count Then Exit For

End If

Next k

s2 = s & s2

s = "select ksh as考生号,xm as姓名,xb as性别,total as总分,zydh1 as报考第一专业,category as类别from zong where " & s2 Set Mydb = ExeCutesql(s, str_text)

Set MSHFlexGrid1.DataSource = Mydb

Label4.Caption = str_text

End If

End Sub

此外还设计了两个函数,分别用于数据库的连接和执行相应查询,函数如下:

’连接数据库

Private Function Connectstring() As String

Str_path = CurDir() & "\" & "加分管理.MDB"

Connectstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=’" & Str_path & "’;Persist Security Info=False" End Function

’执行查询

Private Function ExeCutesql(ByVal Sql As String, Msgstring As String) As ADODB.Recordset

Dim cnn As New ADODB.Connection

Dim Rst As ADODB.Recordset

Dim Stokens() As String ’数组

On Error GoTo executesql_error

Stokens = Split(Sql) ’将sql语句按关键字保存在数组中

Set cnn = New ADODB.Connection

cnn.Open Connectstring

If InStr("INSERT,DELETE,UPDATE", UCase$(Stokens(0))) 0 Then

cnn.Execute Sql

Msgstring = Stokens(0) & "操作成功"

Else

Set Rst = New ADODB.Recordset

Rst.Open Trim$(Sql), cnn, adOpenKeyset, adLockOptimistic

Set ExeCutesql = Rst

Msgstring = "查询到" & Rst.RecordCount & "条记录"

End If

executesql_exit:

Set Rst = Nothing ’释放记录集

Set cnn = Nothing ’释放连接语句

Exit Function

executesql_error:

Msgstring = "查询错误:" & Err.Description

Resume executesql_exit

End Function

查询结果界面如图2所示。

图1查询设计界面

图2查询结果界面

3结束语

以上实例对如何利用复选框控件实现对数据表的复合查询等相关问题做了有益的探讨,可以应用于具体的数据库系统的开发,具有较强的实用性。

参考文献:

[1]石付,恒王斌.基于VB6的组合查询系统的开发[J].电脑编程技巧与维护,2004(1).

[2]沈祥玖.VB程序设计[M]. 2版.北京:高等教育出版社,2009.

[3]巫张英.Access数据库基础与应用教程[M].北京:人民邮电出版社,2009.

上一篇:信息时代的网络教学 下一篇:ASP.NET代码复用技术的典型应用