機房收費系統之組合查詢(程式碼部分)
阿新 • • 發佈:2018-12-13
最近的事情比較多,本該留出大塊時間整理組合查詢的程式碼部分,結果是分為一段一段的零散時間來整理的,最終整理的這些組合查詢程式碼可能還是有很多冗餘,本來不想拿出來的,但考慮到自己的不足就要展示給大家,這樣自己才能進步嘛,所以還是厚著臉皮把我冗餘的程式碼展示出來了,還望大家多多給予指導^_^
Private Sub cmdquery_Click() Dim mrc As ADODB.Recordset Dim txtSQL As String Dim MsgText As String Dim cmrc As ADODB.Recordset Dim ctxtSQL As String Dim cMsgText As String myflexgrid.Clear myflexgrid.Rows = 1 '條件一查詢 If Not testtxt(comb12.Text) And Not testtxt(comb123.Text) Then If Not testtxt(combf1.Text) Then '判斷欄位一是否有內容 MsgBox "請輸入欄位!", vbOKOnly, "溫馨提示:" combf1.SetFocus Exit Sub End If If Not testtxt(combop1.Text) Then '判斷操作符一是否有內容 MsgBox "請輸入操作符!", vbOKOnly, "溫馨提示:" combop1.SetFocus Exit Sub End If If Not testtxt(txtquery1.Text) Then '判斷文字框一是否有內容 MsgBox "請輸入查詢內容!", vbOKOnly, "溫馨提示:" txtquery1.SetFocus Exit Sub End If If testtxt(combf2.Text) And Not testtxt(comb12.Text) Then '當欄位二有內容時,判斷組合一是否有內容 MsgBox "請輸入查詢條件!", vbOKOnly, "溫馨提示:" comb12.SetFocus Exit Sub End If '從資料庫上下機資訊表中查詢符合欄位一的內容 txtSQL = "select * from Line_Info where " & fieldname(combf1.Text) & "" & combop1.Text & "'" & Trim(txtquery1.Text) & "' and status='正常下機'" Set mrc = ExecuteSQL(txtSQL, MsgText) If mrc.EOF Then '如果無查詢結果,則說明資料不存在 myflexgrid.Clear myflexgrid.Rows = 1 MsgBox "該條件的資料不存在!", vbOKOnly, "溫馨提示:" Exit Sub End If With myflexgrid '如果有資料,那麼就逐條顯示 .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) = "備註" Do While Not mrc.EOF .Rows = .Rows + 1 .CellAlignment = 4 .TextMatrix(.Rows - 1, 0) = Trim(mrc.Fields(1)) .TextMatrix(.Rows - 1, 1) = Trim(mrc.Fields(3)) .TextMatrix(.Rows - 1, 2) = Trim(mrc.Fields(6)) .TextMatrix(.Rows - 1, 3) = Trim(mrc.Fields(7)) .TextMatrix(.Rows - 1, 4) = Trim(mrc.Fields(8)) .TextMatrix(.Rows - 1, 5) = Trim(mrc.Fields(9)) .TextMatrix(.Rows - 1, 6) = Trim(mrc.Fields(11)) .TextMatrix(.Rows - 1, 7) = Trim(mrc.Fields(12)) .TextMatrix(.Rows - 1, 8) = Trim(mrc.Fields(13)) mrc.MoveNext Loop End With mrc.Close End If '條件二查詢 '判斷欄位三有內容時,組合二是否有內容,如果沒有則組合二獲得焦點輸入組合條件 If testtxt(combf3.Text) And Not testtxt(comb123.Text) Then MsgBox "請輸入查詢條件!", vbOKOnly, "溫馨提示:" comb123.SetFocus Exit Sub End If If testtxt(comb12.Text) Then '如果組合一有內容,那麼判斷對應的文字框是否為空 If Not testtxt(combf1.Text) Then '判斷欄位一是否為空 MsgBox "請輸入欄位!", vbOKOnly, "溫馨提示:" combf1.SetFocus Exit Sub End If If Not testtxt(combop1.Text) Then '判斷操作符一是否為空 MsgBox "請輸入操作符!", vbOKOnly, "溫馨提示:" combop1.SetFocus Exit Sub End If If Not testtxt(txtquery1.Text) Then '判斷查詢一是否為空 MsgBox "請輸入查詢內容!", vbOKOnly, "溫馨提示:" txtquery1.SetFocus Exit Sub End If If Not testtxt(combf2.Text) Then '判斷欄位二是否為空 MsgBox "請輸入欄位名!", vbOKOnly, "溫馨提示:" combf2.SetFocus Exit Sub End If If Not testtxt(combop2.Text) Then '判斷操作符二是否為空 MsgBox "請輸入操作符!", vbOKOnly, "溫馨提示:" combop2.SetFocus Exit Sub End If If Not testtxt(txtquery2.Text) Then '判斷查詢二是否為空 MsgBox "請輸入查詢內容!", vbOKOnly, "溫馨提示:" txtquery2.SetFocus Exit Sub End If '從資料庫上下機表中查詢對應條件的結果 txtSQL = "select * from Line_Info where " & fieldname(combf1.Text) & "" & combop1.Text & "'" & Trim(txtquery1.Text) & "' and status='正常下機'" & fieldname(comb12.Text) & " " & fieldname(combf2.Text) & "" & combop2.Text & "'" & Trim(txtquery2.Text) & "' and status='正常下機'" Set mrc = ExecuteSQL(txtSQL, MsgText) If mrc.EOF Then myflexgrid.Clear myflexgrid.Rows = 1 MsgBox "該條件的資料不存在!", vbOKOnly, "溫馨提示:" Exit Sub End If With myflexgrid .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) = "備註" Do While Not mrc.EOF .Rows = .Rows + 1 .CellAlignment = 4 .TextMatrix(.Rows - 1, 0) = Trim(mrc.Fields(1)) .TextMatrix(.Rows - 1, 1) = Trim(mrc.Fields(3)) .TextMatrix(.Rows - 1, 2) = Trim(mrc.Fields(6)) .TextMatrix(.Rows - 1, 3) = Trim(mrc.Fields(7)) .TextMatrix(.Rows - 1, 4) = Trim(mrc.Fields(8)) .TextMatrix(.Rows - 1, 5) = Trim(mrc.Fields(9)) .TextMatrix(.Rows - 1, 6) = Trim(mrc.Fields(11)) .TextMatrix(.Rows - 1, 7) = Trim(mrc.Fields(12)) .TextMatrix(.Rows - 1, 8) = Trim(mrc.Fields(13)) mrc.MoveNext Loop End With End If If testtxt(comb123.Text) Then '如果組合二有內容,判斷一三是否為空 If Not testtxt(combf1.Text) Then '判斷欄位一是否為空 MsgBox "請輸入欄位!", vbOKOnly, "溫馨提示:" combf1.SetFocus Exit Sub End If If Not testtxt(combop1.Text) Then '判斷操作符一是否為空 MsgBox "請輸入操作符!", vbOKOnly, "溫馨提示:" combop1.SetFocus Exit Sub End If If Not testtxt(txtquery1.Text) Then '判斷查詢一是否為空 MsgBox "請輸入查詢內容!", vbOKOnly, "溫馨提示:" txtquery1.SetFocus Exit Sub End If If Not testtxt(combf3.Text) Then '判斷欄位三是否為空 MsgBox "請輸入欄位!", vbOKOnly, "溫馨提示:" combf3.SetFocus Exit Sub End If If Not testtxt(combop3.Text) Then '判斷操作符三是否為空 MsgBox "請輸入操作符!", vbOKOnly, "溫馨提示:" combop3.SetFocus Exit Sub End If If Not testtxt(txtquery3.Text) Then '判斷查詢三是否為空 MsgBox "請輸入查詢內容!", vbOKOnly, "溫馨提示:" txtquery3.SetFocus Exit Sub End If If comb12.Text = "" Then '組合一為空的查詢 txtSQL = "select * from Line_Info where " & fieldname(combf1.Text) & "" & combop1.Text & "'" & Trim(txtquery1.Text) & "' " & fieldname(comb123.Text) & " " & fieldname(combf3.Text) & "" & combop3.Text & " '" & Trim(txtquery3.Text) & "' and status='正常下機'" Set mrc = ExecuteSQL(txtSQL, MsgText) End If If testtxt(comb12.Text) Then '組合一 不為空的查詢 If comb123.Text = "與" Then txtSQL = "select * from Line_Info where (" & fieldname(combf1.Text) & "" & combop1.Text & "'" & Trim(txtquery1.Text) & "' and status='正常下機' " & fieldname(comb12.Text) & " " & fieldname(combf2.Text) & "" & combop2.Text & "'" & Trim(txtquery2.Text) & "' and status='正常下機')and " & fieldname(combf3.Text) & "" & combop3.Text & "'" & Trim(txtquery3.Text) & "' and status='正常下機'" Set mrc = ExecuteSQL(txtSQL, MsgText) End If If comb123.Text = "或" Then txtSQL = "select * from Line_Info where( " & fieldname(combf1.Text) & "" & combop1.Text & "'" & Trim(txtquery1.Text) & " ' and status='正常下機' " & fieldname(comb12.Text) & " " & fieldname(combf2.Text) & "" & combop2.Text & "'" & Trim(txtquery2.Text) & "' and status='正常下機') or " & fieldname(combf3.Text) & "" & combop3.Text & "'" & Trim(txtquery3.Text) & "' and status='正常下機'" Set mrc = ExecuteSQL(txtSQL, MsgText) End If Else txtSQL = "select * from Line_Info where " & fieldname(combf1.Text) & "" & combop1.Text & "'" & Trim(txtquery1.Text) & "' " & fieldname(comb123.Text) & " " & fieldname(combf3.Text) & "" & combop3.Text & " '" & Trim(txtquery3.Text) & "' and status='正常下機'" Set mrc = ExecuteSQL(txtSQL, MsgText) End If With myflexgrid .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) = "備註" Do While Not mrc.EOF .Rows = .Rows + 1 .CellAlignment = 4 .TextMatrix(.Rows - 1, 0) = Trim(mrc.Fields(1)) .TextMatrix(.Rows - 1, 1) = Trim(mrc.Fields(3)) .TextMatrix(.Rows - 1, 2) = Trim(mrc.Fields(6)) .TextMatrix(.Rows - 1, 3) = Trim(mrc.Fields(7)) .TextMatrix(.Rows - 1, 4) = Trim(mrc.Fields(8)) .TextMatrix(.Rows - 1, 5) = Trim(mrc.Fields(9)) .TextMatrix(.Rows - 1, 6) = Trim(mrc.Fields(11)) .TextMatrix(.Rows - 1, 7) = Trim(mrc.Fields(12)) .TextMatrix(.Rows - 1, 8) = Trim(mrc.Fields(13)) mrc.MoveNext Loop End With mrc.Close End If End Sub