|
-
Jun 30th, 2006, 08:21 PM
#1
Thread Starter
Member
[RESOLVED] Using msgbox and vbokcancel options
I am looking to display a confirmation ok/cancel when exitin my program. As it is right now, both the OK and CANCEL buttons exit the program. I would like to exit on the OK and keep program running when CANCEL is clicked.(In case the close button was hit by accident). Thanks for any replies.
VB Code:
Private Sub CmdClose_Click()
MsgBox "Are you sure you want to exit?", vbOKCancel
End
End Sub
Bry
EDIT- See Bruces post for sweet/simple code to this question.
Last edited by BryanW; Jun 30th, 2006 at 09:49 PM.
-
Jun 30th, 2006, 08:48 PM
#2
Lively Member
Re: Using msgbox and vbokcancel options
Code:
If Msgbox("ARe you sure you want to quit?",vbyesno+vbcritcal,"Quitting?") = vbNo then
blnCancelled = True
End if
Where blnCancelled is a public variable on your form.
Then on the unload event
Code:
If blnCancelled Then
Cancel = 3
End If
-
Jun 30th, 2006, 09:02 PM
#3
Thread Starter
Member
Re: Using msgbox and vbokcancel options
Thank you for the reply, I am unsure of where to put this code in my program.
VB Code:
If blnCancelled Then
Cancel = 3
End If
Heres what it looks like atm
VB Code:
Private Sub CmdClose_Click()
Dim blnCancelled
If MsgBox("Are you sure you want to quit?", vbYesNo + vbCritical, "Quitting?") = vbNo Then
blnCancelled = True
End If
If blnCancelled Then
Cancel = 3
End If
End Sub
This throws an error saying cancel is not defined. I dont know what you mean(very sorry) when you say "on the unload event". I couldn't find this option. Thank you for your time.
Bry
-
Jun 30th, 2006, 09:26 PM
#4
PowerPoster
Re: Using msgbox and vbokcancel options
VB Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim Answer As Integer
Answer = MsgBox("Exit My Program?", _
vbQuestion + vbYesNo, "Confirm Exit")
If Answer = vbNo Then Cancel = -1
End Sub
Private Sub CmdClose_Click()
Unload Me
End Sub
-
Jun 30th, 2006, 09:32 PM
#5
Thread Starter
Member
Re: Using msgbox and vbokcancel options
Thanks for the replies, here's what I came up with and seems to work.
VB Code:
Private Sub CmdClose_Click()
If MsgBox("Are you sure you want to quit?", vbYesNo + vbQuestion) = vbYes Then
End
Else
End If
End Sub
Is this as just as good as the code above? Or is it a mistake to make it so simple? Thanks.
Bry
-
Jun 30th, 2006, 09:36 PM
#6
Re: Using msgbox and vbokcancel options
1. I modified the response to align with the Question.
Ie Do you... Yes or No
2. Rory, I thinks its better to Unload if the answer is Yes, rather than introduce another Sub (Query_Unload) in this case.
3. Can be done with one line of code.
4. And use VB Constants, such as vbYes or vbCancel opposed to 3 etc.
5. I added a Question mark to the MessageBox as it's asking a Question 
VB Code:
Private Sub CmdClose_Click()
If MsgBox("Are you sure you want to quit?", vbYesNo + vbQuestion, "Confirm") = vbYes Then Unload Me
End Sub
Last edited by Bruce Fox; Jun 30th, 2006 at 09:39 PM.
-
Jun 30th, 2006, 09:36 PM
#7
Re: Using msgbox and vbokcancel options
Looks like we double posted Bryan 
Edit: NEVER use END!
-
Jun 30th, 2006, 09:47 PM
#8
Thread Starter
Member
Re: Using msgbox and vbokcancel options
Thanks Bruce, cookies fer everyone! Can you explain why "unload me" works? is that an internal command? Hopefully you understand the question. And thanks for the never use END tip!
-
Jun 30th, 2006, 09:55 PM
#9
Re: [RESOLVED] Using msgbox and vbokcancel options
Unload is used to release objects, in this case the Form.
'Me' in this context relates to the Form, so Unload Me unloads the Form.
Alernatly, if say you had 3 Forms, then from Form1 you could do Unload Form2 ... for example
It (behind the sceens, amonst other Subs) fires the Query_Unload() event. That is handly
as you can addional stuff like houskeeping if required.
-
Jun 30th, 2006, 10:18 PM
#10
PowerPoster
Re: [RESOLVED] Using msgbox and vbokcancel options
My code is used for Terminating the program (x) and can be used for others...
but yep your's is a one liner version :-)
VB Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim Answer As Integer
Answer = MsgBox("Exit My Program?", _
vbQuestion + vbYesNo, "Confirm Exit")
If Answer = vbNo Then Cancel = -1
End Sub
Private Sub CmdClose_Click()
Unload Me
End Sub
Private Sub Form_Terminate()
Unload Me
End Sub
Last edited by rory; Jun 30th, 2006 at 10:27 PM.
-
Jul 1st, 2006, 01:44 AM
#11
Re: [RESOLVED] Using msgbox and vbokcancel options
Rory, yep, there certainly is a place for Query_Unload code. Amonst others, I mentioned one above.
For what it's worth you can ditch that Variable and do:
VB Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If MsgBox("Exit My Program?", _
vbQuestion + vbYesNo, "Confirm Exit") = vbNo Then Cancel = [b]True[/b]
End Sub
Private Sub CmdClose_Click()
Unload Me
End Sub
Private Sub Form_Terminate()
Unload Me
End Sub
-
Jul 1st, 2006, 01:47 AM
#12
PowerPoster
Re: [RESOLVED] Using msgbox and vbokcancel options
true .. thanks .. yep it followed me from some other site where i found it originally
-
Jul 1st, 2006, 05:41 PM
#13
Re: [RESOLVED] Using msgbox and vbokcancel options
you can get rid of the If too:
VB Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Cancel = If MsgBox("Exit My Program?", vbQuestion + vbYesNo, "Confirm Exit") = vbNo
End Sub
There's also no need for Unload Me in the Form_Terminate event. It's the last event that occurs before a form is destroyed, so it's already well on the way to being unloaded.
-
Jul 1st, 2006, 09:14 PM
#14
PowerPoster
Re: [RESOLVED] Using msgbox and vbokcancel options
thanks bush. ..
minus the If though .. ;-)
Cancel = MsgBox("Exit My Program?", vbQuestion + vbYesNo, "Confirm Exit") = vbNo
-
Jul 2nd, 2006, 09:57 AM
#15
Re: [RESOLVED] Using msgbox and vbokcancel options
yes that's me not checking before posting
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|