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

如果你是以SQL2000作数据查询那么就是这样。

select * from sysobjects where type='U' and name like '%表名%' 任意位置包含表名的都会查询出来。

如果要加一个文本框,在VB里可以这样写。

sSQL="select * from sysobjects where type='U' and name like '%" &text1.text &"%'"

给个参考,这句的意思是从学生信息表中选择满足学号为'" &Trim(Combo1.Text) &"' 而且 课程为'" &Trim(Combo2.Text) &"'而且 班级 = '" &Trim(Combo3.Text) &"' 的任意*信息给记录集Adodc1.RecordSource。 { 其中Trim()用来去除字符串两边的空格.。再一个就是and条件的格式 : 条件1 and (条件2 and 条件3) }

Adodc1.RecordSource = " select * from 学生信息表 where 学号 = '" &Trim(Combo1.Text) &"' and ( 课程 ='" &Trim(Combo2.Text) &"' and班级 = '" &Trim(Combo3.Text) &"' ) "


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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存