1. 程式人生 > >第一次機房收費系統—組合查詢

第一次機房收費系統—組合查詢

  今天總結組合查詢,組合查詢無非涉及到兩個語句!敲機房給我的一點想法就是,有的程式碼自己還不確定是否在這個世界上時候存在,其實它是存在的,恐於自己怕浪費時間,功能還沒有實現。還是太將就了,所以小夥伴們做得時候有想法一定要大膽嘗試哦!找到每個窗體後面最基本最簡單的那個道理。加油!

組合查詢:

先放兩張有代表性的組合查詢的圖片:

1.學生資訊管理系統:(組合查詢)

       

2.機房收費系統:(組合查詢)

             

比較:

  1.學生資訊管理系統:

     大家可以明顯的看出學生組合查詢的條件有三個,分別是學號查詢、姓名查詢、班號查詢。這三個條件任意組合,三者之間的關係只能為“與”。

  2.部分程式碼展示:

Private Sub cmdInquire_Click()
    Dim txtSQL As String
    Dim MsgText As String
    Dim sMeg As String
    Dim dd(4) As Boolean
    Dim mrc As ADODB.Recordset
    
    '組合 SQL 語句
    txtSQL = "select * from student_Info where"
    ' 判斷是否選擇學號查詢方式
    If Check1.Value Then
        If Trim(txtSID.Text) = "" Then
         sMeg = "學號不能為空"
         MsgBox sMeg, 48, "警告"
         txtSID.SetFocus
         Exit Sub
    Else
        '判斷輸入學號是否為數字
        If Not IsNumeric(Trim(txtSID.Text)) Then
             MsgBox "請輸入數字!", 48, "警告"
             Exit Sub
             txtSID.SetFocus
        End If
        dd(0) = True
        '組合查詢語句
        txtSQL = txtSQL & " student_ID='" & Trim(txtSID.Text) & "'"
        End If
    End If
         
    '判斷是否選擇姓名查詢方式
    If Check2.Value Then
       '判斷是否輸入姓名
       If Trim(txtName.Text) = "" Then
            sMeg = "使用者未輸入姓名!"
            MsgBox sMeg, 48, "警告"
            txtName.SetFocus
            Exit Sub
        Else
            dd(1) = True
            If dd(0) Then
               '組合查詢語句
              txtSQL = txtSQL & "and student_Name='" & txtName.Text & "'"
            Else
              txtSQL = txtSQL & " student_Name='" & txtName.Text & "'"
            End If
        End If
    End If
    
        '判斷是否選擇班號查詢方式
    If Check3.Value Then
        '判斷是否輸入班號
        If Trim(txtClassno.Text) = "" Then
            sMeg = "未輸入班號!"
            MsgBox sMeg, 48, "警告"
            txtClassno.SetFocus
            Exit Sub
        Else
            dd(2) = True
            If dd(0) Or dd(1) Then
                 '組合查詢語句
               txtSQL = txtSQL & " and class_No='" & txtClassno.Text & "'"
Else txtSQL = txtSQL & " class_No='" & txtClassno.Text & "'" End If End If End If '判斷是否設定查詢方式 If Not (dd(0) Or dd(1) Or dd(3)) Then MsgBox "請選擇查詢方式!", 48, "警告" Exit Sub End If ' 查詢所有滿足條件的內容 txtSQL = txtSQL & " order by student_ID" ' 執行查詢語句 Set mrc = ExecuteSQL(txtSQL, MsgText) '將查詢內容顯示在表格控制元件中 With myFlexgrid .rows = 2 .CellAlignment = 4 .TextMatrix(1, 0) = "性別" .TextMatrix(1, 1) = "班號" .TextMatrix(1, 2) = "學號" .TextMatrix(1, 3) = "姓名" .TextMatrix(1, 4) = "出生日期" .TextMatrix(1, 5) = "聯絡電話" .TextMatrix(1, 6) = "入校時間" .TextMatrix(1, 7) = "家庭住址" ' 判斷是否移動到資料集物件的最後一條 MsgBox txtSQL MsgBox MsgText Do While Not mrc.EOF .rows = .rows + 1 .CellAlignment = 4 .TextMatrix(.rows - 1, 0) = mrc.Fields(2) .TextMatrix(.rows - 1, 1) = mrc.Fields(4) .TextMatrix(.rows - 1, 2) = mrc.Fields(0) .TextMatrix(.rows - 1, 3) = mrc.Fields(1) .TextMatrix(.rows - 1, 4) = Format(mrc.Fields(3), "yyyy-mm-dd") .TextMatrix(.rows - 1, 5) = mrc.Fields(5) .TextMatrix(.rows - 1, 6) = Format(mrc.Fields(6), "yyyy-mm-dd") .TextMatrix(.rows - 1, 7) = mrc.Fields(7) '移動到下一條記錄 mrc.MoveNext Loop End With mrc.Close End Sub
Private Sub Check1_Click()

    If Check1.Value = 1 Then
    txtSID.Enabled = True
    txtSID.Visible = True
    Else
    txtSID.Text = ""
    txtSID.Enabled = False
    txtSID.Visible = False
    End If
    
    '檢查其他的複選框是否選中
    
    If Check2.Value = 0 Then
    txtName.Text = ""
    txtName.Enabled = False
    txtName.Visible = False
    End If
    
    If Check3.Value = 0 Then
    txtClassno.Text = ""
    txtClassno.Enabled = False
    txtClassno.Visible = False
    End If 
End Sub

 機房收費系統

  

  1.從上圖可以看出

  一組查詢包含三個控制元件的查詢內容,組合關係分別為“與”“或”。如果第一個組合關係選中,第二行必須有查詢條件,同樣第二組組合查詢選中,第三行必須有組合查詢條件。

2.思路圖:


3.部分程式碼展示

Private Sub CmdFind_Click()
Dim txtSql As String
Dim msgText As String
Dim mrc As ADODB.Recordset

txtSql = "select * from line_Info where "
'在line_Info這張表中獲得整行記錄。其中*表示整行記錄,也可以換成你需要查詢的具體記錄。
      
If Trim(Combo1.Text) = "" Or Trim(Combo4.Text) = "" Or Trim(Text1.Text) = "" Then
   MsgBox "請將選項內容填寫完整!", vbOKOnly, "提示"
   Exit Sub
Else
   
   txtSql = txtSql & " " & field(Combo1.Text) & " " & Combo4.Text & "'" & Trim(Text1.Text) & "'"
          
   If Combo7.Text <> "" Then
   '判斷第一個組合關係是否選中
       
       If Trim(Combo2.Text) = "" Or Trim(Combo5.Text) = "" Or Trim(Text2.Text) = "" Then
       '如果選中,判斷第二行內容是否填寫完整,且符合要求
           MsgBox "請將第二行選項內容填寫完整!", vbOKOnly, "提示"
           Exit Sub
       Else
           txtSql = txtSql & " " & field(Combo7.Text) & " " & field(Combo2.Text) & " " & Combo5.Text & "'" & Trim(Text2.Text) & "'"
           '將前兩行的條件聯絡起來,完成SQL語句
            If Combo8.Text <> "" Then
            '判斷第二個組合關係是否選中
                 If Combo3.Text = "" Or Combo6.Text = "" Or Text3.Text = "" Then
                 '如果選中,判斷第二行內容是否填寫完整,且符合要求
                     MsgBox "請將第三行選項內容填寫完整!", vbOKOnly, "提示"
                     Exit Sub
                 Else
                     txtSql = txtSql & " " & field(Combo8.Text) & " " & field(Combo3.Text) & " " & Trim(Combo6.Text) & "'" & Trim(Text3.Text) & "'"
                     '將三行的條件聯絡起來,完成SQL語句

                 End If
            End If
        End If
   End If
End If

總結:找到組合查詢最簡單做基本的那個語句。然後按照你的思路進行程式碼描述,進行資訊的輸入與輸出。

txtSQL = "select * from 表名稱 where"  
txtSQL = txtSQL & " " & field(組合關係) & " " & field(某行條件中運算子前的內容) & " " & 該行條件中的運算子號 & "'" & 運算子後的內容 & "'"  
注意:我們看機房組合查詢欄位名,由於個別欄位名的特殊原因,操作符是有限制的,假如欄位名裡面有性別、卡號、姓名、備註之類的欄位名,操作符只能是“= <>”。欄位名為日期和時間時,查詢的內容需要有合適要求。當然程式碼可以進行限制。特別值得注意,未來的我們不能在這上面有任何一點馬虎。 解決程式碼:
Select Case Combo1.Text
        Case "姓名", "卡號", "備註", "性別"
        
            Combo4.Clear
            Combo4.AddItem "="
            Combo4.AddItem "<>"
        Case "上機日期", "下機日期", "上機時間", "下機時間", "餘額", "消費金額"
            Combo4.Clear
            Combo4.AddItem "="
            Combo4.AddItem "<>"
            Combo4.AddItem "<"
            Combo4.AddItem ">"
    End Select
技巧: 1.快速清除所有文字框、combox、以及MSFlexGrid1 內容
Dim ctl As Control   '定義一個變數,把控制元件裝載到裡面
   For Each ctl In Controls
    '刪除所有text文字框內容
   If TypeOf ctl Is TextBox Then ctl.Text = ""
       Next ctl
   '刪除所有combobox文字框內容
   For Each ctl In Controls
       If TypeOf ctl Is ComboBox Then ctl.Text = ""
   Next ctl
  MSFlexGrid1.Clear
2.當選擇的條件為日期時,日期和時間有格式要求,但是如果在Text控制元件中輸入時間的具體格式,此時比較麻煩。這時用到DTPicker控制元件。可以和Text控制元件進行互動使用。
If Combo3.Text = "上機日期" Or Combo3.Text = "下機日期" Then
        DTPicker3.Format = dtpCustom
        DTPicker3.CustomFormat = Format("yyyy-MM-dd")
        DTPicker3.Visible = True
        Text3.Visible = False
        Text3.Text = DTPicker1.Value
    Else
     If Combo3.Text = "上機時間" Or Combo3.Text = "下機時間" Then
            DTPicker3.Format = dtpTime
            DTPicker3.Visible = True
            Text3.Visible = False
            Text3.Text = DTPicker1.Value
     Else
            DTPicker3.Visible = False
            Text3.Visible = True
    End If
  End If
最近看小夥伴的部落格,收穫頗多,最後三點的技巧就是來自李光小夥伴。讓我的第一次機房收費系統顯得不要不要的!站在巨人的肩膀上,這個世界原來是這個樣子的。