On exit problem when close button or red x is pressed

I have an unbound combo box on a form.  I validate the field 'on exit' to ensure it is not null.  If it is null I display an error message and then return to the field.  This works well, until the close button or red x is entered.  When either of these events occur, I want to skip the error checking and close the form.

Here is an example of the code I would like:

Private Sub unbPaymentType_Exit(Cancel As Integer)

    '  don't error check if close button or red x is pressed
    if CloseButtonPressed = True or RedXPressed = True then
          exit sub
    end if

    '  if payment type is blank, can't exit
    If Nz(Me.unbPaymentType, "") <= "" Then
        MsgBox "You Must Select a Payment Type", vbOKOnly, "Invalid Payment Type"
        Me.unbPaymentType.SetFocus
        Cancel = 1
    End If
       
End Sub

of course the first if statement is not valid syntax, but you probably understand what I am after.

Seems simple, but I can't find any way to determine what caused the 'on exit' of my field.

Any help would be greatly appreciated.
compuzak1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

compuzak1Author Commented:
I found a work-around by setting the default value to one of the selections.  This works ok, but I would prefer to have the unbound box clear when the form is opened.  The question is still fully open.
0
rockiroadsCommented:
Dont know about capturing that keystroke so why not have a button say "Cancel Edit" or some text and this then is the only graceful way to leave your form without error checking?

0
rockiroadsCommented:
If it hits unbPaymentType_Exit when you hit this new button, you can always use a variable. Set this to false on form_load then set it to true if that btn hit. Then in your exit button, do validation only if that variable is false
0
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

compuzak1Author Commented:
The error check occurs as soon as you click on anything else (due to 'on exit' routine for the field I am validating).  Therefore, there is no way to do anything unless we can determine that the close button or the red x has been pressed.  If they move to another control on the form, I need the error check.  I only want to bypass the error check if the close button is pressed or the red x is pressed.
0
rockiroadsCommented:
Thats what Im saying. I don't think you can trap that so create your own "exit" button on the form.
0
compuzak1Author Commented:
I am confused.  I have my own close button and the red x is active.  The problem I have is that the program starts with focus on a field called unbPaymentType.  It has an 'on exit' validation that checks for null.  If unbPaymentType is null, the error message displays PRIOR to the close button event or the red x event.  I can't get out of the unbPaymentType field without an error message (if the unbPaymentType is null).  I want to bypass the error message if the close button on my form is clicked or the red-x is clicked.

Isn't there some way to determine what caused the unbPaymentType exit?  Something like "screen.activecontrol" but for the next control that will get focus (something ilike screen.nextcontrol).
0
rockiroadsCommented:
Ok, what Im saying is have a flag in your form

eg - defined on top of form

    dim cancelForm as boolean

and ensure you initialise it when form loads

    private sub form_load
        cancelForm = false


Now in your close btn, say its called cmdCancel (ignore red x), you set it

    private sub cmdCancel_Click()
        cancelForm = true
        'rest of your close/cancel code


Now in exit btn you check for this flag

Private Sub unbPaymentType_Exit(Cancel As Integer)

    '  don't error check if close button or red x is pressed
    if cancelForm = false then
        '  if payment type is blank, can't exit
        If Nz(Me.unbPaymentType, "") <= "" Then
            MsgBox "You Must Select a Payment Type", vbOKOnly, "Invalid Payment Type"
             Me.unbPaymentType.SetFocus
            Cancel = 1
        End If
    end if
       
End Sub


0
compuzak1Author Commented:
The unbPaymentType_Exit event occurs before the cmbCancel event (at least on my form), so everytime I exit the unbPaymentType field it gives the error message.  Also, I don't like to turn off the red x, many of my users hate it being turned off and complain about it being off.

0
rockiroadsCommented:
Why not then move then validation to form_unload instead of unbPaymentType_Exit. Same thing, set the Cancel field if it fails validation.
In conjunction with code here, use the BeforeUpdate function as well for your validation so it is performed when changes made as well as exiting the form

I am not saying get rid of the red x. Leave it still there but dont treat it as cancel. Force your cmdCancel to cancel the form only
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.