Results 1 to 15 of 15

Thread: [RESOLVED] Using msgbox and vbokcancel options

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    36

    Resolved [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:
    1. Private Sub CmdClose_Click()
    2.     MsgBox "Are you sure you want to exit?", vbOKCancel
    3. End
    4. 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.

  2. #2
    Lively Member
    Join Date
    Feb 2005
    Location
    California
    Posts
    97

    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

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    36

    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:
    1. If blnCancelled Then
    2.        Cancel = 3
    3.     End If


    Heres what it looks like atm

    VB Code:
    1. Private Sub CmdClose_Click()
    2.  
    3. Dim blnCancelled
    4.     If MsgBox("Are you sure you want to quit?", vbYesNo + vbCritical, "Quitting?") = vbNo Then
    5.           blnCancelled = True
    6.     End If
    7.  
    8.     If blnCancelled Then
    9.        Cancel = 3
    10.     End If
    11. 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

  4. #4
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Using msgbox and vbokcancel options

    VB Code:
    1. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2.     Dim Answer As Integer
    3.     Answer = MsgBox("Exit My Program?", _
    4.     vbQuestion + vbYesNo, "Confirm Exit")
    5.     If Answer = vbNo Then Cancel = -1
    6. End Sub
    7.  
    8. Private Sub CmdClose_Click()
    9.     Unload Me
    10. End Sub

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    36

    Re: Using msgbox and vbokcancel options

    Thanks for the replies, here's what I came up with and seems to work.

    VB Code:
    1. Private Sub CmdClose_Click()
    2.     If MsgBox("Are you sure you want to quit?", vbYesNo + vbQuestion) = vbYes Then
    3.         End
    4.     Else
    5.        
    6.     End If
    7.  
    8.  
    9. End Sub

    Is this as just as good as the code above? Or is it a mistake to make it so simple? Thanks.

    Bry

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    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:
    1. Private Sub CmdClose_Click()
    2.     If MsgBox("Are you sure you want to quit?", vbYesNo + vbQuestion, "Confirm") = vbYes Then Unload Me
    3. End Sub
    Last edited by Bruce Fox; Jun 30th, 2006 at 09:39 PM.

  7. #7
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Using msgbox and vbokcancel options

    Looks like we double posted Bryan

    Edit: NEVER use END!

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    36

    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!

  9. #9
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    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.

  10. #10
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    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:
    1. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2.     Dim Answer As Integer
    3.     Answer = MsgBox("Exit My Program?", _
    4.     vbQuestion + vbYesNo, "Confirm Exit")
    5.     If Answer = vbNo Then Cancel = -1
    6. End Sub
    7.  
    8. Private Sub CmdClose_Click()
    9.     Unload Me
    10. End Sub
    11.  
    12. Private Sub Form_Terminate()
    13.     Unload Me
    14. End Sub
    Last edited by rory; Jun 30th, 2006 at 10:27 PM.

  11. #11
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    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:
    1. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2.  
    3.     If MsgBox("Exit My Program?", _
    4.     vbQuestion + vbYesNo, "Confirm Exit") = vbNo Then Cancel = [b]True[/b]
    5.  
    6. End Sub
    7.  
    8. Private Sub CmdClose_Click()
    9.     Unload Me
    10. End Sub
    11.  
    12. Private Sub Form_Terminate()
    13.     Unload Me
    14. End Sub

  12. #12
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: [RESOLVED] Using msgbox and vbokcancel options

    true .. thanks .. yep it followed me from some other site where i found it originally

  13. #13
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: [RESOLVED] Using msgbox and vbokcancel options

    you can get rid of the If too:
    VB Code:
    1. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2.     Cancel = If MsgBox("Exit My Program?", vbQuestion + vbYesNo, "Confirm Exit") = vbNo
    3. 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.

  14. #14
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: [RESOLVED] Using msgbox and vbokcancel options

    thanks bush. ..
    minus the If though .. ;-)

    Cancel = MsgBox("Exit My Program?", vbQuestion + vbYesNo, "Confirm Exit") = vbNo

  15. #15
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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
  •  



Click Here to Expand Forum to Full Width