機房--學生基本資訊維護(組合查詢)
阿新 • • 發佈:2018-11-25
第一步:
我定義了資料庫查詢程式碼中的三個變數(where 後面的全部定義成了變數,就避免一直寫不同的條件了)
①欄位名
②操作符
③要查詢的內容
第二步:
定義好了之後,把他們定義成函式,封裝,在程式碼中直接呼叫
第三步:
程式碼部分:
(call add 這個程式碼是資料的顯示)
Public mrc As ADODB.Recordset Public txtsql As String Public msgtext As String Private Sub cmdInquiry_Click() Dim c(2) As Boolean '判斷條件中的格子中是否有資料 '判斷三個條件對應的框中是否為空 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 student_Info where " '組合查詢所有的情況 If c(2) Then If cobRelation2.Text = "或" Then '0 or 1 or 2 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 CContent(Content As String) If Content = txtContent1.Text Then CContent = txtContent1.Text Else If Content = txtContent2.Text Then CContent = txtContent2.Text Else If Content = txtContent3.Text Then CContent = txtContent3.Text End If End If End If End Function
②這是要查詢欄位名的定義:
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" End Select End Function
③這是操作符的定義
ublic Function CSymbol(Symbol As String)
Select Case Symbol
Case "="
CSymbol = "="
Case ">"
CSymbol = ">"
Case "<"
CSymbol = "<"
Case "<>"
CSymbol = "<>"
End Select
End Function