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
请各位大哥解读。

解决方案 »

  1.   

    set mrc=new adodb.recordset
    在 Set mrc = ExecuteSQL(txtSQL, MsgText) 前面加或者 
    dim mrc as new adodb.recordset
      

  2.   

    首先你少了一个函数
    Public Function ConnectString() As String
        ConnectString = "dsn=MDGDYJBH;uid=sa;pwd="
    End Function
    把它放到模块中
    同时你在odbc数据源的文件dsn要和你的数据库连接上
      

  3.   

    dim mrc as adodb.recordset
    set mrc = new adodb.recordset
    加上这个就可以了
      

  4.   

    “对象变量或With块变量未设置”,是因为你的数据库没有连接上:有两种可能。1、是数据对象定义有误。2、是sql语句有误。 你程序的公共模块中的函数没有毛病,txtSQL = "select * from user_Info where user_ID =" & txtUserName.Text & "",上面这条语句有错误:改为: txtSQL = "select * from user_Info where user_ID = '" & UserName & "'" 基本上没问题了,还有cmd_click()事件中的代码要写全。愿你学有所成!
      

  5.   

    在出错后那边加上   end if!!!
      

  6.   

    上面各位大哥所说的,我都试过了,可是还是不行,如果我在开始的时候这样写 dim mrc as new adodb.recordset的话就会出现“对象已经关闭,不允许操作”。如果我写成set mrc=new adodb.recordset的话,还是出现最初的错误。救命呀!!!连接是没有问题的。不缺少ConnectString语句。
      

  7.   

    你是否在工程中引用了,“Microsoft ActiveX Data objects 2.6librarv”