IF Statement on form close. trying to exit sub without form closing if condition met.

the-m0th

Registered User.
Local time
Today, 23:29
Joined
Apr 14, 2015
Messages
51
Hi Everyone.

i've been asked to update our call answering screen at work. there are a couple of combo boxes that need to be filled in for every call that our operators sometimes miss. i've tried to rectify this by putting an if statement in the form close event like this.

Code:
    If IsNull(cbo_actiontaken) Then
        MsgBox "What Action Was Taken As A Result Of This Call?"
        Exit Sub
    End If
    
    If IsNull(cbo_operator) Then
        MsgBox "Enter Your Initials. They Should Have Been Input On The Front Screen!"
        Exit Sub
    End If

my problem now is that when you click okay on the first message box, the form closes anyway. is there something i can put in there to stop it closing? i've read about e.cancel = true but i get an error when i put that line of code in. any help will be greatly appreciated.

thanks
Wayne
 
Put a variable in the form (the very top of the form events code block)
Private bCanClose as Boolean

this will allow close if true.

In form_close event ,ask the questions, then
bCanClose= true

In form_unload, cancel = not bCanClose
 
hi, thanks for replying.

i've tried the code you posted but now when i click on close nothing happens. any ideas?

thanks again.
Wayne
 
Form_Close is too late for this...the closing cannot be canceled; you have to use the Form_BeforeUpdate event, which can be
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

 If IsNull(cbo_actiontaken) Then
        MsgBox "What Action Was Taken As A Result Of This Call?"
        Cancel = True
        cbo_actiontaken.SetFocus
        cbo_actiontaken.DropDown  
        Exit Sub
    End If
    
    If IsNull(cbo_operator) Then
        MsgBox "Enter Your Initials. They Should Have Been Input On The Front Screen!"
        Cancel = True
        cbo_operator.SetFocus
        cbo_operator.DropDown  
        Exit Sub
    End If

End Sub
Linq ;0)>
 
The before update event; why didn't I think of that? :rolleyes:
 
hi,

i've put that code in the beforeupdate event but it doesn't trigger when the cross is clicked to close the form. i'm struggling to understand how putting the code there will allow it to run when i try and close the form. sorry, i'm still pretty new to all of this.

thanks
Wayne
 
I would probably hide the cross so the user had to use your buttons.
 

i've put that code in the beforeupdate event but it doesn't trigger when the cross is clicked to close the form.
That really doesn't make any sense! If the Record is Dirty and you Close the Form, regardless of how you close it, the Form_BeforeUpdate event is going to fire.

Are you sure you put the code in the BeforeUpdate event of the Form?
 
i got it working, i put it in the form unload event and it's working like a dream. thanks for all your help guys.
 

Users who are viewing this thread

Back
Top Bottom