Private Sub cmdOK_Click()
    Dim txtSQL As String
    Dim mrc As ADODB.Recordset
    Dim MsgText As String
    
    UserName = ""
    If Trim(txtUserName.Text = "") Then
       MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
       txtUserName.SetFocus
    Else
       txtSQL = "select * from user_Info where user_ID =" & txtUserName.Text & ""
       Set mrc = ExecuteSQL(txtSQL, MsgText)
       
       If mrc.EOF = True Then  ?????当程序运行到这里就说“对象变量或With块变量未设置”。Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.Recordset
  Dim cnn As ADODB.Connection
  Dim rst As ADODB.Recordset
  Dim sTokens() As String
  On Error GoTo ExecuteSQL_Error
  sTokens = Split(SQL)
  Set cnn = New ADODB.Connection
  cnn.Open ConnectString
  If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) Then
    cnn.Execute SQL
    MsgString = sTokens(0) & "query successful"
  Else
    Set rst = New ADODB.Recordset
    rst.Open Trim$(SQL), cnn, adOpenKeyset, adLockOptimistic
    Set ExecuteSQL = rst
    MsgString = "查询到" & rst.RecordCount & "条纪录"
  End If
ExecuteSQL_Exit:
  Set rst = Nothing
  Set cnn = Nothing
  Exit Function
ExecuteSQL_Error:
  MsgString = "查询错误:" & Err.Description
  Resume ExecuteSQL_Exit
End Function
请各位大哥解读。上面各位大哥所说的,我都试过了,可是还是不行,如果我在开始的时候这样写 dim mrc as new adodb.recordset的话就会出现“对象已经关闭,不允许操作”。如果我写成set mrc=new adodb.recordset的话,还是出现最初的错误。救命呀!!!连接是没有问题的。不缺少ConnectString语句。
工程中引用了,“Microsoft ActiveX Data objects 2.6librarv”

解决方案 »

  1.   

    txtSQL = "select * from user_Info where user_ID =" & txtUserName.Text & ""语句有错误,试一下:txtSQL = "select * from user_Info where user_ID ='" & txtUserName.Text & "'"
      

  2.   

    很显然,程序中是有几出漏洞的:
    Private Sub cmdOK_Click()
        Dim txtSQL As String
        Dim rs As NEW ADODB.Recordset
        Dim MsgText As String
        UserName = ""    
        If Trim(txtUserName.Text = "") Then
           MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
           txtUserName.SetFocus
           EXIT Sub                '次出加这个是非常有必要的
        Else
           txtSQL = "select * from user_Info where user_ID ='" & txtUserName.Text & "'"                          '应该在双引号之前和之后加单引号,否则必错!!!!!
           Set mrc = ExecuteSQL(txtSQL, MsgText)
           If mrc.EOF = True Then  ?????当程序运行到这里就说“对象变量或With块变量未设置”。
    '''其实主要是你的对象变量没有设置,程序写的很乱,把你的意图告诉我,我给你程序!
    Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.Recordset
      Dim cnn As ADODB.Connection
      Dim rst As ADODB.Recordset
      Dim sTokens() As String
      On Error GoTo ExecuteSQL_Error
      sTokens = Split(SQL)
      Set cnn = New ADODB.Connection
      cnn.Open ConnectString
      If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) Then
        cnn.Execute SQL
        MsgString = sTokens(0) & "query successful"
      Else
        Set rst = New ADODB.Recordset
        rst.Open Trim$(SQL), cnn, adOpenKeyset, adLockOptimistic
        Set ExecuteSQL = rst
        MsgString = "查询到" & rst.RecordCount & "条纪录"
      End If
    ExecuteSQL_Exit:
      Set rst = Nothing
      Set cnn = Nothing
      Exit Function
    ExecuteSQL_Error:
      MsgString = "查询错误:" & Err.Description
      Resume ExecuteSQL_Exit
    End Function
    E-mail:[email protected]
      

  3.   

    Dim mrc As ADODB.Recordset
    改成:Dim mrc As new ADODB.Recordset(如果有错,换个名字看看)txtSQL = "select * from user_Info where user_ID =" & txtUserName.Text & ""
    改成
    txtSQL = "select * from user_Info where user_ID ='" & txtUserName.Text & "'"