All greatness comes from a brave start.
It's hard to combine queries before, but when you really start typing, you'll find it's not difficult. Finish the first one, basically even if it's finished.
This is the standard style of combination query in the computer room charging system. The reason why it is called combination query is that we need to select several conditions and then combine them together to query. Firstly, combinatorial queries are divided into three groups of query conditions and two combinational relations, and combinational relations are two-two-join combinational conditions. So when you choose the first combination relationship, you must have the query conditions of the first two rows, and after choosing the second combination relationship, all three rows must be selected. Conversely, if no combination relationship is selected, the query content of the first row can only be queried.
Take the maintenance of students'basic information as an example to give an explanation.
First, the idea should be clear, so the flow chart should be drawn first.
Then write the code according to the flow chart.
This is part of the code for composite queries.
Dim txtsql As String
Dim msgText As String
Dim mrc As ADODB.Recordset
Dim ctl As Control
Dim ctl1 As Control
txtsql = "select * from student_info where"
'Check input conditions
If Trim(comboField1.Text) = "" Or Trim(comboOpSign1.Text) = "" Or Trim(Text1.Text) = "" Then
MsgBox "Please enter a complete query condition", , "Su Shi reminds you"
Exit Sub
End If
'Combining the first row of query content
txtsql = txtsql & " " & Trim(Field(comboField1.Text)) & " " & Trim((comboOpSign1.Text)) & " " & "'" & Trim(Text1.Text) & "'"
If Trim(comboCombineRe1.Text <> "") Then 'The first combinatorial relationship exists
If Trim(comboField2.Text) = "" Or Trim(comboOpSign2.Text = "") Or Trim(Text2.Text = "") Then
MsgBox "You have chosen the first combination relation. Please enter the second line of query condition.", , "Su Shi reminds you"
Exit Sub
Else
txtsql = txtsql & " " & Field(Trim(comboCombineRe1.Text)) & " " & Field(comboField2.Text) & " " & comboOpSign2.Text & " " & "'" & Trim(Text2.Text) & "'"
End If
End If
If Trim(comboCombineRe2.Text <> "") Then 'The second combinatorial relationship exists
If Trim(comboField3.Text) = "" Or Trim(comboOpSign3.Text = "") Or Trim(Text3.Text = "") Then
MsgBox "You have chosen the second combination relationship. Please enter the third line of query conditions.", , "Su Shi reminds you"
Exit Sub
Else
txtsql = txtsql & " " & Field(Trim(comboCombineRe2.Text)) & " " & Field(comboField3.Text) & " " & comboOpSign3.Text & " " & "'" & Trim(Text2.Text) & "'"
End If
End If
Set mrc = ExecuteSQL(txtsql, msgText)
If mrc.EOF = True Then 'Check whether the information exists, if it does not, give prompts and empty all text boxes
MsgBox "If you don't get the result, maybe the information you enter doesn't exist, or the information is contradictory."
For Each ctl In Controls
'Clear all text
......
Exit Sub
End If
'matching
With myFlexGrid
.rows = 1 'First row naming
.CellAlignment = 4 'The contents of cells are centered and aligned
.TextMatrix(0, 0) = "Card number"
......
Do While Not mrc.EOF
.rows = .rows + 1
.CellAlignment = 4
.TextMatrix(.rows - 1, 0) = Trim(mrc.Fields(0))
......
mrc.MoveNext
Loop
End With
Basically, if you understand the above code, the combination query is about the same.
In addition, add a few tips.
Clear all text with one key. Originally, I wanted to get the code into a module and call it directly, but it was not successful. I could be taught by God's Congress.
Dim ctl As Control
Dim ctl1 As Control
For Each ctl In Controls
'Clear all text
If TypeOf ctl Is TextBox Then
ctl.Text = ""
End If
Next ctl
'Eliminate combobox text
For Each ctl1 In Controls
If TypeOf ctl1 Is ComboBox Then
ctl1.Text = ""
End If
Next ctl1
myFlexGrid.clear
Define a function to match text to fields in the database.
Public Function Field(i As String) As String
'Define a function to let text The text in the database corresponds to the fields in the database.
Select Case i
Case "Card number"
Field = "cardno"
Case "Student ID"
Field = "studentno"
......
Case "and"
Field = "and"
......
End Select
End Function
When clicking on conditions such as date and time, remember to remind the input date to inquire about all kinds. A warm spring window can be set. In addition, conditional operators such as name and gender only have "=" and "<>", so we can do an optimization.
Private Sub comboField1_click()
If comboField1.Text = "Date of boarding" Or comboField1.Text = "Boarding time" Or comboField1.Text = "Date of landing" Or comboField1.Text = "Downtime" Then
MsgBox "The date query format is yyyy/mm/dd,The time query format is hh:mm:ss!", vbOKOnly + vbExclamation, "Su Shi reminds you"
End If
Select Case comboField1.Text
Case "Full name", "Remarks"
comboOpSign1.clear
comboOpSign1.AddItem "="
comboOpSign1.AddItem "<>"
Case "Card number", "Date of boarding", "Boarding time", "Date of landing", "Downtime", "Consumption amount", "balance"
comboOpSign1.clear
comboOpSign1.AddItem "="
comboOpSign1.AddItem "<>"
comboOpSign1.AddItem "<"
comboOpSign1.AddItem ">"
End Select
End Sub
It's not difficult to knock, so we can't wait to feel before we start to act.
Come on.