让VB快速读取数据库的函数中,为什么"rs.MoveFirst "会出错啊?
让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 = rs
Set rs = Nothing
Set Conn = Nothing
End Function
[解决办法]
是不是BOF为真啊?
if rs.BOF=false then
rs.Movefirst
End if
[解决办法]
看看返回了什么
另外研究下rs.Open SQL, Conn, 1, 2
1,2是什么意思,这两个参数决定你rs的形式
[解决办法]
Public Function GetDb(ByVal aDbFile As String, ByVal aSql As String) as Recordset
[解决办法]
Set rs = GetDb("set.mdb", "select * from Set_SmtpMail")
if rs.recordcount>0 then'必须进行判断
MsgBox rs("smtpname")
rs.MoveFirst ' 为什么会在这句上出错啊?
MsgBox rs("smtpname
........
end if
[解决办法]
函数中,RS已经被释放掉了,如何RS.MOVEFIRST?看4楼
[解决办法]
Set rs = GetDb("set.mdb", "select * from Set_SmtpMail")
这后面为啥不判断一下有没有数据?
[解决办法]
Set rs = Nothing
Set Conn = Nothing
End Function
已经释放对像 再操作rs 当然出错了
[解决办法]
试试:
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