前言 组合查询是机房的三大难点之一。不过只要弄懂敲完一个组合查询的窗体,另外两个也就不成问题了,说到底逻辑最重要。
正文 先来看以下几个控件之间的关系,如下图。选择了第一个组合条件,就可以查询前两行所要求的内容,选择第二个条件同理。
我是这么设置的:每一行为一组,选择了第一个组合关系,第二行的combo和text才可用,同理,选择了第二个组合关系,第三行才可用。
首先判断有没有输入查询数据,一行一行判断。
Private Sub cmdInquiry_Click()
Dim mrc As ADODB.Recordset
Dim txtSQL As String
Dim MsgText As String
'判断第一行查询条件是否输入数据
If comboField1(0) = "" Or comboOpSign1(0) = "" Or txtInquiry1(0).Text = "" Then
MsgBox "请在第一行输入完整的查询条件!", 0 + 48
Exit Sub
Else
txtSQL = "select * from student_info where " & Field(Trim(comboField1(0).Text)) & Trim(comboOpSign1(0).Text) & "'" & Trim(txtInquiry1(0).Text) & "'"
End If
'判断第二行查询条件是否输入数据
If comboCombine1(0).Text <> "" Then 'And comboField1(0) <> "" And comboOpSign1(0) <> "" And txtInquiry1(0).Text <> "" Then
If comboField1(1).Text = "" Or comboOpSign1(1).Text = "" Or txtInquiry1(1).Text = "" Then
MsgBox "请在第二行输入完整的查询条件", 0 + 48
Exit Sub
Else
txtSQL = txtSQL & RelationName(comboCombine1(0).Text) & " " & Field(Trim(comboField1(1).Text)) & Trim(comboOpSign1(1).Text) & "'" & Trim(txtInquiry1(1).Text) & "'"
End If
End If
'判断第三行查询条件是否输入数据
If comboCombine1(1).Text <> "" Then
If comboField1(2).Text = "" Or comboOpSign1(2).Text = " " Or txtInquiry1(2).Text = "" Then
MsgBox "请在第三行输入完整的查询条件!", 0 + 48
Exit Sub
Else
txtSQL = txtSQL & RelationName(comboCombine1(1).Text) & " " & Field(Trim(comboField1(2).Text)) & comboOpSign1(2).Text & "'" & txtInquiry1(2) & "'"
End If
End If
Set mrc = ExecuteSQL(txtSQL, MsgText)