在敲组合查询的时候,我遇到了很多问题,比如说查询的语法怎么连接啊,怎么让控件中的文本信息也就是说字段名,组合关系对应数据库表中的字段哪?
也就是说我们要让卡号=Cardno,让姓名=StudentName ,这样我们在查询的时候才方便了,反成不能直接给Combox的text里面上英文的吧? 于是就定义个函数,让它实现这个功能,函数如下。
<span style="font-size:18px;">Public Function Field(i As String) As String Select Case i Case "卡号" Field = "cardno" Case "姓名" Field = "studentname" Case "上机日期" Field = "ondate" Case "上机时间" Field = "ontime" Case "下机日期" ....... End select End Function </span>这样就行了。下面这组合查语法以及代码
<span style="font-size:18px;">Private Sub cmdInqurie_Click() Dim ctrl As Control Dim mrc As ADODB.Recordset Dim txtSQL As String Dim Msgtext As String '检查条件输入 If Trim(cmbfeild1.Text) = "" Or Trim(cmboperator1.Text) = "" Or Trim(txt1.Text) = "" Then MsgBox "请输入完整的查询条件", , "提示" Exit Sub End If Dim i, iCols As Integer '让所有列都居中显示文字 iCols = MSFlexGrid1.Cols For i = 0 To iCols - 1 MSFlexGrid1.ColAlignment(i) = flexAlignCenterCenter Next txtSQL = "select * from line_info where " txtSQL = txtSQL & Trim(Field(cmbfeild1.Text)) & Trim((cmboperator1.Text)) & "'" & Trim(txt1.Text) & "'" If Trim(cmbRelation1.Text <> "") Then '第一个组合关系存在 If Trim(cmbfeild2.Text) = "" Or Trim(cmboperator2.Text = "") Or Trim(txt2.Text = "") Then MsgBox "你已经选择了第一个组合关系,请输入第二行查询条件", , "提示" Exit Sub Else txtSQL = txtSQL & Field(Trim(cmbRelation1.Text)) & " " & Field(cmbfeild2.Text) & cmboperator2.Text & "'" & Trim(txt2.Text) & "'" End If End If If Trim(cmbRelation2.Text <> "") Then '第二个组合关系存在 If Trim(cmbfeild3.Text) = "" Or Trim(cmboperator3.Text) = "" Or Trim(txt3.Text) = "" Then MsgBox "你已经选择了第二个组合关系,请输入第三行查询条件", , "提示" Exit Sub Else txtSQL = txtSQL & Field(cmbRelation2.Text) & " " & Field(cmbfeild3.Text) & cmboperator3.Text & "'" & Trim(txt3.Text) & "'" End If End If On Error GoTo error1 '错误语句保护,当用户输入查询的格式不对时给出提示信息。 Set mrc = ExecuteSQL(txtSQL, Msgtext) If mrc.EOF = True Then '检查信息是否存在,如果不存在给出提示并清空所有文本框 MsgBox "没有查询到结果,可能会你输入的信息不存在,或者信息矛盾" For Each ctrl In Me.Controls If TypeOf ctrl Is TextBox Then '是否为文本框TextBox ctrl.Text = "" '清空所有文本框 End If Next For Each ctrl In Me.Controls If TypeOf ctrl Is ComboBox Then '是否为文本框TextBox ctrl.Text = "" End If Next Exit Sub End If With MSFlexGrid1 .Rows = 1 .TextMatrix(0, 0) = "卡号" .TextMatrix(0, 1) = "姓名" .TextMatrix(0, 2) = "上机日期" ........ Do While Not mrc.EOF .Rows = .Rows + 1 .TextMatrix(.Rows - 1, 0) = Trim(mrc!cardno) .TextMatrix(.Rows - 1, 1) = mrc!studentname .TextMatrix(.Rows - 1, 2) = mrc!ondate .TextMatrix(.Rows - 1, 3) = mrc!OnTime ........ mrc.MoveNext Loop End With mrc.Close error1: MsgBox "你输入的查询信息格式有误,请按标准格式输入。" End Sub</span>
原文地址:http://blog.csdn.net/zhoukun1008/article/details/38516523