1. 程式人生 > >機房收費系統--學生基本資訊維護(組合查詢)

機房收費系統--學生基本資訊維護(組合查詢)

對於這個窗體來說,主要的功能就是實現“組合查詢”,什麼是組合查詢?

組合查詢:

多數SQL查詢都只包含從一個或多個表中返回資料的單條 SELECT 語句。MySQL也允許執行多個查詢(多條 SELECT 語句),並將結果作為單個查詢結果集返回。這些組合查詢通常稱為並(union)或複合查詢(compound query)。

有兩種基本情況,其中需要使用組合查詢:

1.在單個查詢中從不同的表返回類似結構的資料;

2.對單個表執行多個查詢,按單個查詢返回資料。

我們這個窗體實現的就是: 對單個表執行多個查詢

對於“學生基本資訊維護”窗體,我實現的方法是藉助於“組合關係”控制元件是否為空,若為空則不存在組合關係,若不為空即存在組合關係,那麼需要再次輸入一行的查詢條件,

運用倆次的  if  trim(if releation.text=””) 判斷語句,來判斷是否需要進行組合查詢;

其實在敲這個窗體之前我是處於矇蔽狀態的,但是多虧了流程圖幫助我理清了實現這個窗體的邏輯關係。

下面是我的流程圖:

實現這個窗體我程式碼的主要框架是利用了if .. then...end if 

下面是我的程式碼,我感覺有些程式碼量大的,但是很清晰,歡迎大家指點;

Private Sub cmdOk_Click()
      Dim mrc As ADODB.Recordset
      Dim txtsql As String
      Dim msgtxt As String
      Dim cola As String
      Dim colb As String
      Dim colc As String
      
      
      '判斷選擇條件是否為空
      If Trim(col1.Text = "") Then
            MsgBox "請輸入要查詢的欄位名字", vbOKOnly + vbExclamation, "警告"
      Else
      
            If Trim(fuhao1.Text = "") Then
                  MsgBox "請輸入操作符", vbOKOnly + vbExclamation, "警告"
            Else
            
                  If Trim(txt1.Text = "") Then
                        MsgBox "請輸入有查詢的內容", vbOKOnly + vbExclamation, "警告"
                  Else
                        If col1.Text = "卡號" Then
                              cola = " cardno"
                              Else
                                    If col1.Text = "學號" Then
                                    cola = " studentno"
                                    Else
                                          If col1.Text = "姓名" Then
                                          cola = " studentName"
                                          Else
                                                If col1.Text = "性別" Then
                                                cola = " sex"
                                                Else
                                                      If col1.Text = "系別" Then
                                                      cola = " department"
                                                      Else
                                                            If col1.Text = "年級" Then
                                                            cola = " grade"
                                                            Else
                                                                  If col1.Text = "班級" Then
                                                                  cola = " class"
                                                                  End If
                                                            End If
                                                      End If
                                                End If
                                          End If
                                    End If
                              End If
                        Y1 = True
                        txtsql = "select * from student_info where" & cola & fuhao1.Text & "'" & txt1.Text & "'"
                  End If
            End If
      End If
      
      '如果使用者選擇了關係操作符,說明要有組合查詢,那麼組合查詢的資訊需要驗證是否完整
      
      If Not Trim(relation1.Text = "") Then '如果第一個組合查詢不為空,說明使用者已經選擇
            a = True '用來判斷已經選擇了第一個關係操作符號
            
            If Trim(col2.Text = "") Then
                  MsgBox "請輸入要查詢的欄位名字"
            Else
            
                  If Trim(fuhao2.Text = "") Then
                        MsgBox "請輸入操作符"
                  Else
                  
                        If Trim(txt2.Text = "") Then
                              MsgBox "請輸入要查詢的內容"
                        Else
                              
                                    
                                    
                                    If col2.Text = "卡號" Then
                                          colb = " cardno"
                                          Else
                                                If col2.Text = "學號" Then
                                                colb = " studentno"
                                                Else
                                                      If col2.Text = "姓名" Then
                                                      colb = " studentName"
                                                      Else
                                                            If col2.Text = "性別" Then
                                                            colb = " sex"
                                                            Else
                                                                  If col2.Text = "系別" Then
                                                                  colb = " department"
                                                                  Else
                                                                        If col2.Text = "年級" Then
                                                                        colb = " grade"
                                                                        Else
                                                                              If col2.Text = "班級" Then
                                                                              colb = " class"
                                                                              End If
                                                                        End If
                                                                  End If
                                                            End If
                                                      End If
                                                End If
                                          End If
                                    If Trim(relation1.Text) = "與" Then
                        
                                    txtsql = txtsql & "and" & colb & fuhao2.Text & "'" & txt2.Text & "'"
                                    Else
                                    
                                    txtsql = txtsql & "or" & colb & fuhao2.Text & "'" & txt2.Text & "'"
                                    End If
                                    
                              End If
                        End If
                  End If
            End If
      
      
      If Not Trim(relation2.Text = "") Then
            b = True   '用來判斷已經選擇了第二個關係操作符號
            
            If Trim(col3.Text = "") Then
                  MsgBox "請輸入要查詢的欄位名字"
            Else
            
                  If Trim(fuhao3.Text = "") Then
                        MsgBox "請輸入操作符號"
                        Else
                        If Trim(txt2.Text = "") Then
                              MsgBox "請輸入要查詢的內容"
                        Else
                              
                              If col3.Text = "卡號" Then
                                    colc = " cardno"
                                    Else
                                          If col3.Text = "學號" Then
                                          colc = " studentno"
                                          Else
                                                If col3.Text = "姓名" Then
                                                colc = " studentName"
                                                Else
                                                      If col3.Text = "性別" Then
                                                      colc = " sex"
                                                      Else
                                                            If col3.Text = "系別" Then
                                                            colc = " department"
                                                            Else
                                                                  If col3.Text = "年級" Then
                                                                  colc = " grade"
                                                                  Else
                                                                        If col3.Text = "班級" Then
                                                                        colc = " class"
                                                                        End If
                                                                  End If
                                                            End If
                                                      End If
                                                End If
                                          End If
                                    End If
                              
                              If Trim(relation2.Text) = "與" Then
                              txtsql = txtsql & "and" & colc & fuhao3.Text & "'" & txt3.Text & "'"
                              Else
                              txtsql = txtsql & "or" & colc & fuhao3.Text & "'" & txt3.Text & "'"
                              
                              End If
                              Y3 = True
                              
                        End If
                  End If
            End If
      End If
      
      

           
            If a = True Or b = True Or Y1 = True Then
            
            
           Set mrc = ExecuteSQL(txtsql, msgtxt)
                  If mrc.EOF Then
                        MsgBox "不存在該資訊", vbOKOnly + vbExclamation, "警告"
                  Else
                              
                              With myFlexGrid1
                        .Rows = 2
                        .CellAlignment = 4
                        .TextMatrix(0, 1) = "卡號"
                        .TextMatrix(0, 2) = "學號"
                        .TextMatrix(0, 3) = "姓名"
                        .TextMatrix(0, 4) = "性別"
                        .TextMatrix(0, 5) = "系別"
                        .TextMatrix(0, 6) = "年級"
                        .TextMatrix(0, 7) = "班級"
                        .TextMatrix(0, 8) = "卡上餘額"
                        .TextMatrix(0, 9) = "操作老師"
                              End With
                        
                        
                                    
                  End If
           Do While Not mrc.EOF
                        i = i + 1
                              With myFlexGrid1
                                    .Rows = .Rows + 1
                                    .CellAlignment = 4
                                    .TextMatrix(i, 1) = Trim(mrc.Fields(0))
                                    .TextMatrix(i, 2) = Trim(mrc.Fields(1))
                                    .TextMatrix(i, 3) = Trim(mrc.Fields(2))
                                    .TextMatrix(i, 4) = Trim(mrc.Fields(3))
                                    .TextMatrix(i, 5) = Trim(mrc.Fields(4))
                                    .TextMatrix(i, 6) = Trim(mrc.Fields(5))
                                    .TextMatrix(i, 7) = Trim(mrc.Fields(6))
                                    .TextMatrix(i, 8) = Trim(mrc.Fields(7))
                                    .TextMatrix(i, 9) = Trim(mrc.Fields(9))
                                    mrc.MoveNext
                                    
                              End With
                              
                        Loop
                  
            Else
                  MsgBox "沒有輸入條件無法查詢,請先輸入查詢內容", vbOKOnly + vbExclamation, "警告"
                  
                  
            End If
            
End Sub