Okay, I'm writing an inventory program that uses a DSN in order to access a MSAccess database.
Before, whenever I needed a recordset, I'd declare all the variables, open the db, open the record, etc in the sub itself.
I wanted to change it so I could just call a function and get the requested recordset. However, now that I've done that, I keep getting an error saying that the dataset is closed.
Here's what I've done (only showing relevant code):
Before, whenever I needed a recordset, I'd declare all the variables, open the db, open the record, etc in the sub itself.
I wanted to change it so I could just call a function and get the requested recordset. However, now that I've done that, I keep getting an error saying that the dataset is closed.
Here's what I've done (only showing relevant code):
Code:
Public rsTemp As New ADODB.Recordset
Public dbTemp As New ADODB.Connection
Public Sub Startup() 'run at startup
dbTemp.Open "dsn=" & DSN
End Sub
Public Function GetRecords(DSN As String, sql As String) As ADODB.Recordset
'ignore the DSN parameter
Dim grRS As New ADODB.Recordset
'Open the recordset
grRS.Open sql, dbTemp, adOpenDynamic, adLockOptimistic
'send it back to the sub that asked for it
Set GetRecords = grRS
'Close the recordset
grRS.Close
End Function
Public Sub PROBLEMSUB()
'Getting recordset and putting into rsTemp
Set rsTemp = GetRecords(DSN, "SELECT Value From Environment Where (Setting='CompanyId')")
'Retrieve my data
'THIS IS WHERE THE PROBLEM IS. VB is telling me that this recordset is closed! What am I doing wrong?
WarehouseId = rsTemp("Value")
End Sub