Link to home
Start Free TrialLog in
Avatar of thatsthefactsjac
thatsthefactsjac

asked on

QueryUnload

I have a form with the close button and a cancel button.  when the close button is clicked, i want the cmdCancel_Click event to fire.  The event fires, but the form does not unload.  Why?

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  If Not UnloadMode = vbFormCode Then
    Cancel = True
    cmdCancel_Click
  End If
End Sub

Private Sub cmdCancel_Click()
  Unload Me
End Sub
Avatar of Suat M. Ozgur
Suat M. Ozgur
Flag of United States of America image

It runs after Cmd_Cancel_click line (End If). So Unload doesnot run because code executing didnot finish in form.

I think so.

suat
Avatar of JonFish85
JonFish85

to fix that problem, try this:

Private Sub Command1_Click()
  MsgBox "Bye!"
  Unload Me
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  If UnloadMode <> vbFormCode Then
    Command1_Click
  End If
End Sub


it worked for me... hope this helps!
Try using this line of code in QueryUnload

 If Not UnloadMode = vbFormCode Then Cancel = True And Me.cmd_cancel.Value = True


suat
ASKER CERTIFIED SOLUTION
Avatar of Suat M. Ozgur
Suat M. Ozgur
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
Avatar of thatsthefactsjac

ASKER

Thanks again smozgur.  I had a feeling that it had to do with the 'end if' after the unload, but had no idea how to get both commands on one line.
Hi JonFish85, your code runs because Cancel=True line doesnot exist, not because of the MsgBox add. (I think so)

So problem is Cancel=True and thatsthefactsjac, why do you need to set Cancel as True there?

Anyway if you dont need it just delete that line and use your code. If you need it then my code would work above.

Regards
Suat
look for And operator in If..Then...Else statement.

Thanks for grading a comment fast and avoid keeping questions open.

with my best regards
suat
nice work smozgur! I've never sen "And" used that way before... btw, I had just added the Messagebox as some code to throw in there, cause I couldnt think of anything else ;-)
Thanks, actually i tried semicolon first but it didnot work for this because : also means there is an End If at the end of the If.

like this

 If Not UnloadMode = vbFormCode Then: Cancel = True: cmd_cancel_Click

I often use this way in immediate window for Loops or in If...Then...Else statement.

suat