【前言】
一拖再拖,组合查询终于做完了,并且弄明白了其中的含义。下面就以操作员的工作记录窗体为例,来总结一下组合查询吧!
【内容】
组合查询:
第一行(可单独查询)即为第一个条件,第二行为第二个条件,第三行为第三个条件。利用“组合关系”控件将条件连接起来,进行组合查询。
导图
这张导图是我看了好多大佬的博客,和自己的思考之后画出来的,可能某个地方还不是理解的很透彻,望指点!
查询代码
Private Sub cmdInquiry_Click()
Dim txtSQL1 As String
Dim txtSQL2 As String
Dim txtSQL3 As String
Dim MsgText As String
Dim mrc As ADODB.Recordset
Dim a, b, c As String
'给变量a赋值(当字段1选择日期、时间或非日期、非时间)
If comboField1.Text = "日期" Or comboField1.Text = "时间" Then
a = DTPicker1.Value
Else
a = txtInquiry1.Text
End If
'给变量b赋值(当字段2选择日期、时间或非日期、非时间)
If comboField2.Text = "日期" Or comboField2.Text = "时间" Then
b = DTPicker2.Value
Else
b = txtInquiry2.Text
End If
'给变量c赋值(当字段3选择日期、时间或非日期、非时间)
If comboField3.Text = "日期" Or comboField3.Text = "时间" Then
c = DTPicker3.Value
Else
c = txtInquiry3.Text
End If
'第一行内容是否输入完整
If comboField1.Text = "" Then '判断字段名是否为空
MsgBox "请选择字段名!", 48, "警告"
comboField1.SetFocus
Exit Sub
End If
If comboOptSign1.Text = "" Then '判断操作符是否为空
MsgBox "请选择操作符!", 48, "警告"
comboOptSign1.SetFocus
Exit Sub
End If
If txtInquiry1.Text = "" Then '判断要查询的内容是否为空
MsgBox "请输入要查询的内容!", 48, "警告"
txtInquiry1.SetFocus
Exit Sub
End If
'第一个组合关系存在
txtSQL1 = "select * from worklog_Info where " & field(comboField1.Text) _
& " " & comboOptSign1.Text & " '" & Trim(a) & " '"
Set mrc = ExecuteSQL(txtSQL1, MsgText)
If Trim(comboGroup1.Text) <> "" Then
If Trim(comboField2.Text) = "" Or Trim(comboOptSign2.Text) = "" Or Trim(b = "") Then
MsgBox "请将第二行内容补充完整!", 48, "警告"
Exit Sub
Else
txtSQL2 = txtSQL1 & " " & fieldname(comboGroup1.Text) & " " & field(comboField2.Text) _
& comboOptSign2.Text & "'" & Trim(b) & "'"
Set mrc = ExecuteSQL(txtSQL2, MsgText)
End If
End If
'第二个组合关系存在
If Trim(comboGroup2.Text) <> "" Then
If Trim(comboField3.Text) = "" Or Trim(comboOptSign3.Text) = "" Or Trim(c = "") Then
MsgBox "请将第三行内容补充完整!", 48, "警告"
Exit Sub
Else
txtSQL3 = txtSQL2 & " " & fieldname(comboGroup2.Text) & " " & _
field(comboField3.Text) & comboOptSign3.Text & "'" & Trim(c) & "'"
Set mrc = ExecuteSQL(txtSQL3, MsgText)
End If
End If
'判断要查找的内容是否存在
If mrc.EOF Then
MsgBox "没有您要查找的内容,请重新查询!", vbOKOnly + vbExclamation, "提示"
MSFlexGrid1.Clear
Exit Sub
Else
With MSFlexGrid1
.Rows = 1
.CellAlignment = 4
.TextMatrix(0, 0) = "序列号"
.TextMatrix(0, 1) = "教师"
.TextMatrix(0, 2) = "级别"
.TextMatrix(0, 3) = "注册日期"
.TextMatrix(0, 4) = "注册时间"
.TextMatrix(0, 5) = "注销日期"
.TextMatrix(0, 6) = "注销时间"
.TextMatrix(0, 7) = "机器名"
.TextMatrix(0, 8) = "状态"
End With
Do While Not mrc.EOF
With MSFlexGrid1
.Rows = .Rows + 1
.CellAlignment = 4
.TextMatrix(.Rows - 1, 0) = Trim(mrc.Fields(0))
.TextMatrix(.Rows - 1, 1) = Trim(mrc.Fields(1))
.TextMatrix(.Rows - 1, 2) = Trim(mrc.Fields(2))
.TextMatrix(.Rows - 1, 3) = Trim(mrc.Fields(3))
.TextMatrix(.Rows - 1, 4) = Trim(mrc.Fields(4))
.TextMatrix(.Rows - 1, 5) = Trim(mrc.Fields(5)) & ""
.TextMatrix(.Rows - 1, 6) = Trim(mrc.Fields(6)) & ""
.TextMatrix(.Rows - 1, 7) = Trim(mrc.Fields(7)) & ""
.TextMatrix(.Rows - 1, 8) = Trim(mrc.Fields(8)) & ""
End With
mrc.MoveNext
Loop
End If
mrc.Close
End Sub
在做的过程中必有的思考:在选择字段时,当选择了日期或者时间的时候,是不是用DTPicker1更好一点呢?想到这里了,就要动手去实践一下,这部分我也是试过多次,看了许多博客,才弄明白该如何去写代码。
思考:当字段选择了时间或日期,“要查询的内容”控件变为DTPicker1,否则还是用原来的文本框。
代码实现:变量
1.使用变量来代替文本框或DTPicker1;
Dim a, b, c As String
'给变量a赋值(当字段1选择日期、时间或非日期、非时间)
If comboField1.Text = "日期" Or comboField1.Text = "时间" Then
a = DTPicker1.Value
Else
a = txtInquiry1.Text
End If
'给变量b赋值(当字段2选择日期、时间或非日期、非时间)
If comboField2.Text = "日期" Or comboField2.Text = "时间" Then
b = DTPicker2.Value
Else
b = txtInquiry2.Text
End If
'给变量c赋值(当字段3选择日期、时间或非日期、非时间)
If comboField3.Text = "日期" Or comboField3.Text = "时间" Then
c = DTPicker3.Value
Else
c = txtInquiry3.Text
End If
2.查询语句中将“要查询的内容”字段用变量来代替;
txtSQL1 = "select * from worklog_Info where " & field(comboField1.Text) _
& " " & comboOptSign1.Text & " '" & Trim(a) & " '"
Set mrc = ExecuteSQL(txtSQL1, MsgText)
txtSQL2 = txtSQL1 & " " & fieldname(comboGroup1.Text) & " " & field(comboField2.Text) _
& comboOptSign2.Text & "'" & Trim(b) & "'"
Set mrc = ExecuteSQL(txtSQL2, MsgText)
txtSQL3 = txtSQL2 & " " & fieldname(comboGroup2.Text) & " " & _
field(comboField3.Text) & comboOptSign3.Text & "'" & Trim(c) & "'"
Set mrc = ExecuteSQL(txtSQL3, MsgText)
3.当选择了时间或日期,将DTPicker1字段显示,文本框隐藏。
If comboField1.Text = "注册日期" Then
DTPicker1.Format = dtpShortDate
txtInquiry1.Visible = False
txtInquiry1.Enabled = False
DTPicker1.Visible = True
DTPicker1.Enabled = True
ElseIf comboField1.Text = "注册时间" Then
DTPicker1.Format = dtpShortTime
txtInquiry1.Visible = False
txtInquiry1.Enabled = False
DTPicker1.Visible = True
DTPicker1.Enabled = True
ElseIf comboField1.Text = "注销日期" Then
DTPicker1.Format = dtpShortDate
txtInquiry1.Visible = False
txtInquiry1.Enabled = False
DTPicker1.Visible = True
DTPicker1.Enabled = True
ElseIf comboField1.Text = "注销时间" Then
DTPicker1.Format = dtpShortTime
txtInquiry1.Visible = False
txtInquiry1.Enabled = False
DTPicker1.Visible = True
DTPicker1.Enabled = True
ElseIf comboField1.Text <> "注册时间" And comboField1.Text <> "注册日期" _
And comboField1.Text <> "注销日期" _
And comboField1.Text <> "注销时间" Then
DTPicker1.Visible = False
DTPicker1.Enabled = False
txtInquiry1.Visible = True
txtInquiry1.Enabled = True
End If