?
Solved

QueryUnload

Posted on 2001-09-12
9
Medium Priority
?
655 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:thatsthefactsjac
  • 6
  • 2
9 Comments
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 6477826
It runs after Cmd_Cancel_click line (End If). So Unload doesnot run because code executing didnot finish in form.

I think so.

suat
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6477833
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!
0
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 6477837
Try using this line of code in QueryUnload

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


suat
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 17

Accepted Solution

by:
Suat Ozgur earned 200 total points
ID: 6477846
setting button value as True instead of calling Click runs nice. But also focus around one line code. As you see there is not End If in my code.

suat
0
 

Author Comment

by:thatsthefactsjac
ID: 6477858
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.
0
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 6477864
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
0
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 6477869
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
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6477872
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 ;-)
0
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 6477880
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
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question