機房收費系統—組合查詢
阿新 • • 發佈:2019-02-12
Private Sub cmdInquire_Click()
Dim objrs As ADODB.Recordset
Dim txtSQL As String
Dim msgText As String
Dim strA As String '定義三個儲存SQL語句的字串
Dim strB As String
Dim strC As String
'先寫上一個不完整的SQL語句,等用到時再補充
txtSQL = "select * from student_Info where "
'每個字串代表分別是三行三種查詢條件時的三種情況
strA = FieldName(cmbField(0).Text) & Operate(cmbOperate(0).Text) & Trim(txtContent(0).Text)
strB = strA & " " & Connect(cmbConnect(0).Text) & " " & FieldName(cmbField(1).Text) & Operate(cmbOperate(1).Text) & Trim(txtContent(1).Text)
strC = strB & " " & Connect(cmbConnect(1).Text) & " " & FieldName(cmbField(2).Text) & Operate(cmbOperate(2).Text) & Trim(txtContent(2).Text)
'判斷查詢條件,當只有一行條件時,預設選擇第一行的查詢條件,即第一個組合關係沒有選擇;
'當有兩條查詢條件時,預設是選擇前兩行,即第一個組合關係不為空
'當有三行查詢條件時,即每個控制元件都不為空,即第二個組合關係不為空
If Trim(cmbConnect(0).Text) = "" Then
If Trim(cmbField(0).Text) = "" Or Trim(cmbOperate(0).Text) = "" Or (txtContent(0).Text) = "" Then
MsgBox "請在第一行輸入查詢條件", 48, "提示"
Exit Sub
Else
txtSQL = txtSQL & strA
End If
Else
If Trim(cmbConnect(1).Text) = "" Then
If Trim(cmbField(1).Text) = "" Or Trim(cmbOperate(1).Text) = "" Or (txtContent(1).Text) = "" Then
MsgBox "請在第二行輸入查詢條件", 48, "提示"
Exit Sub
Else
txtSQL = txtSQL & strB
End If
Else
If Trim(cmbField(2).Text) = "" Or Trim(cmbOperate(2).Text) = "" Or (txtContent(2).Text) = "" Then
MsgBox "請在第三行輸入查詢條件", 48, "提示"
Exit Sub
Else
txtSQL = txtSQL & strC
End If
End If
End If
Set objrs = ExecuteSQL(txtSQL, msgText)
If objrs.EOF Then
MsgBox "沒有查到記錄", 48, "提示"
Exit Sub
End If
'將查詢到的資料寫到myFlexGrid表格中,在寫入資料之前要注意清空資料表,防止資料重複
With myFlexGrid
.Clear
.Cols = 7
.CellAlignment = 4
.TextMatrix(0, 0) = "卡號"
.TextMatrix(0, 1) = "學號"
.TextMatrix(0, 2) = "姓名"
.TextMatrix(0, 3) = "性別"
.TextMatrix(0, 4) = "系別"
.TextMatrix(0, 5) = "年級"
.TextMatrix(0, 6) = "班級"
.Rows = 1
Do While Not objrs.EOF
.Rows = .Rows + 1
.CellAlignment = 4
.TextMatrix(.Rows - 1, 0) = objrs!cardno
.TextMatrix(.Rows - 1, 1) = objrs!studentNo
.TextMatrix(.Rows - 1, 2) = objrs!studentName
.TextMatrix(.Rows - 1, 3) = objrs!sex
.TextMatrix(.Rows - 1, 4) = objrs!department
.TextMatrix(.Rows - 1, 5) = objrs!grade
.TextMatrix(.Rows - 1, 6) = objrs!Class
objrs.MoveNext
Loop
End With
objrs.Close
End Sub
Dim objrs As ADODB.Recordset
Dim txtSQL As String
Dim msgText As String
Dim strA As String '定義三個儲存SQL語句的字串
Dim strB As String
Dim strC As String
'先寫上一個不完整的SQL語句,等用到時再補充
txtSQL = "select * from student_Info where "
'每個字串代表分別是三行三種查詢條件時的三種情況
strA = FieldName(cmbField(0).Text) & Operate(cmbOperate(0).Text) & Trim(txtContent(0).Text)
strB = strA & " " & Connect(cmbConnect(0).Text) & " " & FieldName(cmbField(1).Text) & Operate(cmbOperate(1).Text) & Trim(txtContent(1).Text)
strC = strB & " " & Connect(cmbConnect(1).Text) & " " & FieldName(cmbField(2).Text) & Operate(cmbOperate(2).Text) & Trim(txtContent(2).Text)
'判斷查詢條件,當只有一行條件時,預設選擇第一行的查詢條件,即第一個組合關係沒有選擇;
'當有兩條查詢條件時,預設是選擇前兩行,即第一個組合關係不為空
'當有三行查詢條件時,即每個控制元件都不為空,即第二個組合關係不為空
If Trim(cmbConnect(0).Text) = "" Then
If Trim(cmbField(0).Text) = "" Or Trim(cmbOperate(0).Text) = "" Or (txtContent(0).Text) = "" Then
MsgBox "請在第一行輸入查詢條件", 48, "提示"
Exit Sub
Else
txtSQL = txtSQL & strA
End If
Else
If Trim(cmbConnect(1).Text) = "" Then
If Trim(cmbField(1).Text) = "" Or Trim(cmbOperate(1).Text) = "" Or (txtContent(1).Text) = "" Then
MsgBox "請在第二行輸入查詢條件", 48, "提示"
Exit Sub
Else
txtSQL = txtSQL & strB
End If
Else
If Trim(cmbField(2).Text) = "" Or Trim(cmbOperate(2).Text) = "" Or (txtContent(2).Text) = "" Then
MsgBox "請在第三行輸入查詢條件", 48, "提示"
Exit Sub
Else
txtSQL = txtSQL & strC
End If
End If
End If
Set objrs = ExecuteSQL(txtSQL, msgText)
If objrs.EOF Then
MsgBox "沒有查到記錄", 48, "提示"
Exit Sub
End If
'將查詢到的資料寫到myFlexGrid表格中,在寫入資料之前要注意清空資料表,防止資料重複
With myFlexGrid
.Clear
.Cols = 7
.CellAlignment = 4
.TextMatrix(0, 0) = "卡號"
.TextMatrix(0, 1) = "學號"
.TextMatrix(0, 2) = "姓名"
.TextMatrix(0, 3) = "性別"
.TextMatrix(0, 4) = "系別"
.TextMatrix(0, 5) = "年級"
.TextMatrix(0, 6) = "班級"
.Rows = 1
Do While Not objrs.EOF
.Rows = .Rows + 1
.CellAlignment = 4
.TextMatrix(.Rows - 1, 0) = objrs!cardno
.TextMatrix(.Rows - 1, 1) = objrs!studentNo
.TextMatrix(.Rows - 1, 2) = objrs!studentName
.TextMatrix(.Rows - 1, 3) = objrs!sex
.TextMatrix(.Rows - 1, 4) = objrs!department
.TextMatrix(.Rows - 1, 5) = objrs!grade
.TextMatrix(.Rows - 1, 6) = objrs!Class
objrs.MoveNext
Loop
End With
objrs.Close
End Sub