View Full Version : Solved: Error trapping for disk drives

09-20-2004, 03:02 AM
Im having a small problem when trying to backup my data to a floppy, I have an error trapping routine that only works once. The code is below;

Private Sub cmdQuit_Click()
On Error GoTo Err_Disk
Dim answer As String


answer = MsgBox("Your data is about to be backed up, Please insert a disk into drive A: and click OK to continue with the backup or cancel to exit the database", vbOKCancel, "Backup Data")
If answer = vbOK Then
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "tbltransaction", "A:\Transactiondata.xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "tblclients", "A:\Clientdata.xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "tblresourcetype", "A:\ResourceTypedata.xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "tbltransaction", "A:\Childdata.xls"
MsgBox "Backup is complete. Click OK"
GoTo Exit_cmdQuit_Click
End If


MsgBox "Please insert a disk in drive A:, if you continue to get this message try inserting another disk", , "Disk Error"
GoTo Main
End If

Exit Sub

End Sub

The code works fine the first time you dont insert a disk but when you do it the second time I get a Runtime Error 3436 saying unable to create file. It seems to me that when I access the drive without the disk in it the first time the error event occurs but when you do it again it doesnt?

09-20-2004, 12:03 PM
This is to do with Active and Enabled Error Handlers.

An Error Handler becomes Enabled following an appropriate On Error statement.
An Enabled Error Handler bcomes Active following an error.

An Active Error Handler remains Active until DeActivated (or the end of the Routine).
An Active Error Handler is DeActivated by a Resume statement.

All Error Handling (in the current Routine) is Disabled while an Error Handler is Active.
When an error occurs and there isn't an Enabled Error Handler in the current Routine, the error is passed back to the caller for it to handle according to the same rules.

In your code you Enable your Error Handler at the beginning and, the first time an error occurs it becomes Active.
You never DeActivate it so the next time an error occurs it is passed to the Caller (Access) for it to handle, which it does by issuing the Runtime Error Message you see.
To DeActivate (and hence ReEnable) it you should use a Resume Statement. Instead of coding GoTo Main, you should code Resume Main.

I note that you have an extra End If in your code, but perhaps you have just posted an extract from the whole thing.

Anne Troy
09-28-2004, 10:40 PM
Globetrot: Any luck with your questions here?

06-15-2005, 09:44 PM
Thanks people, the problem is solved...