组合窗体顾名思义就是组合查询,刚一看到这个的时候有点发憷,一点一点的尝试,分析,查资料(师哥师姐的讲解非常清楚),慢慢的缕清思路,一点一点的实现,其实每一个地方都不难,跟着思路走就都实现了,三个查询窗体的原理是一样的,这里就拿学生基本信息维护来介绍吧。
思路
(1)窗体加载Msflexgeid显示信息,第二行,第三行隐藏,修改不可选
(2)查询:
当第一行选择数据后,选择组合关系第二行显示;
当一二行选好数据之后,选择组合关系第三行显示;
也可以让控件都显示出来然后Enable=False
注意事项
(1)字段名的应用需要转换成SQL语言
(2)字段名的显示后相应的字段显示相应的操作符
(3)点击修改时没有选中数据是需要提示
下面就简要的展示几个功能的代码,然后自己就能完成了。(代码不全需自行添加)
1、加载窗体
Private Sub Form_Load()
comboField1.AddItem "卡号"
comboField1.AddItem "学号"
comboField1.AddItem "姓名"
comboField1.AddItem "性别"
comboField1.AddItem "系别"
comboField1.AddItem "年级"
comboField1.AddItem "班级"
comboRelation1.AddItem "与"
comboRelation1.AddItem "或"
comboField2.Visible = False
comboOperate2.Visible = False
comboRelation2.Visible = False
txtContent2.Visible = False
comboField3.Visible = False
comboOperate3.Visible = False
txtContent3.Visible = False
MSFlexGrid1.SelectionMode = flexSelectionByRow '设置MSFlexGrid1的选择方式是按行选择
MSFlexGrid1.HighLight = flexHighlightWithFocus '突出显示只在控件有焦点时才显现
With MSFlexGrid1
.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) = "备注"
.TextMatrix(0, 9) = "状态"
.TextMatrix(0, 10) = "日期"
.TextMatrix(0, 11) = "时间"
.TextMatrix(0, 12) = "类型"
End With
cmdmodify.Enabled = False
End Sub
2、操作符的显示
emptyPrivate Sub combofield1_click()
If comboField1.Text = "卡号" Then
comboOperate1.Clear
comboOperate1.AddItem "="
comboOperate1.AddItem ">"
comboOperate1.AddItem "<"
comboOperate1.AddItem "<>"
End If
If comboField1.Text = "系别" Then
comboOperate1.Clear
comboOperate1.AddItem "="
comboOperate1.AddItem "<>"
End If
If comboField1.Text = "年级" Then
comboOperate1.Clear
comboOperate1.AddItem "="
comboOperate1.AddItem ">"
comboOperate1.AddItem "<"
comboOperate1.AddItem "<>"
End If
If comboField1.Text = "班级" Then
comboOperate1.Clear
comboOperate1.AddItem "="
comboOperate1.AddItem "<>"
End If
End Sub
3、字符转换成SQL语言
emptyPublic Function Field(a As String) As String
Select Case a
Case "卡号"
Field = "cardno"
Case "姓名"
Field = "studentname"
Case "学号"
Field = "studentno"
Case "系别"
Field = "department"
Case "班级"
Field = "class"
Case "年级"
Field = "grade"
Case "上机日期"
Field = "ondate"
Case "上机时间"
Field = "ontime"
Case "下机日期"
Field = "offdate"
Case "下机时间"
Field = "offtime"
Case "消费金额"
Field = "consume"
Case "余额"
Field = "cash"
Case "备注"
Field = "status"
Case "与"
Field = "and"
Case "或"
Field = "or"
End Select
End Function
4、查询
Private Sub cmdinquiry_click()
Dim mrc As ADODB.Recordset
Dim txtSQL As String
Dim Msgtext As String
'判断第一行是否为空
'当comboRelation被选中的时候,判断第二行时候为空,以此类推,最后查询txtsql语句
txtSQL = "select * from student_info where "
If Trim(comboField1.Text) = "" Or Trim(comboOperate1.Text) = "" Or Trim(txtContent1.Text) = "" Then
MsgBox "请输入完整的查询条件!", vbOKCancel + vbExclamation, "提示"
Exit Sub
End If
txtSQL = txtSQL & Field(comboField1.Text) & Trim(comboOperate1.Text) & "'" & Trim(txtContent1.Text) & "'"
If Trim(comboRelation1.Text <> "") Then
If Trim(comboField2.Text) = "" Or Trim(comboOperate2.Text) = "" Or Trim(txtContent2.Text) = "" Then
MsgBox "请输入两行完整的查询条件!", vbOKCancel + vbExclamation, "提示"
Exit Sub
End If
txtSQL = txtSQL & Field(comboRelation1.Text) & " " & Field(comboField2.Text) & Trim(comboOperate2.Text) & "'" & Trim(txtContent2.Text) & "'"
End If
If Trim(comboRelation2.Text <> "") Then
If Trim(comboField3.Text) = "" Or Trim(comboOperate3.Text) = "" Or Trim(txtContent3.Text) = "" Then
MsgBox "请输入三行完整的查询条件!", vbOKCancel + vbExclamation, "提示"
Exit Sub
End If
txtSQL = txtSQL & Field(comboRelation2.Text) & " " & Field(comboField3.Text) & Trim(comboOperate3.Text) & "'" & Trim(txtContent3.Text) & "'"
End If
Set mrc = ExecuteSQL(txtSQL, Msgtext)
With MSFlexGrid1
.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) = "备注"
.TextMatrix(0, 9) = "状态"
.TextMatrix(0, 10) = "日期"
.TextMatrix(0, 11) = "时间"
.TextMatrix(0, 12) = "类型"
Do While Not mrc.EOF
.Rows = .Rows + 1
.CellAlignment = 4
.TextMatrix(.Rows - 1, 0) = Trim(mrc.Fields(0))
.TextMatrix(.Rows - 1, 1) = Trim(mrc.Fields(1))
.TextMatrix(.Rows - 1, 2) = Trim(mrc.Fields(2))
.TextMatrix(.Rows - 1, 3) = Trim(mrc.Fields(3))
.TextMatrix(.Rows - 1, 4) = Trim(mrc.Fields(4))
.TextMatrix(.Rows - 1, 5) = Trim(mrc.Fields(5))
.TextMatrix(.Rows - 1, 6) = Trim(mrc.Fields(6))
.TextMatrix(.Rows - 1, 7) = Trim(mrc.Fields(7))
.TextMatrix(.Rows - 1, 8) = Trim(mrc.Fields(8))
.TextMatrix(.Rows - 1, 9) = Trim(mrc.Fields(10))
.TextMatrix(.Rows - 1, 10) = Trim(mrc.Fields(12))
.TextMatrix(.Rows - 1, 11) = Trim(mrc.Fields(13))
.TextMatrix(.Rows - 1, 12) = Trim(mrc.Fields(14))
mrc.MoveNext
Loop
End With
mrc.Close
If MSFlexGrid1.Rows = 1 Then
MsgBox "没有此记录,请重新输入查询信息!", vbOKCancel + vbExclamation, "提示"
Exit Sub
End If
cmdmodify.Enabled = True
End Sub
5、修改
Private Sub cmdmodify_click()
If MSFlexGrid1.Row = 0 Then
MsgBox "请选择数据!", vbOKCancel + vbExclamation, "提示"
Exit Sub
Else
SetParent frmmodifysinfo.hWnd, FrmMain.Picture1.hWnd
frmmodifysinfo.Show
End If
End Sub
每一步都是要用心走下去的,如果不能很专心的去做,那就先用心5分钟。
虽然自己的速度慢了,但是也不要急,一定要调整好自己的步伐,才能走的更好!