P
Paul Johnson
I am looking for answers to four related questions here.
In almost every code example I come across in books or in online tutorials,
a Recordset object is opened like this:
Private Sub Example()
Dim db As Database
Dim rst As Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("SomeTable", dbOpenDynaset)
[do actions here]
Set rst = Nothing
Set db = Nothing
End Sub
I prefer getting the same results with the following compact code:
Private Sub Example()
With CurrentDb.OpenRecordset("SomeTable", dbOpenDynaset)
[do actions here]
.Close
End With
End Sub
Question 1: In Example 1, are the objects set as dimensioned variables only
to make the code more understandable to novices, or in a multi-developer
workplace, or is there a compelling reason that's beyond my limited
computer-science background?
Question 2: Is there any harm in creating the recordset object directly
from the CurrentDb object, as in Example 2?
Question 3: Is it really necessary to set these objects to Nothing, as in
Example 1, or close the recordset in Example 2? I thought that the scope of
procedure variables and objects (at least in these Private Subs) is limited
to the current procedure, and that they vanish when the procedure is exited.
I keep hearing that this cleanup is good practice, but what consequence are
we avoiding?
Question 4: Since I'm compiling and creating an MDE file for distribution,
I'm not concerned about the readability of the code in the end. Also, I'm
the only developer likely to work on this application, and I am accustomed
to my own habits. If I can shrink my code throughout like the above example
shows, does it make any difference once Access compiles it, or does the
compiler turn both types of code into the same set of instructions anyway
(like the way it ignores white space and comments)?
I have not been diligent in closing my recordsets in the past, and I have
not noticed any problems coming from it, but I have been curious if that is
what contributes to the increasing size of the mdb file. I'm guessing there
must be pieces of code or table data getting tagged to the end of the file
and not cleared from the buffer until the Compact/Repair command is
executed. I recently stepped through my code and closed all open objects
and set dimensioned object variables to Nothing before exiting subs, and the
file growth continues as before. It's not a problem, as the Compact command
keeps it in check (I have the "Compact on Exit" option set), but it made me
wonder about the above items.
Thanks in advance for any input.
Paul Johnson
In almost every code example I come across in books or in online tutorials,
a Recordset object is opened like this:
Private Sub Example()
Dim db As Database
Dim rst As Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("SomeTable", dbOpenDynaset)
[do actions here]
Set rst = Nothing
Set db = Nothing
End Sub
I prefer getting the same results with the following compact code:
Private Sub Example()
With CurrentDb.OpenRecordset("SomeTable", dbOpenDynaset)
[do actions here]
.Close
End With
End Sub
Question 1: In Example 1, are the objects set as dimensioned variables only
to make the code more understandable to novices, or in a multi-developer
workplace, or is there a compelling reason that's beyond my limited
computer-science background?
Question 2: Is there any harm in creating the recordset object directly
from the CurrentDb object, as in Example 2?
Question 3: Is it really necessary to set these objects to Nothing, as in
Example 1, or close the recordset in Example 2? I thought that the scope of
procedure variables and objects (at least in these Private Subs) is limited
to the current procedure, and that they vanish when the procedure is exited.
I keep hearing that this cleanup is good practice, but what consequence are
we avoiding?
Question 4: Since I'm compiling and creating an MDE file for distribution,
I'm not concerned about the readability of the code in the end. Also, I'm
the only developer likely to work on this application, and I am accustomed
to my own habits. If I can shrink my code throughout like the above example
shows, does it make any difference once Access compiles it, or does the
compiler turn both types of code into the same set of instructions anyway
(like the way it ignores white space and comments)?
I have not been diligent in closing my recordsets in the past, and I have
not noticed any problems coming from it, but I have been curious if that is
what contributes to the increasing size of the mdb file. I'm guessing there
must be pieces of code or table data getting tagged to the end of the file
and not cleared from the buffer until the Compact/Repair command is
executed. I recently stepped through my code and closed all open objects
and set dimensioned object variables to Nothing before exiting subs, and the
file growth continues as before. It's not a problem, as the Compact command
keeps it in check (I have the "Compact on Exit" option set), but it made me
wonder about the above items.
Thanks in advance for any input.
Paul Johnson