看了好几篇关于组合查询的博客。感觉大家写的非常好!也有看不懂的,我敲得组合查询没有使用布尔值。
首先定义数组,用于简化,数据库属性。
进入判断,首先判断第一行条件是否填写完整!然后再判断第一个逻辑符号。如果为空直接直接查询第一行条件,如果没有,判断第二个逻辑符合是否为空,并且查询第二行是否为空。如果第二个逻辑符合为空则,查询两行条件。如果第二个逻辑符合不为空,判断第三行条件是否为空,进行三行条件查询!
'一共分4种情况 '首先将数据库属性赋值简化 Dim i As Integer '做为循环赋值combol Dim j As Integer '做为循环赋值combo3 '赋值数组 学生基本信息维护
'一共分4种情况 '首先将数据库属性赋值简化 Dim i As Integer '做为循环赋值combol Dim j As Integer '做为循环赋值combo3 '赋值数组 Dim str1(3) '对应combo1 Dim str3(2) '对于combo3 For i = 0 To 2 Select Case Combo1(i).Text Case "卡号" str1(i) = "cardno" Case "学号" str1(i) = "studentNo" Case "姓名" str1(i) = "studentName" Case "性别" str1(i) = "sex" Case "系别" str1(i) = "department" Case "年级" str1(i) = "grade" Case "班级" str1(i) = "class" End Select Next i '对于combo3 For j = 0 To 1 Select Case Combo3(j).Text Case "与" str3(j) = "and " Case "或" str3(j) = "or" End Select Next j Dim txtSQL As String Dim Msgtext As String txtSQL = "select * from student_Info where " '第一步先判断第一行是否为空 If Combo1(0).Text = "" Or Combo2(0).Text = "" Or Text1.Text = "" Then MsgBox "请将第一行内容填写完整!", vbOKOnly + vbExclamation, "警告" Exit Sub End If '全部判断,判断是否选择第一个combo3 If Combo3(0).Text <> "" Then '如果判断combo3(0)不为空,在判断 If Combo1(1).Text = "" Or Combo2(1).Text = "" Or Text2.Text = "" Then MsgBox "请将第二行填写完整!", vbOKOnly + vbExclamation, "警告!" Exit Sub Else '在判断第二个逻辑关系 If Combo3(1).Text <> "" Then '进行下一轮判断 If Combo1(2).Text = "" Or Combo2(2).Text = "" Or Text3.Text = "" Then MsgBox "请将第三行条件填写完整", vbOKOnly + vbExclamation, "警告!" Exit Sub Else '查询三行条件 txtSQL = txtSQL & str1(0) & Combo2(0).Text & "'" & Trim(Text1.Text) & " ' " txtSQL = txtSQL & str3(0) & " " & str1(1) & Trim(Combo2(1).Text) & "'" & Trim(Text2.Text) & " ' " txtSQL = txtSQL & str3(1) & " " & str1(2) & Trim(Combo2(2).Text) & "'" & Trim(Text3.Text) & " ' " Set mrc = ExecuteSQL(txtSQL, Msgtext) Call vieWdate '定义函数用于表格 End If Else txtSQL = txtSQL & str1(0) & Combo2(0).Text & "'" & Trim(Text1.Text) & " ' " txtSQL = txtSQL & str3(0) & " " & str1(1) & Trim(Combo2(1).Text) & "'" & Trim(Text2.Text) & " ' " Set mrc = ExecuteSQL(txtSQL, Msgtext) Call vieWdate End If End If Else txtSQL = txtSQL & str1(0) & Combo2(0).Text & "'" & Trim(Text1.Text) & " ' " Set mrc = ExecuteSQL(txtSQL, Msgtext) Call vieWdate End If '运行MSHFlexGrid1表