Combination query about computer room management in VB version
In the VB version of the computer room management system, a lot of database queries are used, and a lot of combination queries are also used. It is a headache for those who are new to the database to figure out the combination query.
Next, I will simply write a simple combined query in my own way
First put a combined query form to show you the main components
(I like to make a simpler interface, and I may pursue different things!)
Of course, everyone will have some thinking time to design this thing. Even if there are references made by the previous brothers and sisters, of course, in order to save more code, a lot of changes have been made to the previous ones. As shown in the figure below, see if you can see the difference.
Of course, know is that the difference is on the top of the three frames. If you query several conditions, the Enabled of the control below needs a lot of code to control, so I will simply Directly change the value of the Frame.Enabled property to operate, saving a lot of code.
Operator work record query
This query is to connect to the database and query the worklog table
code
'结合第一图对应command来理解此代码
Private Sub cmdExcel_Click()'导出excel
Dim xlApp As Excel.Application '定义一个excel应用程序
Dim xlBook As Excel.Workbook '定义一个工作簿
Dim xlSheet As Excel.Worksheet '定义一个工作表
Dim xlRange As Excel.Range '定义一个单元格范围
Dim i As Integer '定义横坐标
Dim j As Integer '定义纵坐标
Set xlApp = CreateObject("excel.application") '创建excel应用程序对象
Set xlBook = xlApp.Workbooks.Add '创建一个工作簿
Set xlSheet = xlBook.Worksheets(1) '创建一个工作表
DoEvents '转让控制权,以便让操作系统处理其它的事件
With myFlexGrid '将FlexGrid控件中内容导入excel表格中
For i = 0 To .Rows - 1 '遍历所有的行
For j = 0 To .Cols - 1 '遍历所有的列
DoEvents '转让控制权,以便让操作系统处理其它的事件
xlApp.ActiveSheet.Cells(i + 1, j + 1) = .TextMatrix(i, j) '创建新的单元格,并添加flexgrid中的内容
Next j
Next i
End With
xlApp.Visible = True
End Sub
Private Sub cmdExpty_Click()
For Each ctrl In Me.Controls
If TypeOf ctrl Is ComboBox Then
ctrl.Text = ""
End If
If TypeOf ctrl Is TextBox Then
ctrl.Text = ""
End If
Next
myFlexGrid.Clear
myFlexGrid.Rows = 0
End Sub
'查询按钮事件
Private Sub cmdInquire_Click()
Dim MsgText As String
Dim MsgText1 As String
Dim MsgText2 As String
Dim txtsql, txtSQL1, txtSQL2 As String
Dim mrc As adodb.Recordset
Dim a, b, c As String
Dim a1, b1, c1 As String
Dim i
Select Case cb1(0).Text
Case "教师"
a = "UserId"
Case "级别"
a = "level"
Case "注册日期"
a = "LoginDate"
Case "注册时间"
a = "LoginTime"
Case "注销日期"
a = "LogoutDate"
Case "注销时间"
a = "LogoutTime"
Case "机器名"
a = "computer"
End Select
Select Case cb1(1).Text
Case "教师"
b = "UserId"
Case "级别"
b = "level"
Case "注册日期"
b = "LoginDate"
Case "注册时间"
b = "LoginTime"
Case "注销日期"
b = "LogoutDate"
Case "注销时间"
b = "LogoutTime"
Case "机器名"
b = "computer"
End Select
Select Case cb1(2).Text
Case "教师"
c = "UserId"
Case "级别"
c = "level"
Case "注册日期"
c = "LoginDate"
Case "注册时间"
c = "LoginTime"
Case "注销日期"
c = "LogoutDate"
Case "注销时间"
c = "LogoutTime"
Case "机器名"
c = "computer"
End Select
For i = 0 To 2
If tt1(i).Visible = True And tt1(i) = "" Then
tt1(i).SetFocus
End If
Next
If f2.Enabled = True Then
If cb1(1) = "" Or cb2(1) = "" Then
MsgBox "请输入完整查询信息!"
End If
End If
If f3.Enabled = True Then
If cb1(2) = "" Or cb2(2) = "" Then
MsgBox "请输入完整查询信息!"
Exit Sub
End If
End If
If DTP1(0).Visible = True Then
a1 = Format(DTP1(0), "yyyy-mm-dd")
Else
a1 = tt1(0).Text
End If
If DTP1(1).Visible = True Then
b1 = Format(DTP1(1), "yyyy-mm-dd")
Else
b1 = tt1(1)
End If
If DTP1(2).Visible = True Then
c1 = Format(DTP1(2), "yyyy-mm-dd")
Else
c1 = tt1(2).Text
End If
txtsql = "select * from worklog_info where " & a & cb2(0).Text & "'" & a1 & "'"
txtSQL1 = txtsql & cb3.Text & " " & b & cb2(1).Text & " '" & b1 & "'"
txtSQL2 = txtSQL1 & cb6.Text & " " & c & cb2(2).Text & " ' " & c1 & "'"
If cb3.Text = "" Then
Set mrc = ExecuteSQL(txtsql, MsgText)
ElseIf cb6.Text = "" Then
Set mrc = ExecuteSQL(txtSQL1, MsgText1)
ElseIf Trim(cb6.Text) > 0 Then
Set mrc = ExecuteSQL(txtSQL2, MsgText2)
End If
If mrc.EOF Then
MsgBox "暂无无数据", vbOKOnly + vbExclamation, "提示"
End If
If mrc.EOF = False Then
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) = "状态"
.ColWidth(5) = 2000
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) = Format(mrc.Fields(3))
.TextMatrix(.Rows - 1, 4) = Format(mrc.Fields(4))
.TextMatrix(.Rows - 1, 5) = Format(mrc.Fields(5))
.TextMatrix(.Rows - 1, 6) = Format(mrc.Fields(6))
.TextMatrix(.Rows - 1, 7) = Trim(mrc.Fields(7))
.TextMatrix(.Rows - 1, 8) = Trim(mrc.Fields(8))
mrc.MoveNext
Loop
End With
End If
End Sub
Private Sub Form_Load() 'for 循环遍历添加内容
Dim i, j
For i = 0 To 2
tt1(i).Visible = False
Next
f2.Enabled = False
f3.Enabled = False
For i = 0 To 2
With cb1(i)
.AddItem "教师"
.AddItem "注册日期"
.AddItem "注册时间"
.AddItem "注销日期"
.AddItem "注销时间"
.AddItem "机器名"
End With
Next
For j = 0 To 2
With cb2(j)
.AddItem "="
.AddItem "<"
.AddItem ">"
.AddItem "<>"
End With
Next
With cb3
.AddItem ""
.AddItem "and"
.AddItem "or"
End With
With cb6
.AddItem ""
.AddItem "and"
.AddItem "or"
End With
End Sub
Private Sub cb3_click()
If Trim(cb3.Text) = "" Then
f2.Enabled = False
Else
f2.Enabled = True
End If
End Sub
Private Sub cb6_click()
If Trim(cb6.Text) = "" Then
f3.Enabled = False
Else
f3.Enabled = True
End If
End Sub
Private Sub cb1_click(index As Integer) '随cb1(i)的内容来更换
Dim i
For i = 0 To 2
If cb1(i).Text = "教师" Or cb1(i).Text = "机器名" Then
tt1(i).Visible = True
DTP1(i).Visible = False
Else
tt1(i).Visible = False
DTP1(i).Visible = True
End If
Next
End Sub
It can be seen from the code that the display of the logical judgment control is much higher than the database query. When thinking is always scratching my head, I also wrote it after thinking about an event, and I hope everyone can learn from it.