1. 程式人生 > >機房收費系統(三)-組合查詢

機房收費系統(三)-組合查詢

【前言】
一拖再拖,組合查詢終於做完了,並且弄明白了其中的含義。下面就以操作員的工作記錄窗體為例,來總結一下組合查詢吧!
【內容】
組合查詢:
第一行(可單獨查詢)即為第一個條件,第二行為第二個條件,第三行為第三個條件。利用“組合關係”控制元件將條件連線起來,進行組合查詢。

導圖
在這裡插入圖片描述
這張導圖是我看了好多大佬的部落格,和自己的思考之後畫出來的,可能某個地方還不是理解的很透徹,望指點!

查詢程式碼

Private Sub cmdInquiry_Click()

Dim txtSQL1 As String
Dim txtSQL2 As String
Dim txtSQL3 As String
Dim MsgText As String
Dim mrc As ADODB.Recordset
Dim a, b, c As String
    
    '給變數a賦值(當欄位1選擇日期、時間或非日期、非時間)
    If comboField1.Text = "日期" Or comboField1.Text = "時間" Then
        a = DTPicker1.Value
    Else
        a = txtInquiry1.Text
    End If
 
    '給變數b賦值(當欄位2選擇日期、時間或非日期、非時間)
    If comboField2.Text = "日期" Or comboField2.Text = "時間" Then
        b = DTPicker2.Value
    Else
        b = txtInquiry2.Text
    End If
    
    '給變數c賦值(當欄位3選擇日期、時間或非日期、非時間)
    If comboField3.Text = "日期" Or comboField3.Text = "時間" Then
        c = DTPicker3.Value
    Else
        c = txtInquiry3.Text
    End If
    
    '第一行內容是否輸入完整
    If comboField1.Text = "" Then    '判斷欄位名是否為空
        MsgBox "請選擇欄位名!", 48, "警告"
        comboField1.SetFocus
        Exit Sub
    End If
    If comboOptSign1.Text = "" Then  '判斷操作符是否為空
        MsgBox "請選擇操作符!", 48, "警告"
        comboOptSign1.SetFocus
        Exit Sub
    End If
    If txtInquiry1.Text = "" Then    '判斷要查詢的內容是否為空
        MsgBox "請輸入要查詢的內容!", 48, "警告"
        txtInquiry1.SetFocus
        Exit Sub
    End If
    
    '第一個組合關係存在
    txtSQL1 = "select * from worklog_Info where " & field(comboField1.Text) _
            & " " & comboOptSign1.Text & " '" & Trim(a) & " '"
    Set mrc = ExecuteSQL(txtSQL1, MsgText)
    
    If Trim(comboGroup1.Text) <> "" Then
        If Trim(comboField2.Text) = "" Or Trim(comboOptSign2.Text) = "" Or Trim(b = "") Then
            MsgBox "請將第二行內容補充完整!", 48, "警告"
            Exit Sub
        Else
            txtSQL2 = txtSQL1 & " " & fieldname(comboGroup1.Text) & " " & field(comboField2.Text) _
                    & comboOptSign2.Text & "'" & Trim(b) & "'"
            Set mrc = ExecuteSQL(txtSQL2, MsgText)
        End If
    End If
    
    '第二個組合關係存在
    If Trim(comboGroup2.Text) <> "" Then
        If Trim(comboField3.Text) = "" Or Trim(comboOptSign3.Text) = "" Or Trim(c = "") Then
            MsgBox "請將第三行內容補充完整!", 48, "警告"
            Exit Sub
        Else
            txtSQL3 = txtSQL2 & " " & fieldname(comboGroup2.Text) & " " & _
                    field(comboField3.Text) & comboOptSign3.Text & "'" & Trim(c) & "'"
            Set mrc = ExecuteSQL(txtSQL3, MsgText)
        End If
    End If
    
    '判斷要查詢的內容是否存在
    If mrc.EOF Then
        MsgBox "沒有您要查詢的內容,請重新查詢!", vbOKOnly + vbExclamation, "提示"
        MSFlexGrid1.Clear
        Exit Sub
    Else
        With MSFlexGrid1
            .Rows = 1
            .CellAlignment = 4
            .TextMatrix(0, 0) = "序列號"
            .TextMatrix(0, 1) = "教師"
            .TextMatrix(0, 2) = "級別"
            .TextMatrix(0, 3) = "註冊日期"
            .TextMatrix(0, 4) = "註冊時間"
            .TextMatrix(0, 5) = "登出日期"
            .TextMatrix(0, 6) = "登出時間"
            .TextMatrix(0, 7) = "機器名"
            .TextMatrix(0, 8) = "狀態"
        End With
        
        Do While Not mrc.EOF
            With MSFlexGrid1
                .Rows = .Rows + 1
                .CellAlignment = 4
                .TextMatrix(.Rows - 1, 0) = Trim(mrc.Fields(0))
                .TextMatrix(.Rows - 1, 1) = Trim(mrc.Fields(1))
                .TextMatrix(.Rows - 1, 2) = Trim(mrc.Fields(2))
                .TextMatrix(.Rows - 1, 3) = Trim(mrc.Fields(3))
                .TextMatrix(.Rows - 1, 4) = Trim(mrc.Fields(4))
                .TextMatrix(.Rows - 1, 5) = Trim(mrc.Fields(5)) & ""
                .TextMatrix(.Rows - 1, 6) = Trim(mrc.Fields(6)) & ""
                .TextMatrix(.Rows - 1, 7) = Trim(mrc.Fields(7)) & ""
                .TextMatrix(.Rows - 1, 8) = Trim(mrc.Fields(8)) & ""
            End With
            mrc.MoveNext
        Loop
    End If
    mrc.Close
End Sub

在做的過程中必有的思考:在選擇欄位時,當選擇了日期或者時間的時候,是不是用DTPicker1更好一點呢?想到這裡了,就要動手去實踐一下,這部分我也是試過多次,看了許多部落格,才弄明白該如何去寫程式碼。

思考當欄位選擇了時間或日期,“要查詢的內容”控制元件變為DTPicker1,否則還是用原來的文字框。
程式碼實現:變數
1.使用變數來代替文字框或DTPicker1;

Dim a, b, c As String
    '給變數a賦值(當欄位1選擇日期、時間或非日期、非時間)
    If comboField1.Text = "日期" Or comboField1.Text = "時間" Then
        a = DTPicker1.Value
    Else
        a = txtInquiry1.Text
    End If
    '給變數b賦值(當欄位2選擇日期、時間或非日期、非時間)
    If comboField2.Text = "日期" Or comboField2.Text = "時間" Then
        b = DTPicker2.Value
    Else
        b = txtInquiry2.Text
    End If
    '給變數c賦值(當欄位3選擇日期、時間或非日期、非時間)
    If comboField3.Text = "日期" Or comboField3.Text = "時間" Then
        c = DTPicker3.Value
    Else
        c = txtInquiry3.Text
    End If

2.查詢語句中將“要查詢的內容”欄位用變數來代替;

txtSQL1 = "select * from worklog_Info where " & field(comboField1.Text) _
            & " " & comboOptSign1.Text & " '" & Trim(a) & " '"
    Set mrc = ExecuteSQL(txtSQL1, MsgText)
txtSQL2 = txtSQL1 & " " & fieldname(comboGroup1.Text) & " " & field(comboField2.Text) _
                    & comboOptSign2.Text & "'" & Trim(b) & "'"
            Set mrc = ExecuteSQL(txtSQL2, MsgText)
txtSQL3 = txtSQL2 & " " & fieldname(comboGroup2.Text) & " " & _
                    field(comboField3.Text) & comboOptSign3.Text & "'" & Trim(c) & "'"
            Set mrc = ExecuteSQL(txtSQL3, MsgText)

3.當選擇了時間或日期,將DTPicker1欄位顯示,文字框隱藏。

If comboField1.Text = "註冊日期" Then
        DTPicker1.Format = dtpShortDate
        txtInquiry1.Visible = False
        txtInquiry1.Enabled = False
        DTPicker1.Visible = True
        DTPicker1.Enabled = True
        
    ElseIf comboField1.Text = "註冊時間" Then
        DTPicker1.Format = dtpShortTime
        txtInquiry1.Visible = False
        txtInquiry1.Enabled = False
        DTPicker1.Visible = True
        DTPicker1.Enabled = True
    ElseIf comboField1.Text = "登出日期" Then
        DTPicker1.Format = dtpShortDate
        txtInquiry1.Visible = False
        txtInquiry1.Enabled = False
        DTPicker1.Visible = True
        DTPicker1.Enabled = True
        
    ElseIf comboField1.Text = "登出時間" Then
        DTPicker1.Format = dtpShortTime
        txtInquiry1.Visible = False
        txtInquiry1.Enabled = False
        DTPicker1.Visible = True
        DTPicker1.Enabled = True
         
    ElseIf comboField1.Text <> "註冊時間" And comboField1.Text <> "註冊日期" _
           And comboField1.Text <> "登出日期" _
           And comboField1.Text <> "登出時間" Then
        DTPicker1.Visible = False
        DTPicker1.Enabled = False
        txtInquiry1.Visible = True
        txtInquiry1.Enabled = True
    End If