前言
机房的小难点之一就是组合查询,刚开始完全没有思路,看了看他人的博客,就感觉豁然开朗其实是很简单的,流程图一画出来就简单多了,剩下的就是代码设计。
定义函数,使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