AccessRobo
Registered User.
- Local time
- Yesterday, 22:43
- Joined
- Mar 27, 2012
- Messages
- 12
I have very basic skills and am trying to learn VBA. I just added some error handling code to code I took out of my tutorial book. Please would some of the members have a look and tell me if I have made a mistake. I understand you have to close things and then resume them, but I don't really know what my code says. I read, by not closing and destroying objects defensively you can cause problems and waste memory.
My code is:
Private Sub Form_AfterInsert()
On Error GoTo ErrorHandler
Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb()
Set rst = db.OpenRecordset("Product")
MsgBox "Added " & rst.RecordCount & _
"th Product."
rst.Close
Set rst = Nothing
Set db = Nothing
CleanUpAndExit:
Exit Sub
ErrorHandler:
Call MsgBox("An error was encountered" & vbCrLf & vbCrLf & _
"Description: " & Err.Description & vbCrLf & _
"Error Number: " & Err.Number, , "Error")
Resume CleanUpAndExit
End Sub
My code is:
Private Sub Form_AfterInsert()
On Error GoTo ErrorHandler
Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb()
Set rst = db.OpenRecordset("Product")
MsgBox "Added " & rst.RecordCount & _
"th Product."
rst.Close
Set rst = Nothing
Set db = Nothing
CleanUpAndExit:
Exit Sub
ErrorHandler:
Call MsgBox("An error was encountered" & vbCrLf & vbCrLf & _
"Description: " & Err.Description & vbCrLf & _
"Error Number: " & Err.Number, , "Error")
Resume CleanUpAndExit
End Sub