近些天来,一致在做机房收费系统,看到那么多的条件, 就头疼。可是没办法,不管如何路还是要继续走的啊。最后通过上网查资料,自己想,终于想出来一个解决办法,这个办法可能不是很好,但是能帮你解决问题。
代码如下:
Private Sub Cmdlk_Click() Dim strCf1 As String Dim strCf2 As String Dim strCf3 As String Dim strCop1 As String Dim strCop2 As String Dim strCop3 As String Dim strCc1 As String Dim strCc2 As String Dim strTxtsql As String Dim strMsgtext As String Dim rst As ADODB.Recordset '判断第一个字段选择的那个,并转化为数据库表中字段名 Select Case ComboField1.Text Case "卡号" strCf1 = "cardno" Case "学号" strCf1 = "studentno" Case "姓名" strCf1 = "studentname" Case "性别" strCf1 = "sex" Case "系别" strCf1 = "department" Case "年级" strCf1 = "grade" Case "班号" strCf1 = "class" End Select '判断链接条件 Select Case ComboOperator1.Text Case "=" strCop1 = "=" Case "<" strCop1 = "<" Case ">" strCop1 = ">" Case "<>" strCop1 = "<>" End Select '判断链接条件是与 或 还是空 空 也很重要 Select Case ComboConnect1.Text Case "或" strCc1 = "or" Case "与" strCc1 = "and " Case "" strCc1 = "" End Select '判断第二个字段选择的那个,并转化为数据库表中字段名 Select Case ComboField2.Text Case "卡号" strCf2 = "cardno" Case "学号" strCf2 = "studentno" Case "姓名" strCf2 = "studentname" Case "性别" strCf2 = "sex" Case "系别" strCf2 = "department" Case "年级" strCf2 = "grade" Case "班号" strCf2 = "class" End Select '判断链接条件 Select Case ComboOperator2.Text Case "=" strCop2 = "=" Case "<" strCop2 = "<" Case ">" strCop2 = ">" Case "<>" strCop2 = "<>" End Select '判断链接条件是与 或 还是空 空 也很重要 Select Case ComboConnect2.Text Case "或" strCc2 = "or" Case "与" strCc2 = "and" Case "" strCc2 = "" End Select '判断第三个字段选择的那个,并转化为数据库表中字段名 Select Case ComboField3.Text Case "卡号" strCf3 = "cardno" Case "学号" strCf3 = "studentno" Case "姓名" strCf3 = "studentname" Case "性别" strCf3 = "sex" Case "系别" strCf3 = "department" Case "年级" strCf3 = "grade" Case "班号" strCf3 = "class" End Select '判断链接条件 Select Case ComboOperator3.Text Case "=" strCop3 = "=" Case "<" strCop3 = "<" Case ">" strCop3 = ">" Case "<>" strCop3 = "<>" End Select '根据已经选择的执行语句 If ComboConnect1.Text = "" Then strTxtsql = "select * from student_Info where " & strCf1 & strCop1 & "'" & Trim(TxtLknews1.Text) & "'" '就选择了第一行,即一个条件 Set rst = Executesql(strTxtsql,strMsgtext) Else If ComboField2.Text = "" Or ComboOperator2.Text = "" Or TxtLknews2.Text = "" Then MsgBox "请将字段,操作符,查询条件添加完整。",vbOKCancel + vbExclamation,"提示" ComboField2.SetFocus Exit Sub Else If ComboConnect2.Text = "" Then strTxtsql = "select * from student_Info where " & strCf1 & strCop1 & "'" & Trim(TxtLknews1.Text) & "'" & " " & Trim(strCc1) & " " & strCf2 & strCop2 & "'" & Trim(TxtLknews2.Text) & "'" ' 选择了两行, 即有两个条件& " " & Trim(strCc2) & " " & strCf3 & strCop3 & "'" & Trim(TxtLknews3.Text) & "'" Set rst = Executesql(strTxtsql,strMsgtext) Else If ComboField3.Text = "" Or ComboOperator3.Text = "" Or TxtLknews3.Text = "" Then MsgBox "请添加字段","提示" ComboField3.SetFocus Exit Sub Else strTxtsql = "select * from student_Info where " & strCf1 & strCop1 & "'" & Trim(TxtLknews1.Text) & "'" & " " & Trim(strCc1) & " " & strCf2 & strCop2 & "'" & Trim(TxtLknews2.Text) & "'" & " " & Trim(strCc2) & " " & strCf3 & strCop3 & "'" & Trim(TxtLknews3.Text) & "'" '选择了三行, 即有三个条件一起执行 Set rst = Executesql(strTxtsql,strMsgtext) End If End If End If End If '将数据库种的数据写到控件表中 With myflexGrid .Rows = 1 .Clear .CellAlignment = 4 .TextMatrix(0,0) = "卡号" .TextMatrix(0,1) = "学号" .TextMatrix(0,2) = "姓名" .TextMatrix(0,3) = "性别" .TextMatrix(0,4) = "系别" .TextMatrix(0,5) = "年级" .TextMatrix(0,6) = "班级" Do While Not rst.EOF .Rows = .Rows + 1 .CellAlignment = 4 .TextMatrix(.Rows - 1,0) = rst!cardno .TextMatrix(.Rows - 1,1) = rst!studentno .TextMatrix(.Rows - 1,2) = rst!studentname .TextMatrix(.Rows - 1,3) = rst!sex .TextMatrix(.Rows - 1,4) = rst!department .TextMatrix(.Rows - 1,5) = rst!grade .TextMatrix(.Rows - 1,6) = rst!Class rst.MoveNext Loop rst.Close End With End Sub
我不知道如何将它封装成一个函数,如果哪位高手知道,还请指点一下!
原文链接:https://www.f2er.com/vb/258597.html