PGAC
Programmer
- Sep 19, 2011
- 20
Dear all,
As part of Access (2003) VBA (Win XP), I am aware it is good practice to close any objects to explicitly open in any sub/function so that these objects don’t end up “floating around” in memory.
The current way I do this is:
==============================================
Public Sub Example1
Dim rstA As DAO.Recordset
Dim rstB As DAO.Recordset
etc
etc
etc
ExitCode:
If Not rstA Is Nothing Then
Call rstA.Close
Set rstA = Nothing
If Not rstB Is Nothing Then
Call rstB.Close
Set rstB = Nothing
End If
End Sub
==============================================
What I wanted to know if there were any cons to the below example (which has been edited to the 'bare bones') and whether it is acceptable or not:
==============================================
Public Sub Example2
Dim rstA As DAO.Recordset
Dim rstB As DAO.Recordset
etc
etc
etc
ExitCode:
Call CloseDAORecordset(rstA)
Call CloseDAORecordset(rstB)
End If
----------------------------------------------
Public Sub CloseDAORecordset(ByRef rst As DAO.Recordset)
If Not rst Is Nothing Then
Call rst.Close
Set rst = Nothing
End If
End Sub
==============================================
The reason for my asking this comes from the concept of re-usability of code. Likewise, by declaring the recordset variable as ByRef, it is referring to the object in memory, not a copy of (like ByVal). This also makes codes in other functions/subs tidier as I only have to use one line of code per recordset to ensure the object is cleared from memory which is very useful as I have a sub that needs 6 DAO.recordsets to carry out the job at hand.
I have tried the above and it does set the recordsets to nothing, which is what I would expect from a ByVal declaration (and trying to view records after the external sub has set them to nothing produces an error which is what would be expected since it is an attempt to access something that effectively no longer exists).
The point is – is this good practice or bad? I’d appreciate any criticisms, pros and cons. Many thanks!
As part of Access (2003) VBA (Win XP), I am aware it is good practice to close any objects to explicitly open in any sub/function so that these objects don’t end up “floating around” in memory.
The current way I do this is:
==============================================
Public Sub Example1
Dim rstA As DAO.Recordset
Dim rstB As DAO.Recordset
etc
etc
etc
ExitCode:
If Not rstA Is Nothing Then
Call rstA.Close
Set rstA = Nothing
If Not rstB Is Nothing Then
Call rstB.Close
Set rstB = Nothing
End If
End Sub
==============================================
What I wanted to know if there were any cons to the below example (which has been edited to the 'bare bones') and whether it is acceptable or not:
==============================================
Public Sub Example2
Dim rstA As DAO.Recordset
Dim rstB As DAO.Recordset
etc
etc
etc
ExitCode:
Call CloseDAORecordset(rstA)
Call CloseDAORecordset(rstB)
End If
----------------------------------------------
Public Sub CloseDAORecordset(ByRef rst As DAO.Recordset)
If Not rst Is Nothing Then
Call rst.Close
Set rst = Nothing
End If
End Sub
==============================================
The reason for my asking this comes from the concept of re-usability of code. Likewise, by declaring the recordset variable as ByRef, it is referring to the object in memory, not a copy of (like ByVal). This also makes codes in other functions/subs tidier as I only have to use one line of code per recordset to ensure the object is cleared from memory which is very useful as I have a sub that needs 6 DAO.recordsets to carry out the job at hand.
I have tried the above and it does set the recordsets to nothing, which is what I would expect from a ByVal declaration (and trying to view records after the external sub has set them to nothing produces an error which is what would be expected since it is an attempt to access something that effectively no longer exists).
The point is – is this good practice or bad? I’d appreciate any criticisms, pros and cons. Many thanks!