该来的总会来的!!!!组合查询就这么悄无声息的到了,
组合查询 最重要的就是运用数据的概念:
Dim i As integer
For i = 0 To 2
With Combo1(i)
.AddItem "卡号"
.AddItem "学号"
.AddItem "姓名"
.AddItem "性别"
.AddItem "日期"
.AddItem "时间"
.AddItem "机器名"
End With
Next i
还要注意一些细节问题:
Dim i As Integer
i = Index
If Combo1(i).Text = "姓名" Or Combo1(i).Text = "性别" Then '防止出现姓名 备注等大于小于的情况
Combo2(i).Clear
Combo2(i).AddItem "="
Combo2(i).AddItem "<>"
Else
Combo2(i).Clear '每次切换完都要清空一下 防止重复叠加选项的情况出现
Combo2(i).AddItem "="
Combo2(i).AddItem "<"
Combo2(i).AddItem ">"
Combo2(i).AddItem "<>"
End If
不要忘记日期控件:
For i = 0 To 2
If Combo1(i).Text = "日期" Then
DTPicker1(i).Format = dtpShortDate
DTPicker1(i).Visible = True
Text1(i).Visible = False
Else
If Combo1(i).Text = "时间" Then
DTPicker1(i).Format = dtpTime
DTPicker1(i).Visible = True
Text1(i).Visible = False
Else
DTPicker1(i).Visible = False
Text1(i).Visible = True
If Combo1(i).Text = "卡号" Or Combo1(i).Text = "学号" Or Combo1(i).Text = "姓名" Or Combo1(i).Text = "机器名" Or Combo1(i).Text = "性别" Then
DTPicker1(i).Visible = False
Text1(i).Visible = True
End If
End If
End If
Next i
'这里日期是有多种格式的 dtpicker的fromat属性提供了好几种日期格式
'format=0-dtpLongDate:格式是2018年12月13日
'format=1-dtpShortDate:格式是2018/12/13
'format=2-dtpTime:格式是0:00:00,这个时候是时间,不是日期
'Format=3-dtpCustom,这个时候可以自定义格式,比如
dtpicker1.CustomFormat=Format("yy MM dd")
窗体准备完毕剩下的就是查询了
Dim txtSQLlin As String
Dim MsgText As String
Dim Mrclin As ADODB.Recordset
Dim i As Integer
For i = 0 To 2
If DTPicker1(i).Visible = True Then '查询的时候将dipicker中的日期赋值给文本框 将条件字段名转换成相对应的数据库字段名字符
Text1(i).Text = DTPicker1(i).Value
End If
Next i
If Not Testtxt(Combo1(0).Text) Or Not Testtxt(Combo4(0).Text) Or Not Testtxt(Text1(0).Text) Then
MsgBox "请输入第一行查询条件的内容", 48, "提示"
Combo1(0).SetFocus
Exit Sub
End If
txtSQLlin = "select * from line_info where "
txtSQLlin = txtSQLlin & Trim(Fieldname(Combo1(0).Text)) & Trim(Combo4(0).Text) & "'" & Trim(Text1(0).Text) & "'"
If Not Combo7(0).Text = "" Then '如果选择框不是空值
'判断第二行是否输入
If Not Testtxt(Combo1(1).Text) Or Not Testtxt(Combo4(1).Text) Or Not Testtxt(Text1(1).Text) Then
MsgBox "请输入第二行条件内容", 48, "提示"
Combo1(1).SetFocus
Exit Sub
End If
txtSQLlin = txtSQLlin & " " & RelationName(Trim(Combo7(0).Text)) & " " & Fieldname(Combo1(1).Text) & " " & Trim(Combo4(1).Text) & "'" & Trim(Text1(1).Text) & " '"
End If
If Not Combo7(1).Text = "" Then
'判断第三行是否输入
If Not Testtxt(Combo1(2).Text) Or Not Testtxt(Combo4(2).Text) Or Not Testtxt(Text1(2).Text) Then
MsgBox "请输入第三行查询条件的内容", 48, "提示"
Combo1(2).SetFocus
Exit Sub
Else
txtSQLlin = txtSQLlin & RelationName(Trim(Combo7(1).Text)) & " " & Fieldname(Combo1(2).Text) & Trim(Combo4(2).Text) & "'" & Trim(Text1(2).Text) & "'"
' Txtsql = Txtsql & RelationName(Combo3(1).Text) & " " & Fieldname(Combo1(2).Text) & Combo2(2).Text & "'" & Trim(Text1(2).Text) & "'"
End If
End If
Set Mrclin = ExecuteSQL(txtSQLlin, MsgText)
With MSFlexGrid1
.Rows = 1 '内容在当前单元格的水平和垂直居中对齐
.CellAlignment = 4 '内容在当前列居中对齐
.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) = "备注"
Do While Not Mrclin.EOF
.Rows = .Rows + 1
.CellAlignment = 4
.CellAlignment = 4
.TextMatrix(.Rows - 1, 0) = Trim(Mrclin.Fields(1))
.TextMatrix(.Rows - 1, 1) = Trim(Mrclin.Fields(3))
.TextMatrix(.Rows - 1, 2) = Trim(Mrclin.Fields(6))
.TextMatrix(.Rows - 1, 3) = Trim(Mrclin.Fields(7))
.TextMatrix(.Rows - 1, 4) = Trim(Mrclin.Fields(8)) & ""
.TextMatrix(.Rows - 1, 5) = Trim(Mrclin.Fields(9)) & ""
.TextMatrix(.Rows - 1, 6) = Trim(Mrclin.Fields(11))
.TextMatrix(.Rows - 1, 7) = Trim(Mrclin.Fields(12))
.TextMatrix(.Rows - 1, 8) = Trim(Mrclin.Fields(13))
Mrclin.MoveNext
Loop
Call AdjustColWidth(cmdinquir, MSFlexGrid1)
End With
If Mrclin.RecordCount = 0 Then
MsgBox "没有您要查询的信息", 48, "提示"
Combo1(0).SetFocus
End If
Mrclin.Close
End Sub
这个组合查询要好好理解一些 写的时候一定要细心 遇到错误 多用断点 监事窗口 立即窗口等工具检查问题所在,然后一个会了 其他的就没什么问题的