机房-组合查询的优化版本

问题:
1.选择卡号,查询内容框只能输入数字
2.选择日期或时间,显示DTPicker框
解决
1.查询内容框中写如下代码

Private Sub txtContent1_KeyPress(KeyAscii As Integer)
    '选择“卡号”"消费余额""余额"字段名
    If cobField1.Text = "卡号" Or cobField1.Text = "消费余额" Or cobField1.Text = "余额" Then
        If KeyAscii = 8 Then
        ElseIf KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
        End If
        
        '限制字数
        If Len(txtContent1.Text) > 10 Then
            KeyAscii = 0
            KeyAscii = 8
        MsgBox "字数超出"
        
        End If
     Else
         '只能输入汉字,数字,字母,退格符,:-(时间和日期)
        If KeyAscii >= -20319 And KeyAscii <= -3652 Or KeyAscii = 8 Or KeyAscii = 58 Or KeyAscii = 45 Then
        ElseIf KeyAscii < 48 Or (KeyAscii > 57 And KeyAscii < 64) Or (KeyAscii > 91 And KeyAscii < 96) Or KeyAscii > 122 Then KeyAscii = 0
    
        End If
        '限制字数
        If Len(txtContent1.Text) > 10 Then
            KeyAscii = 0
            KeyAscii = 8
        MsgBox "字数超出"
        
        End If
    End If
End Sub

第一步:首先在窗体加载时隐藏DTPicker框
第二步:在字段名下的控件中写下如下代码

 '如果选择时间,就显示为时间框
    If cobField1.Text = "上机时间" Or cobField1.Text = "下机时间" Then
        txtContent1.Visible = False
        DTPicker1.Visible = True
        DTPicker1.Format = 2
    Else
        '如果点击日期或时间,就显示日期框
        If cobField1.Text = "上机日期" Or cobField1.Text = "下机日期" Then
            DTPicker1.Visible = True
            txtContent1.Visible = False
            DTPicker1.Format = dtpShortDate
        Else
             DTPicker1.Visible = False
            txtContent1.Visible = True
        End If
    End If
  

第三步:
在查询控件下写下如下代码:

'如果时间框显示,将时间框的值赋给txtContent框
    If DTPicker1.Visible = True Then
        txtContent1.Text = DTPicker1.Value
    End If

查询语句:
我将查询语句where之后“字段名”“=”“内容”都设置了为变量
将其封装在模块中。
代码:

 txtsql = "select * from line_Info where "
txtsql = txtsql & CField(cobField1.Text) & CSymbol(cobSymbol1.Text) & "'" & CContent(txtContent1.Text) & "'"
                                     Set mrc = ExecuteSQL(txtsql, msgtext)
                                 

所以不管怎么变化内容框里面的内容或者添加内容框,只需要赋值到原本的内容框中,就可以实现查询,不用动任何代码。

查询完整代码:(顺便附上封装的代码)

Private Sub cmdInquiry_Click()
    Dim c(2) As Boolean   '判断条件中的格子中是否有数据
    '查询内容变量的实例化
    Set ctt = frmInquiryLineSumInfo
    
    '如果时间框显示,将时间框的值赋给txtContent框
    If DTPicker1.Visible = True Then
        txtContent1.Text = DTPicker1.Value
    End If
    '如果时间框显示,将时间框的值赋给txtContent框
    If DTPicker2.Visible = True Then
        txtContent2.Text = DTPicker2.Value
    End If
    '如果时间框显示,将时间框的值赋给txtContent框
    If DTPicker3.Visible = True Then
        txtContent3.Text = DTPicker3.Value
    End If
    
        '判断三个条件对应的框中是否为空
         If cobField1.Text = "" Or cobSymbol1.Text = "" Or txtContent1.Text = "" Then
             
         Else
            c(0) = True
         
         
             If cobField2.Text = "" Or cobSymbol2.Text = "" Or txtContent2.Text = "" Then
                
             Else
                c(1) = True
         
             
                 If cobField3.Text = "" Or cobSymbol3.Text = "" Or txtContent3.Text = "" Then
                    
                 Else
                    c(2) = True
                 End If
             End If
         End If
             
             txtsql = "select * from line_Info where "
            
             '组合查询所有的情况
             
                     If c(2) Then
                        '0 or 1 or 2
                             If cobRelation2.Text = "或" Then
                                
                                
                                        txtsql = txtsql & CField(cobField1.Text) & CSymbol(cobSymbol1.Text) & "'" & CContent(txtContent1.Text) & "'" & "or " & CField(cobField2.Text) & CSymbol(cobSymbol2.Text) & " '" & CContent(txtContent2.Text) & "'" & "or " & CField(cobField3.Text) & CSymbol(cobSymbol3.Text) & " '" & CContent(txtContent3.Text) & "'"
                                        Set mrc = ExecuteSQL(txtsql, msgtext)
                 
                                        Call add
                                        mrc.Close
            
                             Else
                                     
                                     If cobRelation2.Text = "与" Then
                                         '0 and 1 and 2
                                         txtsql = txtsql & CField(cobField1.Text) & CSymbol(cobSymbol1.Text) & "'" & CContent(txtContent1.Text) & "'" & "and " & CField(cobField2.Text) & CSymbol(cobSymbol2.Text) & " '" & CContent(txtContent2.Text) & "'" & "and " & CField(cobField3.Text) & CSymbol(cobSymbol3.Text) & " '" & CContent(txtContent3.Text) & "'"
                                         Set mrc = ExecuteSQL(txtsql, msgtext)
                     
                                         Call add
                                         mrc.Close
                                         Exit Sub
                                     Else
                                         MsgBox "请输入第二个组合条件"
                                     End If
                             
                             End If
                     Else
                         
                         If c(1) Then
                                 If cobRelation1.Text = "或" Then
                                     '0 or 1
                                     txtsql = txtsql & CField(cobField1.Text) & CSymbol(cobSymbol1.Text) & "'" & CContent(txtContent1.Text) & "'" & "or " & CField(cobField2.Text) & CSymbol(cobSymbol2.Text) & " '" & CContent(txtContent2.Text) & "'"
                                     Set mrc = ExecuteSQL(txtsql, msgtext)
                                         
                                     Call add
                                     mrc.Close
                                 Else
                                      
                 
                                         If cobRelation1.Text = "与" Then
                                             '0 and 1
                                             txtsql = txtsql & CField(cobField1.Text) & CSymbol(cobSymbol1.Text) & "'" & CContent(txtContent1.Text) & "'" & "and " & CField(cobField2.Text) & CSymbol(cobSymbol2.Text) & " '" & CContent(txtContent2.Text) & "'"
                                             Set mrc = ExecuteSQL(txtsql, msgtext)
                                             
                                             Call add
                                             mrc.Close
                                         Else
                                             MsgBox "请选择组合关系"
                                         End If
                                 
                                 End If
                     
                         Else
                                 If c(0) Then
                             
                                      '0
                                     txtsql = txtsql & CField(cobField1.Text) & CSymbol(cobSymbol1.Text) & "'" & CContent(txtContent1.Text) & "'"
                                     Set mrc = ExecuteSQL(txtsql, msgtext)
                                 
                                     Call add
                                     mrc.Close
                             
                                 Else
                                     MsgBox "请输入第一条件"
                                 End If
                         End If
            End If
    
        
End Sub

封装的代码:

Public Function CField(FieldName As String)

    Select Case FieldName
        Case "卡号"
            CField = "cardno"
            
        Case "学号"
            CField = "studentno"
        Case "姓名"
            CField = "studentname"
        Case "性别"
            CField = "sex"
        Case "系别"
            CField = "department"
        Case "年级"
            CField = "grade"
        Case "班级"
            CField = "class"
        Case "上机日期"
            CField = "ondate"
        Case "上机时间"
            CField = "ontime"
        Case "下机日期"
            CField = "offdate"
        Case "下机时间"
            CField = "offtime"
        Case "状态"
            CField = "status"
        Case "消费金额"
            CField = "consume"
        Case "余额"
            CField = "cash"
        Case "教师"
            CField = "userid"
        Case "登陆日期"
            CField = "logindate"
        Case "登陆时间"
            CField = "logintime"
        Case "注销日期"
            CField = "logoutdate"
        Case "注销时间"
            CField = "logouttime"
        Case "机器名"
            CField = "computer"
    End Select
End Function
Public Function CSymbol(Symbol As String)
    Select Case Symbol
        Case "="
            CSymbol = "="
        Case ">"
            CSymbol = ">"
        Case "<"
            CSymbol = "<"
        Case "<>"
            CSymbol = "<>"
    End Select
End Function
Public Function CContent(Content As String)
    
    If Content = ctt.txtContent1.Text Then
        CContent = ctt.txtContent1.Text
    Else
        If Content = ctt.txtContent2.Text Then
            CContent = ctt.txtContent2.Text
        Else
            If Content = ctt.txtContent3.Text Then
                CContent = ctt.txtContent3.Text
            Else
                If Content = ctt.DTPicker1.Value Then
                    CContent = ctt.DTPicker1.Value
                Else
                    If Content = ctt.DTPicker2.Value Then
                        CContent = ctt.DTPicker2.Value
                    Else
                        If Content = ctt.DTPicker3.Value Then
                            CContent = ctt.DTPicker3.Value
                        End If
                    End If
                End If
            End If
        End If
    End If
    
End Function

猜你喜欢

转载自blog.csdn.net/lclcsdnblink/article/details/84197041