H
huyeote
Hi there,
I'm trying to code up a function in ACCESS 2000 VBA to return a
recordset object to a sub. However, I couldn't make the recordset
returned as the recordset object I got inside the function disappered
as the ADODB connection was closed at the end of the function. I tried
to assign the recordset to a public recordset variable and this method
had the same result. So can anyone tell me how to pass a recordset
object to another sub?
Thanks,
Huyeote
My codes are like:
'---------------------------codes start here
-------------------------------------------------------
Public rstAcctNo_temp as ADODB.Recordset
Function GetRecordset(strSourceFile as String) AS ADODB.Recordset
Dim rstConn As ADODB.Connection
Dim strSQL As String
Dim strConn As String 'for access connection string
strConn = "Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=" & strSourceFile & ";"
strSQL = "SELECT DISTINCT A.AccountNo, A.Supplier, A.Customer,
A.BillingType " _
& "FROM table1 AS A"
Set rstConn = New ADODB.Connection
'Open connection to source DB
rstConn.Open ConnectionString:=strConn
If rstConn.State = adStateOpen Then
'assign data to public variable
Set rstAcctNo_temp = rstConn.Execute(strSQL)
Set Get_rstAcctNo = rstAcctNo_temp
End If
rstConn.Close
End Function
Sub Collating()
Dim rstAcctNo As ADODB.Recordset
................
Set rstAcctNo = Get_rstAcctNo(filePath) 'where rstAcctNo is
nothing after the connection inside the function is closed.
...................
End Sub
I'm trying to code up a function in ACCESS 2000 VBA to return a
recordset object to a sub. However, I couldn't make the recordset
returned as the recordset object I got inside the function disappered
as the ADODB connection was closed at the end of the function. I tried
to assign the recordset to a public recordset variable and this method
had the same result. So can anyone tell me how to pass a recordset
object to another sub?
Thanks,
Huyeote
My codes are like:
'---------------------------codes start here
-------------------------------------------------------
Public rstAcctNo_temp as ADODB.Recordset
Function GetRecordset(strSourceFile as String) AS ADODB.Recordset
Dim rstConn As ADODB.Connection
Dim strSQL As String
Dim strConn As String 'for access connection string
strConn = "Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=" & strSourceFile & ";"
strSQL = "SELECT DISTINCT A.AccountNo, A.Supplier, A.Customer,
A.BillingType " _
& "FROM table1 AS A"
Set rstConn = New ADODB.Connection
'Open connection to source DB
rstConn.Open ConnectionString:=strConn
If rstConn.State = adStateOpen Then
'assign data to public variable
Set rstAcctNo_temp = rstConn.Execute(strSQL)
Set Get_rstAcctNo = rstAcctNo_temp
End If
rstConn.Close
End Function
Sub Collating()
Dim rstAcctNo As ADODB.Recordset
................
Set rstAcctNo = Get_rstAcctNo(filePath) 'where rstAcctNo is
nothing after the connection inside the function is closed.
...................
End Sub