• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

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.
0
compuzak1
Asked:
compuzak1
  • 5
  • 4
1 Solution
 
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now