Cancel open on error in function call

meboz

Registered User.
Local time
Tomorrow, 10:22
Joined
Aug 16, 2004
Messages
71
Hi all,

I am opening a form via a button using the usual docmd.open "myform"

In the form_open event, I am making a function call, and in a given circumstance, a handled error occurs.

how do I set cancel = true when the error occurs in the called function?

Thanks in advance
 
Put this in the forms OnOpen event...
Code:
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Form_Open_Err
        
    MsgBox "your code here"
    
Form_Open_Exit:
    Exit Sub
    
Form_Open_Err:
    If Err.Number = 12345 Then
        DoCmd.CancelEvent
    Else
        MsgBox Err.Number & " - " & Err.Description
        Resume Form_Open_Exit
    End If
    
End Sub
You have to trap for the specific error number. I just used 12345 as an example.
 
That makes sense ghudson.

The things is that the error is actually generated in a function that is called by the Form_open event, and that is the error number that needs to be in place of 12345

Any thoughts?
 
Meboz,

Are you saying that the error is both generated in and handled by the called function, thus the form_open event has no notification of the error ?

If that is the case, the called function needs to have a way of notifying the calling function that an error (or only the explicitly required error) occurred.

Perhaps if the error handler in the called function detects the explicit error, either have the function return an explicit (otherwise impossible) value, or have the error handler raise another error (err.raise), which would be passed back up the call stack to the open_form event, n'est pas ?

Regards

John
 
Last edited:
What's the function that you are calling? Post the code.
 
You need to ensure that all of your Functions() and Subs() have error trapping. Then you can determine where you need to trap for a specific error.
 
I'm going to suggest passing a boolean variable to the function (by reference) and, if there is an error, set it to True. If it comes back true you can manage your error handling in thecallin function.
 

Users who are viewing this thread

Back
Top Bottom