Snowflake68
Registered User.
- Local time
- Today, 21:57
- Joined
- May 28, 2014
- Messages
- 464
I am new at error handling and am trying to incorporate a little into my code. The issue I have is that I need to stop code running if there is an error in either of the two subroutines after they are called.
I am trapping errors in the two sub routines and all is working perfectly there but how do I stop the code from continuing when an error is dealt with smoothly.
I have the some very basic error handling on the code below and further error handling on the two subroutines that this code calls.
This is a sample of the error handling code I have on the two sub routines.
I am trapping errors in the two sub routines and all is working perfectly there but how do I stop the code from continuing when an error is dealt with smoothly.
I have the some very basic error handling on the code below and further error handling on the two subroutines that this code calls.
Code:
Private Sub cmdPrintReport_Click()
On Error GoTo ErrorHandling
Dim strCheckNoOfRecords As Variant
Dim strPrint As Boolean
strCheckNoOfRecords = DLookup("[RowID]", "[DSPNote]")
strPrint = DLookup("[Print]", "LookupEmailAddress", "ID = 1")
If strCheckNoOfRecords > 0 Then
If strPrint = True Then
DoCmd.RunMacro "PrintReport"
End If
' I want to stop the code running if I trap an error in either of these two subroutine
Call CreatePDF
Call SendEmailPDF
' I want to stop the code below from running if I get an error in the above routines.
DoCmd.RunMacro "DSPStatusChange"
'Reset RowID in DSP Notes Table
'' DoCmd.OpenQuery "DSPDeleteLines" ' no longer required since introducing the 'Reset' module code
DoCmd.Close
DoCmd.OpenForm "frmReset", , , , , acHidden
Else
MsgBox "There are no DSP Notes to Print." & Chr(13) & Chr(10) & Chr(10) & "Please enter at least 1 DSP Note and try again.", vbInformation, "Error No Data"
End If
ErrorHandling:
MsgBox Err.Description & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "Something went wrong. Please contact your system administrator.", vbCritical, "Error #" & Err.Number
Exit Sub
End Sub
This is a sample of the error handling code I have on the two sub routines.
Code:
On Error GoTo ErrorHandling
'then all my code here
ErrorHandling:
If Err.Number = 2501 Then
MsgBox Err.Description & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "Please check the directory " & strPath & "\" & strDIR & " exists and then try again", vbCritical, "Error #" & Err.Number
Else: MsgBox Err.Description & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "Something went wrong. Please contact your system administrator.", vbCritical, "Error #" & Err.Number
End If
Exit Sub
Last edited: