在学生信息管理系统中我们用到了组合查询,就是通过用check项,将SQL 语句一点点完善,
最后在数据库中查询。
机房收费系统中同样用到了组合查询,差别是提供字段让我们选择,还有就是用到了“and”和“or ”来组合
这里我们引入了comboBox.Tag 属性。用comboBox.Tag来代替选择的字段名。
代码如下:
第一步:将选择的字段名用数据库中的字段代替。
Private Sub Combo1_Change()
If cmbField1.Text = "教师" Then cmbField1.Tag = "UserID"
If cmbField1.Text = "注册日期" Then cmbField1.Tag = "LoginDate"
If cmbField1.Text = "注册时间" Then cmbField1.Tag = "LoginTime"
If cmbField1.Text = "注销日期" Then cmbField1.Tag = "LogoutDate"
If cmbField1.Text = "注销时间 " Then cmbField1.Tag = "LogoutTime"
If cmbField1.Text = "机器号" Then cmbField1.Tag = "computer"
End Sub
Private Sub cmbMode2_Click()
If cmbMode2.Text = "与" Then cmbMode2.Tag = "and"
If cmbMode2.Text = "或" Then cmbMode2.Tag = "or "
End Sub
第二部:编写SQL查询代码,一点点完善
例如:(select * from worklog_Info where ****order by serial)
Private Sub cmdInquiry_Click()
Dim txtSQL As String
Dim MsgText As String
Dim mrc As ADODB.Recordset
txtSQL = "select * from worklog_Info where (" & cmbField1.Tag & cmbSign1.Text & " '" & txtInquire1.Text & "'"
If cmbMode2.Text <> "" Then
If cmbField2.Text = "" Then
MsgBox "请选择字段名!", vbOKOnly + vbExclamation, "警告"
cmbField2.SetFocus
Exit Sub
End If
If cmbSign2.Text = "" Then
MsgBox "请选择操作符!", vbOKOnly + vbExclamation, "警告"
cmbSign2.SetFocus
Exit Sub
End If
If txtInquire2.Text = "" Then
MsgBox "请输入要查询的内容!", vbOKOnly + vbExclamation, "警告"
txtInquire2.SetFocus
Exit Sub
End If
txtSQL = txtSQL & " " & cmbMode2.Tag & " " & cmbField2.Tag & cmbSign2.Text & " '" & txtInquire2.Text & "'"
End If
If cmbMode3.Text <> "" Then
If cmbField3.Text = "" Then
MsgBox "请输入字段名!", vbOKOnly + vbExclamation, "警告"
cmbField3.SetFocus
Exit Sub
End If
If cmbSign3.Text = "" Then
MsgBox "a请输入操作符!", vbOKOnly + vbExclamation, "警告"
cmbSign3.SetFocus
Exit Sub
End If
If txtInquire3.Text = "" Then
MsgBox "请输入要查询的内容!", vbOKOnly + vbExclamation, "警告"
txtInquire3.SetFocus
Exit Sub
End If
txtSQL = txtSQL & " " & cmbMode3.Tag & " " & cmbField3.Tag & cmbSign3.Text & " ' " & txtInquire3.Text & " '"
End If
txtSQL = txtSQL & ") order by serial"
Set mrc = ExecuteSQL(txtSQL, MsgText)
If mrc.RecordCount = 0 Then
myFlexGrid.Clear
MsgBox "结果集为空!", vbOKOnly + vbExclamation, "警告"
Exit Sub
End If