Private Sub Command5_Click()
        Dim txtSQL As String
        Dim rsSearch As ADODB.Recordset
            
        If Trim(Text4.Text) = "" Then
            sMeg = "学号不能为空"
            MsgBox sMeg, vbOKOnly + vbExclamation, "警告"
            Text4.SetFocus           
           Else
              If Not IsNumeric(Trim(Text4.Text)) Then
                MsgBox "请输入数字!", vbOKOnly + vbExclamation, "警告" 
                Text4.SetFocus         
                addFlag = True
                   txtSQL = "select * from UserMessage where UserID = & Text4.Text & " ' "
                   Text1.Text = rsSearch!UserID
                   Text2.Text = rsSearch!UserName
                   Text3.Text = rsSearch!Password
                   Combo1.Text = rsSearch!UserRole
                End If
         End If         
End Sub
我想问下,这段查询程序错在哪里了,数据库连接在Form_Load中做过,为何它不报错误信息,但在我的Text中缺没有数据显示?????数据库是access做的

解决方案 »

  1.   

    只是有这个:
    txtSQL = "select * from UserMessage where UserID = & Text4.Text & " ' "没有执行查询啊~
    rsSearch.Open txtSQL
      

  2.   

    Private Sub Command5_Click()
            Dim txtSQL As String
            Dim rsSearch As ADODB.Recordset
                
            If Trim(Text4.Text) = "" Then
                sMeg = "学号不能为空"
                MsgBox sMeg, vbOKOnly + vbExclamation, "警告"
                Text4.SetFocus           
               Else
                  If Not IsNumeric(Trim(Text4.Text)) Then
                    MsgBox "请输入数字!", vbOKOnly + vbExclamation, "警告" 
                    Text4.SetFocus         
                    addFlag = True
                       txtSQL = "select * from UserMessage where UserID = & Text4.Text & " ' "
                       rsSearch.Open txtSQL'查询
                       Text1.Text = rsSearch!UserID
                       Text2.Text = rsSearch!UserName
                       Text3.Text = rsSearch!Password
                       Combo1.Text = rsSearch!UserRole
                    End If
             End If         
    End Sub
      

  3.   

    Private Sub Command5_Click()
            Dim txtSQL As String
            Dim rsSearch As ADODB.Recordset
                
            If Trim(Text4.Text) = "" Then
                sMeg = "学号不能为空"
                MsgBox sMeg, vbOKOnly + vbExclamation, "警告"
                Text4.SetFocus           
               Else
                  If Not IsNumeric(Trim(Text4.Text)) Then
                    MsgBox "请输入数字!", vbOKOnly + vbExclamation, "警告" 
                    Text4.SetFocus         
                    addFlag = True
                       txtSQL = "select * from UserMessage where UserID = & Text4.Text & " ' "
                       rsSearch.Open txtSQL'查询
                       '
                       '----
                       if rsSearch.recordcount=0 then msgbox "沒有些學號!":exit sub 
                       Text1.Text = rsSearch!UserID
                       Text2.Text = rsSearch!UserName
                       Text3.Text = rsSearch!Password
                       Combo1.Text = rsSearch!UserRole
                    End If
             End If         
    End Sub
      

  4.   

    如楼上几位所说,你只给出了SQL语句,并没有执行SQL语句啊!~~~~~~~Private Sub Command5_Click()
            Dim txtSQL As String
            Dim rsSearch As ADODB.Recordset
                
            If Trim(Text4.Text) = "" Then
                sMeg = "学号不能为空"
                MsgBox sMeg, vbOKOnly + vbExclamation, "警告"
                Text4.SetFocus           
               Else
                  If Not IsNumeric(Trim(Text4.Text)) Then
                    MsgBox "请输入数字!", vbOKOnly + vbExclamation, "警告" 
                    Text4.SetFocus         
                    addFlag = True
                       txtSQL = "select * from UserMessage where UserID = & Text4.Text & " ' "
                       '======================
                       rsSearch.Open txtSQL,cn
                       '======================
                       Text1.Text = rsSearch!UserID
                       Text2.Text = rsSearch!UserName
                       Text3.Text = rsSearch!Password
                       Combo1.Text = rsSearch!UserRole
                    End If
             End If         
    End Sub
      

  5.   

    你代码中的这句话也不对:txtSQL = "select * from UserMessage where UserID = & Text4.Text & " ' "
    现改成:txtSQL = "select * from UserMessage where UserID ='" & Text4.Text & " ' "
      

  6.   

    如果UserID 是数值类型的,就:
    txtSQL = "select * from UserMessage where UserID = " & Val(Text4.Text) & ""如果UserID 是字符类型的,就:
    txtSQL = "select * from UserMessage where UserID = '" & Text4.Text & " '"
      

  7.   

    正如各位大侠所述
    其中txtSQL = "select * from UserMessage where UserID = & Text4.Text & " ' "改为txtSQL = "select * from UserMessage where UserID ='" & Text4.Text & "'"rsSearch.Open txtSQL, Connection, adOpenDynamic, adLockBatchOptimistic
    注:其中connection为链接字符串
       adOpenDynamic 和 adLockBatchOptimistic为数据库打开模式
      

  8.   

    不對了,你沒有給rsSearch對象分配內存啊,所以不會執行的,應該將
     Dim rsSearch As ADODB.Recordset
     改成
      Dim rsSearch As new ADODB.Recordset
     這樣就可以了!還有你可能要注意那個查詢詩句的寫法,字符型的一定要用引號引起來。
      

  9.   

    Private Sub Command5_Click()
            Dim txtSQL As String
            Dim rsSearch As ADODB.Recordset
                
            If Trim(Text4.Text) = "" Then
                sMeg = "学号不能为空"
                MsgBox sMeg, vbOKOnly + vbExclamation, "警告"
                Text4.SetFocus           
               Else
                  If Not IsNumeric(Trim(Text4.Text)) Then
                    MsgBox "请输入数字!", vbOKOnly + vbExclamation, "警告" 
                    Text4.SetFocus         
                    addFlag = True
                       txtSQL = "select * from UserMessage where UserID = & Text4.Text & " ' "
                       rsSearch.Open txtSQL,,adOpenKeyset,adLockOptimistic '查询
                       Text1= rsSearch.Fields("UserID")
                       Text2= rsSearch.Fields("UserName")
                       Text3= rsSearch.Fields("Password")
                       Combo1= rsSearch.Fields("UserRole")
                    End If
             End If         
    End Sub
    来晚啦,也还是给你顶一下,你这段代码没有什么大问题,最后那个部分除了查询那一句其它的没什么问题,我改成那样只是我的习惯问题,你可以不用改!希望对你有所帮助!
      

  10.   

    ryq0000(ryq) 说的也是问题之一,一定要初始化一下rsSearch。你用了ADO,最好是连接也用ADO这样好统一。方便后期的维护等。