前言
機房的小難點之一就是組合查詢,剛開始完全沒有思路,看了看他人的博客,就感覺豁然開朗其實是很簡單的,流程圖一畫出來就簡單多了,剩下的就是代碼設計。
定義函數,使text屬性中字符與數據庫相對應
Public Function Field(i As String) As String
'定義函數,使text屬性中的字符與數據庫相對應
Select Case i
Case "卡號"
Field = "cardno"
Case "學號"
Field = "studentno"
Case "姓名"
Field = "studentname"
Case "性別"
Field = "sex"
Case "系別"
Field = "depart"
Case "年級"
Field = "grade"
Case "班級"
Field = "class"
Case "與"
Field = "and"
Case "或"
Field = "or"
End Select
End Function
組合查詢代碼
Private Sub cmdContent_Click()
Dim txtSQL, Msgtext As String
Dim mrc_stu As ADODB.Recordset
txtSQL = "select * from student_info where"
If Combo1.Text = "" Or Combo4.Text = "" Or txtContent1.Text = "" Then
MsgBox "請輸入要查詢的內容", 0 + 48, "警告"
Exit Sub
Else
txtSQL = txtSQL & " " & Field(Combo1.Text) & "" & Combo4.Text & " '" & Trim(txtContent1.Text) & "'"
If Combo7.Text <> "" Then
If Combo2.Text = "" Or Combo5.Text = "" Or txtContent2.Text = "" Then
MsgBox "請將第二行內容補充完整", 0 + 48, "警告"
Exit Sub
Else
txtSQL = txtSQL & " " & Field(Combo7.Text) & " " & Field(Combo2.Text) & " " & Combo5.Text & " '" & Trim(txtContent2.Text) & "'"
If Combo8.Text <> "" Then
If Combo3.Text = "" Or Combo6.Text = "" Or txtContent3.Text = "" Then
MsgBox "請將第三行內容補充完整", 0 + 48, "警告"
Exit Sub
Else
txtSQL = txtSQL & " " & Field(Combo8.Text) & " " & Field(Combo3.Text) & "" & Combo6.Text & " '" & Trim(txtContent3.Text) & "'"
End If
End If
End If
End If
End If
Set mrc_stu = ExecuteSQL(txtSQL, Msgtext)
If mrc_stu.EOF Then
MsgBox "沒有數據", 0 + 48, "警告"
txtContent1.Text = ""
txtContent2.Text = ""
txtContent3.Text = ""
txtContent1.SetFocus
myflexgrid.Clear
Exit Sub
Else
With myflexgrid
.Rows = 1
.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) = "狀態"
.TextMatrix(0, 9) = "備註"
.TextMatrix(0, 10) = "類型"
.TextMatrix(0, 11) = "日期"
.TextMatrix(0, 12) = "時間"
Do While Not mrc_stu.EOF
.Rows = .Rows + 1
.CellAlignment = 4
.TextMatrix(.Rows - 1, 0) = Trim(mrc_stu.Fields(2))
.TextMatrix(.Rows - 1, 1) = Trim(mrc_stu.Fields(1))
.TextMatrix(.Rows - 1, 2) = Trim(mrc_stu.Fields(0))
.TextMatrix(.Rows - 1, 3) = Trim(mrc_stu.Fields(7))
.TextMatrix(.Rows - 1, 4) = Trim(mrc_stu.Fields(4))
.TextMatrix(.Rows - 1, 5) = Trim(mrc_stu.Fields(5))
.TextMatrix(.Rows - 1, 6) = Trim(mrc_stu.Fields(6))
.TextMatrix(.Rows - 1, 7) = Trim(mrc_stu.Fields(3))
.TextMatrix(.Rows - 1, 8) = Trim(mrc_stu.Fields(10))
.TextMatrix(.Rows - 1, 9) = Trim(mrc_stu.Fields(8))
.TextMatrix(.Rows - 1, 10) = Trim(mrc_stu.Fields(14))
.TextMatrix(.Rows - 1, 11) = Trim(mrc_stu.Fields(12))
.TextMatrix(.Rows - 1, 12) = Trim(mrc_stu.Fields(13))
mrc_stu.MoveNext
Loop
End With
End If
mrc_stu.Close
End Sub
修改命令
單擊選中整行
MSHFlexGrid控件右擊屬性頁
添加標識
Private Sub myflexgrid_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
'判斷鼠標所在行,並添加*
With myflexgrid
If .MouseRow Then
.TextMatrix(.MouseRow, 13) = "*"
Exit Sub
End If
End With
End Sub
如圖:
修改命令
Private Sub cmdRevise_Click()
Dim n As Integer
Dim m As Integer
If myflexgrid.TextMatrix(myflexgrid.RowSel, 13) <> "*" Then
MsgBox "請選擇數據", 0 + 48, "警告"
Exit Sub
Else
SetParent frmRSInformation.hWnd, frmmenu.hWnd
frmRSInformation.Show
With frmSNDefend.myflexgrid
frmRSInformation.txtSID.Text = .TextMatrix(.RowSel, 0)
frmRSInformation.txtName.Text = .TextMatrix(.RowSel, 1)
frmRSInformation.txtCardNo.Text = .TextMatrix(.RowSel, 2)
frmRSInformation.txtMoney.Text = .TextMatrix(.RowSel, 3)
frmRSInformation.txtDept.Text = .TextMatrix(.RowSel, 4)
frmRSInformation.txtGrade.Text = .TextMatrix(.RowSel, 5)
frmRSInformation.txtClass.Text = .TextMatrix(.RowSel, 6)
frmRSInformation.cboSex.Text = .TextMatrix(.RowSel, 7)
frmRSInformation.txtStatus.Text = .TextMatrix(.RowSel, 8)
frmRSInformation.txtExplain.Text = .TextMatrix(.RowSel, 9)
frmRSInformation.cboType.Text = .TextMatrix(.RowSel, 10)
End With
End If
End Sub