I often see code examples, that go through the laborious and possibly confusing process, of declaring, setting, calling, and any associated cleanup, of Object Variables, that otherwise, work fine without the variable - least of all, on a variable that is Private to that function.
Is this at all really necessary, when writing out the full reference, does just as well?
I've heard arguments, that the code is easier to read, and runs faster. The former is highly subjective, and the latter, I have yet to really notice.
Examples;
Public Sub TransactionExample()
Dim wrkSpaceVar As DAO.Workspace
Dim dbVar As DAO.Database
Set wrkSpaceVar = DBEngine(0)
Set dbVar = CurrentDb
On Error GoTo trans_Err
wrkSpaceVar.BeginTrans
dbVar.Execute "SomeActionQuery", dbFailOnError
dbVar.Execute "SomeOtherActionQuery", dbFailOnError
wrkSpaceVar.CommitTrans
trans_Exit:
wrkSpaceVar.Close
Set dbVar = Nothing
Set wrkSpaceVar = Nothing
Exit Sub
trans_Err:
wrkSpaceVar.Rollback
MsgBox "Transaction failed. Error: " & Err.Description
Resume trans_Exit
End Sub
vs
Public Sub TransactionExample()
On Error GoTo trans_Err
DAO.DBEngine.BeginTrans
CurrentDb.Execute "SomeActionQuery", dbFailOnError
CurrentDb.Execute "SomeOtherActionQuery", dbFailOnError
DAO.DBEngine.CommitTrans
Exit Sub
trans_Err:
DAO.DBEngine.Rollback
MsgBox "Transaction failed. Error: " & Err.Description
End Sub
I am not asking about setting variables to "Nothing"; I am asking if they are necessary at all. And for what it's worth, necessary, within the scope, of the examples provided.