bradyboyy88
Well-known Member
- Joined
- Feb 25, 2015
- Messages
- 562
I was wondering how do you actually transfer one disconnected recordset to another so that you can close the original. I have a function below that passes the disconnected recordset as a function but if i were to put oRs.close which is the original recordset than the variable I just passed to is closed also. So how do I truly copy one recordset to a new variable so I can get rid of the first one which is used in my function. Also, if I do not close the original recordset object isnt it still in memory hence a memory leak?
Code:
Option Explicit
'Database connection variables
Public oConn As Object
Public oRs As Object
Public sConn As String
Public Sub SQLOpenDatabaseConnection(StrDBPath As String, EngineType As Integer)
'Define Connection String by inputting StrDBPath into a larger string (Works for Excel DB)
'Define Connection String by inputting StrDBPath into a larger string
'Access Support for engine type
If EngineType = 0 Then
sConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & StrDBPath & ";" & _
"Jet OLEDB:Engine Type=5;" & _
"Persist Security Info=False;Mode=Share Exclusive;"
'Excel Support for engine type
ElseIf EngineType = 1 Then
sConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & StrDBPath & "';" & _
"Extended Properties=""Excel 12.0;HDR=YES;ReadOnly=0;"";"
End If
'Create Connection
Set oConn = CreateObject("ADODB.Connection")
oConn.Mode = 3
oConn.CursorLocation = adUseClient
'Create RecordSet
Set oRs = CreateObject("ADODB.Recordset")
oRs.LockType = adLockPessimistic
RetryConnection:
DoEvents
On Error GoTo ErrorHandler
'Connect to the database
20 oConn.Open sConn
On Error GoTo 0
Exit Sub
ErrorHandler:
'triggered by connection error. Most likely locking type
'MsgBox "looks like we had a connection error. The error number is " & Err.Number & " and the description is " & Err.Description & " on line " & Erl
Err.Clear
Resume RetryConnection
End Sub'Return a query as a recordsetPublic Function SQLQueryDatabaseRecordset(SQLQuery As String) As Variant 'Open Record Set by executing SQL oRs.Open SQLQuery, oConn 'Disconnect the recordset Set oRs.ActiveConnection = Nothing 'Return recordset Set SQLQueryDatabaseRecordset = oRs End FunctionPublic Sub SQLCloseDatabaseConnection() 'Close Connection oConn.Close 'Clear Memory Set oConn = Nothing Set oRs = Nothing
Last edited: