vb复合查询

前端之家收集整理的这篇文章主要介绍了vb复合查询前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在敲《机房收费系统》的时候,我们遇到了一个组合查询,经过不懈努力,终于做出来了,现把思路和代码总结如下:

首先是组合框列表项的载入,在Form_Load中添加如下代码

  1. For intIndex = 0 To 2
  2. cboFields(intIndex).AddItem "教师"
  3. cboFields(intIndex).AddItem "注册日期"
  4. cboFields(intIndex).AddItem "注册时间"
  5. cboFields(intIndex).AddItem "注销日期"
  6. cboFields(intIndex).AddItem "注销时间"
  7. cboFields(intIndex).AddItem "机器名"
  8. cboFields(intIndex).ListIndex = 0
  9. cboOperator(intIndex).AddItem "="
  10. cboOperator(intIndex).AddItem "<"
  11. cboOperator(intIndex).AddItem ">"
  12. cboOperator(intIndex).AddItem "<>"
  13. cboOperator(intIndex).ListIndex = 0
  14. Next intIndex
  15. For intIndex = 0 To 1
  16. cboRelation(intIndex).AddItem ""
  17. cboRelation(intIndex).AddItem "或"
  18. cboRelation(intIndex).AddItem "与"
  19. cboRelation(intIndex).ListIndex = 0
  20. Next intIndex
这里运用了控件数组,载入列表项时再用for循环,大大减少代码量。因为这些列表在查询的时候必然会选择一项内容,所以通过ListIndex属性让组合框默认显示一项,这样既可以节省部分用户时间,也避免了对未选择列表项的处理。

下面是“查询”的代码

  1. Private Sub cmdInquiry_Click()
  2. Dim txtsql As String
  3. Dim MsgText As String
  4. Dim mrc As ADODB.Recordset
  5. '字段和关系
  6. Dim strFields(0 To 2) As String
  7. Dim strRelation(0 To 1) As String
  8. '当选择不同的字段时,赋给变量相应的数据库字段名
  9. For intIndex = 0 To 2
  10. Select Case cboFields(intIndex).Text
  11. Case "教师"
  12. strFields(intIndex) = "UserID"
  13. Case "注册日期"
  14. strFields(intIndex) = "LoginDate"
  15. Case "注册时间"
  16. strFields(intIndex) = "LoginTime"
  17. Case "注销日期"
  18. strFields(intIndex) = "logoutDate"
  19. Case "注销时间"
  20. strFields(intIndex) = "logoutTime"
  21. Case "机器名"
  22. strFields(intIndex) = "computer"
  23. End Select
  24. Next intIndex
  25. '关系
  26. For intIndex = 0 To 1
  27. Select Case cboRelation(intIndex).Text
  28. Case "或"
  29. strRelation(intIndex) = "or"
  30. Case "与"
  31. strRelation(intIndex) = "and"
  32. End Select
  33. Next intIndex
  34. '防止查询条件为空
  35. If txtContent1.Text = "" Then
  36. MsgBox "请输入要查询内容!",vbOKOnly + vbInformation,"提示"
  37. txtContent1.SetFocus
  38. Exit Sub
  39. End If
  40. '如果只有一个查询条件
  41. If cboRelation(0).Text = "" And cboRelation(1).Text = "" Then
  42. txtsql = "select * from worklog_Info where " & strFields(0) & cboOperator(0).Text & "'" & Trim(txtContent1.Text) & "'"
  43. Set mrc = Executesql(txtsql,MsgText)
  44. End If
  45. '如果为一二组合查询
  46. If cboRelation(0).Text <> "" And cboRelation(1).Text = "" Then
  47. '防止查询条件为空
  48. If txtContent2.Text = "" Then
  49. MsgBox "请输入要查询内容!","提示"
  50. txtContent2.SetFocus
  51. Exit Sub
  52. End If
  53.  
  54. txtsql = "select * from worklog_Info where " & strFields(0) & cboOperator(0).Text & "'" & Trim(txtContent1.Text) & "'" _
  55. & " " & strRelation(0) & " " & strFields(1) & cboOperator(1).Text & "'" & Trim(txtContent2.Text) & "'"
  56. Set mrc = Executesql(txtsql,MsgText)
  57. End If
  58. '如果为一二三组合查询
  59. If cboRelation(0).Text <> "" And cboRelation(1).Text <> "" Then
  60. '防止查询条件为空
  61. If txtContent2.Text = "" Then
  62. MsgBox "请输入要查询内容!","提示"
  63. txtContent2.SetFocus
  64. Exit Sub
  65. End If
  66. If txtContent3.Text = "" Then
  67. MsgBox "请输入要查询内容!","提示"
  68. txtContent3.SetFocus
  69. Exit Sub
  70. End If
  71.  
  72. txtsql = "select * from worklog_Info where " & strFields(0) & cboOperator(0).Text & "'" & Trim(txtContent1.Text) & "'" _
  73. & " " & strRelation(0) & " " & strFields(1) & cboOperator(1).Text & "'" & Trim(txtContent2.Text) & "'" _
  74. & " " & strRelation(1) & " " & strFields(2) & cboOperator(2).Text & "'" & Trim(txtContent3.Text) & "'"
  75. Set mrc = Executesql(txtsql,MsgText)
  76. End If
  77. '如果用户选择了组合关系2没选组合关系1提示用户
  78. If cboRelation(0).Text = "" And cboRelation(1).Text <> "" Then
  79. MsgBox "请选择组合关系!","提示"
  80. cboRelation(0).SetFocus
  81. Exit Sub
  82. End If
  83. With MSHFlexGrid1
  84. .Rows = 1
  85. .Cols = 8
  86. .CellAlignment = 4
  87. .TextMatrix(0,0) = "序列号"
  88. .TextMatrix(0,1) = "教师"
  89. .TextMatrix(0,2) = "级别"
  90. .TextMatrix(0,3) = "注册日期"
  91. .TextMatrix(0,4) = "注册时间"
  92. .TextMatrix(0,5) = "注销日期"
  93. .TextMatrix(0,6) = "注销时间"
  94. .TextMatrix(0,7) = "机器名"
  95. Do While Not mrc.EOF
  96. .Rows = .Rows + 1
  97. .CellAlignment = 4
  98. .TextMatrix(.Rows - 1,0) = mrc!serial
  99. .TextMatrix(.Rows - 1,1) = mrc!UserID
  100. .TextMatrix(.Rows - 1,2) = mrc!Level
  101. .TextMatrix(.Rows - 1,3) = mrc!LoginDate
  102. .TextMatrix(.Rows - 1,4) = mrc!LoginTime
  103. If IsNull(mrc.Fields("logoutDate")) Then
  104. .TextMatrix(.Rows - 1,5) = ""
  105. Else
  106. .TextMatrix(.Rows - 1,5) = mrc!logoutDate
  107. End If
  108. If IsNull(mrc.Fields("logoutTime")) Then
  109. .TextMatrix(.Rows - 1,6) = ""
  110. Else
  111. .TextMatrix(.Rows - 1,6) = mrc!logoutTime
  112. End If
  113. .TextMatrix(.Rows - 1,7) = mrc!computer
  114. mrc.MoveNext
  115. Loop
  116. End With
  117. mrc.Close
  118.  
  119. End Sub
这里只是实现了查询功能,但是假如用户不按顺序,直接在最后一行输入查询内容,不选组合方式,程序不会得到查询结果,也不会进行任何提示,这里仅仅展示查询的逻辑过程,细节方面还需要读者自行完善。


初学者,不当之处敬请批评指正!

欢迎光临我的网易博客http://blog.163.com/liu_xiaochun/

猜你在找的VB相关文章