VB 多条件查询

VB 多条件查询,第1张

举例说明,实现如下多条件查询:

Private Sub Command1_Click() Dim jsql jsql = ""

Dim smyregion As String If Check1.Value = 1 Then

 jsql = "图书名称 like '%" + Text1.Text + "%'" End If

If Check2.Value = 1 Then    If jsql = "" Then

    jsql = "作者姓名 like'%" + Text2.Text + "%'"    Else

    jsql = jsql &"and 作者姓名 like'%" + Text2.Text + "%'"     End If End If

If Check3.Value = 1 Then    If jsql = "" Then

    jsql = "出版社名称 like'%" + Text3.Text + "%'"     Else

    jsql = jsql &"and 出版社名称 like '%" + Text3.Text + "%'"     End If End If

If Check4.Value = 1 Then   If jsql = "" Then

   jsql = "出版时间 like '%" + Text4.Text + "%'"     Else

     jsql = jsql &"and 出版时间 like '%" + Text3.Text + "%'"     End If End If

If Check5.Value = 1 Then    If jsql = "" Then

    jsql = "图书类别 like '%" + Text5.Text + "%'"

Else

     jsql = jsql &"and 图书类别 like '%" + Text5.Text + "%'"     End If End If

If jsql = "" Then

MsgBox "请选择查询条件!", vbInformation, "图书音像管理系统"   Exit Sub Else

Adodc1.RecordSource = "select * from book where " &jsql   Adodc1.Refresh End If

If Adodc1.Recordset.RecordCount >0 Then   Set DataGrid1.DataSource = Adodc1 End If    End Sub

Private Sub Command2_Click() Unload Me End Sub

比方说用check1、check2、check3分别选择类型、名称、数量作为查询条件:

Dim sqls As String

sqls = "select * from [table] where true "

If Check1.Value = 1 Then sqls = sqls &"and 类型='" &Text1.Text &"' "

If Check2.Value = 1 Then sqls = sqls &"and 名称='" &Text2.Text &"' "

If Check3.Value = 1 Then sqls = sqls &"and 数量=" &Text3.Text

rs.Open sqls, conn, 1, 1

……

举例说明,实现如下多条件查询:

Private Sub Command1_Click() Dim jsql jsql = ""

Dim smyregion As String If Check1.Value = 1 Then

jsql = "图书名称 like '%" + Text1.Text + "%'" End If

If Check2.Value = 1 ThenIf jsql = "" Then

jsql = "作者姓名 like'%" + Text2.Text + "%'"Else

jsql = jsql &"and 作者姓名 like'%" + Text2.Text + "%'" End If End If

If Check3.Value = 1 ThenIf jsql = "" Then

jsql = "出版社名称 like'%" + Text3.Text + "%'" Else

jsql = jsql &"and 出版社名称 like '%" + Text3.Text + "%'" End If End If

If Check4.Value = 1 Then If jsql = "" Then

jsql = "出版时间 like '%" + Text4.Text + "%'" Else

jsql = jsql &"and 出版时间 like '%" + Text3.Text + "%'" End If End If

If Check5.Value = 1 ThenIf jsql = "" Then

jsql = "图书类别 like '%" + Text5.Text + "%'"

Else

jsql = jsql &"and 图书类别 like '%" + Text5.Text + "%'" End If End If

If jsql = "" Then

MsgBox "请选择查询条件!", vbInformation, "图书音像管理系统" Exit Sub Else

Adodc1.RecordSource = "select * from book where " &jsql Adodc1.Refresh End If

If Adodc1.Recordset.RecordCount >0 Then Set DataGrid1.DataSource = Adodc1 End IfEnd Sub

Private Sub Command2_Click() Unload Me End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9943039.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-03
下一篇2023-05-03

发表评论

登录后才能评论

评论列表(0条)

    保存