这个是我数据源的模块代码,数据源连接正常Public fMainForm As frmMain
Public UserName As String
Public gintMode As Integer
Public flagEdit As BooleanSub Main()
    Dim fLogin As New frmLogin
    fLogin.Show vbModal
    If Not fLogin.OK Then
        'Login Failed so exit app
        End
    End If
    Unload fLogin
    Set fMainForm = New frmMain
    fMainForm.Show
End Sub
Public Function ConnectString() _
   As String
'returns a DB ConnectString
   ConnectString = "DSN=ecardinfo.dsn;UID=sa;PWD=admin"
End Function
Public Function ExecuteSQL(ByVal sql _
   As String, msgstring As String) _
   As ADODB.Recordset
'executes SQL and returns 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
      'rst.MoveLast     'get RecordCount
      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 FunctionPublic Function Testtxt(txt As String) As Boolean
    If Trim(txt) = "" Then
        Testtxt = False
    Else
        Testtxt = True
    End If
End FunctionPublic Sub EnterToTab(Keyasc As Integer)
    If Keyasc = 13 Then
        SendKeys "{TAB}"
    End If
End Sub
----------
这个是程序代码执行时提示错误
If mrc.EOF = True Then出现“实时错误‘91’对象变量或WITH 块变量未设置”
程序代码
--------
Option Explicit
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpbuffer As String, nSize As Long) As Long
Public OK As Boolean
'记录确定次数
Dim miCount As Integer
Private Sub Form_Load()
    Dim sBuffer As String
    Dim lSize As Long
    sBuffer = Space$(255)
    lSize = Len(sBuffer)
    Call GetUserName(sBuffer, lSize)
    If lSize > 0 Then
        txtUserName.Text = ""
   
    Else
        txtUserName.Text = vbNullString
    End If
    
    OK = False
    miCount = 0
End SubPrivate Sub cmdCancel_Click()
    OK = False
    Me.Hide
End Sub
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
            MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
            txtUserName.SetFocus
        Else
            If Trim(mrc.Fields(1)) = Trim(txtPassword.Text) Then
                OK = True
                mrc.Close
                Me.Hide
                UserName = Trim(txtUserName.Text)
            Else
                MsgBox "输入密码不正确,请重新输入!", vbOKOnly + vbExclamation, "警告"
                txtPassword.SetFocus
                txtPassword.Text = ""
            End If
        End If
    End If
    
    miCount = miCount + 1
    If miCount = 3 Then
        Me.Hide
    End If
    Exit Sub
End Sub
源代码下载地址http://fswcn.com/一卡通.rar谁能帮我解决下错误

解决方案 »

  1.   

    这个是程序代码执行时提示错误 
    If mrc.EOF = True Then出现“实时错误‘91’对象变量或WITH 块变量未设置” 
    程序代码 这个代码不在任何过程,函数,属性里
      

  2.   

    Public Function ExecuteSQL(ByVal sql _ 
      As String, msgstring As String) _ 
      As ADODB.Recordset 
    'executes SQL and returns 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 
          'rst.MoveLast    'get RecordCount 
          Set ExecuteSQL = rst 
          msgstring = "查询到" & rst.RecordCount & _ 
            " 条记录 " 
      End If 
    ExecuteSQL_Exit: 
      Set rst = Nothing 
      Set cnn = Nothing 
      Exit Function 
    有问题,为什么你用一个private的connection,还在最后加入Set cnn = Nothing,这样相当于连接已经断开,那后面访问记录集肯定不行啊1
      

  3.   

    Dim mrc As ADODB.Recordset 
    声明了啊Set cnn = Nothing
    私有的
    那该怎么声明修改呢请教下
      

  4.   

    已经解决了哈是个专家告诉我的
    Public Function ExecuteSQL(ByVal sql _
       As String, msgstring As String) _
       As ADODB.Recordset
    'executes SQL and returns 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
          'rst.MoveLast     'get RecordCount
          Set ExecuteSQL = rst
          msgstring = "查询到" & rst.RecordCount & _
             " 条记录 "
       End If'加入此句
       Exit Function  ExecuteSQL_Exit:
       Set rst = Nothing
       Set cnn = Nothing
       Exit Function
       
    ExecuteSQL_Error:
       msgstring = "查询错误: " & _
          Err.Description
       Resume ExecuteSQL_Exit
    End Function