VB SQL 多条件查询

VB SQL 多条件查询,第1张

先选好条件,然后再来按查询按钮实现查询功能,只是一种很普通的查询功能。

其实你可以通过用多个combo控件来实现条件筛选,然后在每一个combo的change事件里写上相同的查询功能的代码(最好是一个自编的函数,这样,你每一次选择条件后,马上就可以看到筛选结果,这样是比较人性化的,而实际止大多的开发人员也是这样做

的。,不明白的话你再问我吧。

什么意思   7各控件存的是字段名称?  然后要求查询出这7各字段均不为空的所有数据?

假设字段名称存在Text1.Text,Text2.Text,Text3.Text,Text4.Text,Text5.Text,Text6.Text,Text7.Text

Dim sqlStr as String

sqlStr = "Select * from myTable where " & Text1.Text & " is not null and " & _

sqlStr = sqlStr & Text2.Text & " is not null and " & _

sqlStr = sqlStr & Text3.Text & " is not null and " & _

sqlStr = sqlStr & Text4.Text & " is not null and " & _

sqlStr = sqlStr & Text5.Text & " is not null and " & _

sqlStr = sqlStr & Text6.Text & " is not null and " & _

sqlStr = sqlStr & Text7.Text & " is not null"

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

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


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

原文地址: https://outofmemory.cn/sjk/9943105.html

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

发表评论

登录后才能评论

评论列表(0条)

保存