該來的總會來的!!!!組合查詢就這麼悄無聲息的到了,
組合查詢 最重要的就是運用數據的概念:
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
這個組合查詢要好好理解一些 寫的時候一定要細心 遇到錯誤 多用斷點 監事窗口 立即窗口等工具檢查問題所在,然後一個會了 其他的就沒什麼問題的