Link to home
Start Free TrialLog in
Avatar of cswebdev
cswebdev

asked on

Capture a button onclick event from field onexit event?

I have a form that has several fields that are required to be filled, if certain other fields have values.  I trigger a message box at the onexit event of these required fields, if the first field has a value and the required field doesn't contain any values.  My problem is that if the user partially fills out the form, gets to one of these required fields and decides to bail out of the form (close), the onexit event of the required field is triggered before the onclick event of the close button and my popup message is displayed before the form closes.  This isn't fatal, but it is annoying.  Is there a way to capture the onclick event of the close button at the onexit event of the field so I can bypass the message box?  I hope I have explained this clearly.
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi cswebdev,

The answer to your question is NO.
The alternative approach is not to use the Control events but to use the Form_Beforeupdate event procedure to do all of the tests and report the problems there.
(Using
Cancel = true
in the beforeupdate procedure stops the record being saved)

Pete
In your button's OnClick event, have you tried including "Me.Undo" to cancel any changes to the current record?  
Here is one approach that might work for you:

In the form (at the top), declare a Boolean flag:

    Private fCancel As Boolean

In the Form_Load event, set the flag to false:

    fCancel = False

In the button OnClick event, set the flag to true:

    fCancel = True

In the OnExit events, check the flag:

    If Not fCancel Then
        ' check for required fields
    End If

I hope that will work for you.

Chuck
Avatar of cswebdev
cswebdev

ASKER

Pete and routinet, thank you for your responses.  Chuck, your solution works perfectly!  Thank you.
Ooops.........Sorry Chuck, it didn't work the second time I tried it.  Apparently the first time I tried your code, I had already tried to close the form, canceled the action and the flag was set to true.  So, then my onexit routine worked.  However, since the flag doesn't get set to true until the button onclick event, at the onexit event the flag is false and I still get the message box.  I need to try something else.
OK, I see how that would work. Here is what I would do:

1. Move all of the required field checks from the OnExit events to the Close button OnClick event.

If Not IsNull(txtBox1) And Len(Trim(txtBox1)) > 0 And (IsNull(txtBox2) Or Len(Trim(txtBox2)) = 0) Then
    MsgBox "You must fill in txtBox2"
    txtBox2.SetFocus
    Exit Sub
End If
'  repeat for every required field

2. Create a Cancel button that closes the form but does not run the required field checks.

I realize that this does not work exactly as it did before but I think it will provide a more pleasing user interface.

Regards,

Chuck
Thank you Chuck.  That is what I am going to have to do.  I just can't check for required fill at each field.  It will have to be a seperate sub at some point.  The form is being used to build a query to produce a report and my checks will have to be attached to my "Show Report" button instead of as the fields are filled.  I had hoped I could force the user to fill all required fields before trying to build the report, but I guess not.
ASKER CERTIFIED SOLUTION
Avatar of Chuck Wood
Chuck Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think I will try your last suggestion.  I really want the user to have all the required fields filled before trying to submit for report.  I will let you know what happens.  Thanks.
Chuck, your last suggestion seems to have done the trick.  I created a sub to validate/check all required fields.  That sub is invoked whenever I need to check for an entry and so far my testing seems to indicate it is working as needed.  I thank you very much for your help.
You are welcome.

Good luck on your project.

Chuck