Error Handling Code

freightguy

Registered User.
Local time
Today, 08:54
Joined
Mar 1, 2016
Messages
36
Hi all - I am having a difficult time locating my problem. I have an error handling code in the "On Exit" event of a form's button. This event runs through a recordset then compiles and sends an email. On the same command button I have an "On Click" event that controls the data input for about a dozen fields (data validation).

When the user populates the form's fields the On Click event will run it's course and find anything blank, pop a message and set focus to blank field. No problem. Problem arises when user leaves the recordset blank. The message pops up (error 3021) however I can't figure out how to set focus to the specific field in the recordset or even set focus to any field on the form.

The application keeps shutting down. I want the program to go back to a field on the form so user has a chance of completing his form.


Code:
ErrorHandler:
    Dim iAnswer As Integer
    
    
    Select Case Err.Number
        Case 0
            Resume
        Case 20
            MsgBox "error code 20"
            Exit Sub
        Case 3021 ' recordset is null (no record found)
            MsgBox "There are no Shipping Details to report. Your request is incomplete. Please continue to add shipping details or click on the UNDO button to clear all and start over."
            ' ????? - needs to set focus somewhere on the form (example I have a field called "ProjectNumber"
        Case 3265
            MsgBox "error code 3265"
            Exit Sub
        Case Else
            MsgBox "Please contact XXX in XXXX at rayxxx@xxxxltd.com with the error code information. Unexpected error occured. " & Err.Number & " " & Err.Description
            Exit Sub
     End Select

If there is anything else required please let me know.
thank you
 
Using the Exit event on a command button to do something seems odd to me. I'd put all the code in the Click event, both data validation and then if data OK, generate your email or whatever.

Post the code where the error is occurring and indicate which line is causing the error. The error handling code doesn't help.
 
I think I had it that way at one time but I will try what you suggest. Failing that I will come back and provide you with rest of the code.

thank you for pointing that out.
 
I have an error handling code in the "On Exit" event of a form's button.
Running relevant logic on the exit-event of a button very unexpected behavior. You should reconsider that.


Furthermore, your error handling code contains logic that should be incorporated into the main code logic of the procedure (e.g. checking if there was data in the recordset).
 

Users who are viewing this thread

Back
Top Bottom