Creating a sub to close recordsets elsewhere in the database
Hello,
I am aware that in Access VBA, it is important to close any objects that you open, like DAO databases, recordsets, etc.
What I wanted to know is, are their any implications of creating a sub and calling this everytime I wanted to close a recordset. For example:
================================================== =====
Public Sub DoSomething()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("SELECT * FROM ATable;")
' etc
' etc
' etc
Call subCloseObject(, rst)
Call subCloseObject(dbs)
End Sub
================================================== =====
Public Sub subCloseObject(Optional ByRef dbs As DAO.Database, Optional ByRef rst As DAO.Recordset)
10 On Error GoTo ErrorCode
100 If Not dbs Is Nothing Then
110 Call dbs.Close
120 Set dbs = Nothing
130 End If
200 If Not rst Is Nothing Then
210 Call rst.Close
220 Set rst = Nothing
230 End If
60000 ExitCode:
60010 Call DoCmd.SetWarnings(True)
60020 Exit Sub
65000 ErrorCode:
65010 Call subErrorNotifier(Err.Number, "{1AC8A26B-57BB-495C-BE83-025664F55551}", Erl)
65020 Resume ExitCode
================================================== =====
Since I have stated ByRef I don't think there are any issues other than this close procedure creating a "Nothing" database/recordset if none is stated. However, if an object is "Nothing" then do I need to worry about it?
Still learning VBA so any help would be greatly appreciated, many thanks.
|