让VB快速读取数据库的函数中,为什么"rs.MoveFirst "会出错啊?
Set rs = GetDb("set.mdb", "select * from Set_SmtpMail")
MsgBox rs("smtpname")
rs.MoveFirst   ' 为什么会在这句上出错啊?
MsgBox rs("smtpname")
Public Function GetDb(ByVal aDbFile As String, ByVal aSql As String)
Dim Conn, SQL
Dim ConnectionString As String
Set Conn = CreateObject("ADODB.Connection") '连接数据库
Conn.Open "provider=Microsoft.Jet.oledb.4.0;" & "data source=" & aDbFile
Dim rs As New ADODB.Recordset
SQL = aSql
rs.Open SQL, Conn, 1, 2
GetDb = rsSet rs = Nothing
Set Conn = Nothing
End Function

解决方案 »

  1.   

    是不是BOF为真啊?
    if rs.BOF=false then
    rs.Movefirst
    End if
      

  2.   

    看看返回了什么另外研究下rs.Open SQL, Conn, 1, 21,2是什么意思,这两个参数决定你rs的形式
      

  3.   

    Public Function GetDb(ByVal aDbFile As String, ByVal aSql As String) as Recordset
      

  4.   

    Set rs = GetDb("set.mdb", "select * from Set_SmtpMail")
    if rs.recordcount>0 then'必须进行判断
    MsgBox rs("smtpname")
    rs.MoveFirst ' 为什么会在这句上出错啊?
    MsgBox rs("smtpname
    ........
    end if
      

  5.   

    函数中,RS已经被释放掉了,如何RS.MOVEFIRST?看4楼
      

  6.   

    rs.Open SQL, Conn, 1, 2
    研究一下1代表啥意思,2代表啥意思?
      

  7.   


    1,2这个问题不大,一般在客户端游标时,1,2可能不支持,但是ado会自动修改,使用最合适的参数代替之
      

  8.   

    Set rs = GetDb("set.mdb", "select * from Set_SmtpMail")这后面为啥不判断一下有没有数据?
      

  9.   

    Set rs = Nothing
    Set Conn = Nothing
    End Function已经释放对像 再操作rs 当然出错了
      

  10.   

    试试:Public Function GetDb(ByVal aDbFile As String, ByVal aSql As String)
    Dim Conn, SQL
    Dim ConnectionString As String
    Set Conn = CreateObject("ADODB.Connection") '连接数据库
    Conn.Open "provider=Microsoft.Jet.oledb.4.0;" & "data source=" & aDbFile
    Dim rs As New ADODB.Recordset
    SQL = aSql
    rs.Open SQL, Conn, 1, 2
    Set GetDb = rs.Clone(adLockReadOnly)Set rs = Nothing
    Set Conn = Nothing
    End Function