I'm using MS Access 2003.
I have the following code behind a command button:
In the Access documentation for the Close command, I see the following two statements:
Two (perhaps dumb) Questions:
1. Since I exit the subroutine in my CleanUpAndExit section, do I really need to explicitly close the database? Should I also set it to Nothing? I see lots of preaching online about the importance of Closing these objects or setting them to Nothing so as to free up resources, but these Help statements seem to say it's unnecessary.
2. If I needed to call multiple functions that run commands against the database and no transaction processing was required, is it better to declare the database and workspace in the called functions or to do that at the highest level and pass those objects into the functions, as I have done here? (Just wondering if there's much of a performance hit for doing that multiple times in the called code versus just once at the highest level.)
Thanks for any clarification you can give.
Wayne
I have the following code behind a command button:
Code:
Private Sub cmdRevalidate_Click()
On Error GoTo ErrorHandler
Dim liaArchiveCount As Long
Dim myWorkSpace As DAO.Workspace
Dim myDatabase As DAO.Database
Set myWorkSpace = DBEngine.Workspaces(0)
Set myDatabase = CurrentDb
liaArchiveCount = ValidateAndArchiveLiaDetails(myWorkSpace, myDatabase)
MsgBox "LIA records promoted to Archive: " & liaArchiveCount, , "Logistics"
CleanUpAndExit:
myDatabase.Close
Exit Sub
ErrorHandler:
Dim strErrorMessage As String
strErrorMessage = "Error Number: " & Err.Number & vbCrLf & _
"Description: " & Err.Description & vbCrLf & _
"Source: " & Err.Source & " in frmLiaErrorCorrection.cmdRevalidate_Click()"
MsgBox strErrorMessage, , "Error"
Resume CleanUpAndExit
End Sub
In the Access documentation for the Close command, I see the following two statements:
Caution If you exit a procedure that declares Connection, Database, or Recordset objects, those objects are closed, all pending transactions are rolled back, and any pending edits to your data are lost.
...
An alternative to the Close method is to set the value of an object variable to Nothing (Set dbsTemp = Nothing).
Two (perhaps dumb) Questions:
1. Since I exit the subroutine in my CleanUpAndExit section, do I really need to explicitly close the database? Should I also set it to Nothing? I see lots of preaching online about the importance of Closing these objects or setting them to Nothing so as to free up resources, but these Help statements seem to say it's unnecessary.
2. If I needed to call multiple functions that run commands against the database and no transaction processing was required, is it better to declare the database and workspace in the called functions or to do that at the highest level and pass those objects into the functions, as I have done here? (Just wondering if there's much of a performance hit for doing that multiple times in the called code versus just once at the highest level.)
Thanks for any clarification you can give.
Wayne