機房-組合查詢的優化版本
阿新 • • 發佈:2018-11-25
問題:
1.選擇卡號,查詢內容框只能輸入數字
2.選擇日期或時間,顯示DTPicker框
解決
1.查詢內容框中寫如下程式碼
Private Sub txtContent1_KeyPress(KeyAscii As Integer) '選擇“卡號”"消費餘額""餘額"欄位名 If cobField1.Text = "卡號" Or cobField1.Text = "消費餘額" Or cobField1.Text = "餘額" Then If KeyAscii = 8 Then ElseIf KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 End If '限制字數 If Len(txtContent1.Text) > 10 Then KeyAscii = 0 KeyAscii = 8 MsgBox "字數超出" End If Else '只能輸入漢字,數字,字母,退格符,:-(時間和日期) If KeyAscii >= -20319 And KeyAscii <= -3652 Or KeyAscii = 8 Or KeyAscii = 58 Or KeyAscii = 45 Then ElseIf KeyAscii < 48 Or (KeyAscii > 57 And KeyAscii < 64) Or (KeyAscii > 91 And KeyAscii < 96) Or KeyAscii > 122 Then KeyAscii = 0 End If '限制字數 If Len(txtContent1.Text) > 10 Then KeyAscii = 0 KeyAscii = 8 MsgBox "字數超出" End If End If End Sub
第一步:首先在窗體載入時隱藏DTPicker框
第二步:在欄位名下的控制元件中寫下如下程式碼
'如果選擇時間,就顯示為時間框 If cobField1.Text = "上機時間" Or cobField1.Text = "下機時間" Then txtContent1.Visible = False DTPicker1.Visible = True DTPicker1.Format = 2 Else '如果點選日期或時間,就顯示日期框 If cobField1.Text = "上機日期" Or cobField1.Text = "下機日期" Then DTPicker1.Visible = True txtContent1.Visible = False DTPicker1.Format = dtpShortDate Else DTPicker1.Visible = False txtContent1.Visible = True End If End If
第三步:
在查詢控制元件下寫下如下程式碼:
'如果時間框顯示,將時間框的值賦給txtContent框
If DTPicker1.Visible = True Then
txtContent1.Text = DTPicker1.Value
End If
查詢語句:
我將查詢語句where之後“欄位名”“=”“內容”都設定了為變數
將其封裝在模組中。
程式碼:
txtsql = "select * from line_Info where "
txtsql = txtsql & CField(cobField1.Text) & CSymbol(cobSymbol1.Text) & "'" & CContent(txtContent1.Text) & "'" Set mrc = ExecuteSQL(txtsql, msgtext)
所以不管怎麼變化內容框裡面的內容或者新增內容框,只需要賦值到原本的內容框中,就可以實現查詢,不用動任何程式碼。
查詢完整程式碼:(順便附上封裝的程式碼)
Private Sub cmdInquiry_Click()
Dim c(2) As Boolean '判斷條件中的格子中是否有資料
'查詢內容變數的例項化
Set ctt = frmInquiryLineSumInfo
'如果時間框顯示,將時間框的值賦給txtContent框
If DTPicker1.Visible = True Then
txtContent1.Text = DTPicker1.Value
End If
'如果時間框顯示,將時間框的值賦給txtContent框
If DTPicker2.Visible = True Then
txtContent2.Text = DTPicker2.Value
End If
'如果時間框顯示,將時間框的值賦給txtContent框
If DTPicker3.Visible = True Then
txtContent3.Text = DTPicker3.Value
End If
'判斷三個條件對應的框中是否為空
If cobField1.Text = "" Or cobSymbol1.Text = "" Or txtContent1.Text = "" Then
Else
c(0) = True
If cobField2.Text = "" Or cobSymbol2.Text = "" Or txtContent2.Text = "" Then
Else
c(1) = True
If cobField3.Text = "" Or cobSymbol3.Text = "" Or txtContent3.Text = "" Then
Else
c(2) = True
End If
End If
End If
txtsql = "select * from line_Info where "
'組合查詢所有的情況
If c(2) Then
'0 or 1 or 2
If cobRelation2.Text = "或" Then
txtsql = txtsql & CField(cobField1.Text) & CSymbol(cobSymbol1.Text) & "'" & CContent(txtContent1.Text) & "'" & "or " & CField(cobField2.Text) & CSymbol(cobSymbol2.Text) & " '" & CContent(txtContent2.Text) & "'" & "or " & CField(cobField3.Text) & CSymbol(cobSymbol3.Text) & " '" & CContent(txtContent3.Text) & "'"
Set mrc = ExecuteSQL(txtsql, msgtext)
Call add
mrc.Close
Else
If cobRelation2.Text = "與" Then
'0 and 1 and 2
txtsql = txtsql & CField(cobField1.Text) & CSymbol(cobSymbol1.Text) & "'" & CContent(txtContent1.Text) & "'" & "and " & CField(cobField2.Text) & CSymbol(cobSymbol2.Text) & " '" & CContent(txtContent2.Text) & "'" & "and " & CField(cobField3.Text) & CSymbol(cobSymbol3.Text) & " '" & CContent(txtContent3.Text) & "'"
Set mrc = ExecuteSQL(txtsql, msgtext)
Call add
mrc.Close
Exit Sub
Else
MsgBox "請輸入第二個組合條件"
End If
End If
Else
If c(1) Then
If cobRelation1.Text = "或" Then
'0 or 1
txtsql = txtsql & CField(cobField1.Text) & CSymbol(cobSymbol1.Text) & "'" & CContent(txtContent1.Text) & "'" & "or " & CField(cobField2.Text) & CSymbol(cobSymbol2.Text) & " '" & CContent(txtContent2.Text) & "'"
Set mrc = ExecuteSQL(txtsql, msgtext)
Call add
mrc.Close
Else
If cobRelation1.Text = "與" Then
'0 and 1
txtsql = txtsql & CField(cobField1.Text) & CSymbol(cobSymbol1.Text) & "'" & CContent(txtContent1.Text) & "'" & "and " & CField(cobField2.Text) & CSymbol(cobSymbol2.Text) & " '" & CContent(txtContent2.Text) & "'"
Set mrc = ExecuteSQL(txtsql, msgtext)
Call add
mrc.Close
Else
MsgBox "請選擇組合關係"
End If
End If
Else
If c(0) Then
'0
txtsql = txtsql & CField(cobField1.Text) & CSymbol(cobSymbol1.Text) & "'" & CContent(txtContent1.Text) & "'"
Set mrc = ExecuteSQL(txtsql, msgtext)
Call add
mrc.Close
Else
MsgBox "請輸入第一條件"
End If
End If
End If
End Sub
封裝的程式碼:
Public Function CField(FieldName As String)
Select Case FieldName
Case "卡號"
CField = "cardno"
Case "學號"
CField = "studentno"
Case "姓名"
CField = "studentname"
Case "性別"
CField = "sex"
Case "系別"
CField = "department"
Case "年級"
CField = "grade"
Case "班級"
CField = "class"
Case "上機日期"
CField = "ondate"
Case "上機時間"
CField = "ontime"
Case "下機日期"
CField = "offdate"
Case "下機時間"
CField = "offtime"
Case "狀態"
CField = "status"
Case "消費金額"
CField = "consume"
Case "餘額"
CField = "cash"
Case "教師"
CField = "userid"
Case "登陸日期"
CField = "logindate"
Case "登陸時間"
CField = "logintime"
Case "登出日期"
CField = "logoutdate"
Case "登出時間"
CField = "logouttime"
Case "機器名"
CField = "computer"
End Select
End Function
Public Function CSymbol(Symbol As String)
Select Case Symbol
Case "="
CSymbol = "="
Case ">"
CSymbol = ">"
Case "<"
CSymbol = "<"
Case "<>"
CSymbol = "<>"
End Select
End Function
Public Function CContent(Content As String)
If Content = ctt.txtContent1.Text Then
CContent = ctt.txtContent1.Text
Else
If Content = ctt.txtContent2.Text Then
CContent = ctt.txtContent2.Text
Else
If Content = ctt.txtContent3.Text Then
CContent = ctt.txtContent3.Text
Else
If Content = ctt.DTPicker1.Value Then
CContent = ctt.DTPicker1.Value
Else
If Content = ctt.DTPicker2.Value Then
CContent = ctt.DTPicker2.Value
Else
If Content = ctt.DTPicker3.Value Then
CContent = ctt.DTPicker3.Value
End If
End If
End If
End If
End If
End If
End Function