messageboxes / breaking out of subs -  two part question

solocommand
solocommand used Ask the Experts™
on
My question has two parts:

First of all, is ther a way to stop the program from ending depending on the status of a variable, e.g; if TextChanged = True then sto from exiting and show a messagebox asking if you want to save.

Second, how do you save what the user chose from a messagebox, like if they clicked no instead of yes, how do you tell your program that they chose no, and execute a command depending on what button they pressed?

Thanks for any help you can give!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
For your second part...

Private Sub Command1_Click()

   b_Save = MsgBox("Save?", vbYesNo)
   If b_Save Then
      'save file
    End If
End Sub


not clear on your first part, under what conditions are you trying to stop the exit?




Commented:
in form_queryunload event check for the condition then set cancel=true

eg
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If Text1 <> "" Then
    MsgBox "text1 is not null save it"
    Cancel = True
End If
End Sub


if msgbox("Select your OPtion",vbyesno)=vbno then
 msgbox "clicked No"
else
 msgbox "clicked yes"
endif

Author

Commented:
mccainz2: on the first part, i meant like in any other text-editing application, where if you make changes and try to exit, it pops up a msgbox saying that the document is not saved. im looking for a way to recreate that in my application. appari: thanks; mccainz2: i think that will help
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
mccainz2:

b_Save = MsgBox("Save?", vbYesNo)
  If b_Save Then
     'save file
   End If
it always goes inside if even irrespective of what we select yes or no. have to change like
b_Save = MsgBox("Save?", vbYesNo)
  If b_Save =vbyes Then
     'save file clicked Yes
  else
     'dont save file clicked No
   End If

Commented:
thanks appari, Ive been writing so much boolean logic today I forgot the damn thing returns 'yes' or 'no'! :)

Commented:
Heres a quick example of what you want on the first part.

Dim b_isSaved As Boolean
Private Sub Command1_Click()
        'save your work
        b_isSaved = True
End Sub


Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

    If Not b_isSaved Then
        If MsgBox("Your latest work is not saved!" & vbCrLf & " Save it now?", vbYesNo) = vbNo Then
            Unload Me
        Else
           'Save work here
           Unload Me
        End If
    End If

End Sub

Private Sub Text1_Change()
    b_isSaved = False
End Sub

Author

Commented:
mccainz2: what about if it was vbyesnocancel, and they chose cancel; how would i stop the form_unload event?

Commented:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
dim intResponse as integer
   If Not b_isSaved Then
      intResponse = MsgBox("Your latest work is not saved!" & vbCrLf & " Save it now?", vbYesNocancel)
       Select case intResponse
       case VByes
               '**** Clicke Yes
       case vbno
               '**** Clicke No      
       case vbcancel
               '**** Clicke Cancel      
       end select
   End If

End Sub
Commented:
here is complete list of constants related to Msgbox.
copied from MSDN

MsgBox Constants
The following constants are used with the MsgBox function to identify what buttons and icons appear on a message box and which button is the default. In addition, the modality of the MsgBox can be specified. Since these constants are built into VBScript, you don't have to define them before using them. Use them anywhere in your code to represent the values shown for each.

Constant Value Description
vbOKOnly 0 Display OK button only.
vbOKCancel 1 Display OK and Cancel buttons.
vbAbortRetryIgnore 2 Display Abort, Retry, and Ignore buttons.
vbYesNoCancel 3 Display Yes, No, and Cancel buttons.
vbYesNo 4 Display Yes and No buttons.
vbRetryCancel 5 Display Retry and Cancel buttons.
vbCritical 16 Display Critical Message icon.
vbQuestion 32 Display Warning Query icon.
vbExclamation 48 Display Warning Message icon.
vbInformation 64 Display Information Message icon.
vbDefaultButton1 0 First button is the default.
vbDefaultButton2 256 Second button is the default.
vbDefaultButton3 512 Third button is the default.
vbDefaultButton4 768 Fourth button is the default.
vbApplicationModal 0 Application modal. The user must respond to the message box before continuing work in the current application.
vbSystemModal 4096 System modal. On Win16 systems, all applications are suspended until the user responds to the message box. On Win32 systems, this constant provides an application modal message box that always remains on top of any other programs you may have running.  


The following constants are used with the MsgBox function to identify which button a user has selected. These constants are only available when your project has an explicit reference to the appropriate type library containing these constant definitions. For VBScript, you must explicitly declare these constants in your code.

Constant Value Description
vbOK 1 OK button was clicked.
vbCancel 2 Cancel button was clicked.
vbAbort 3 Abort button was clicked.
vbRetry 4 Retry button was clicked.
vbIgnore 5 Ignore button was clicked.
vbYes 6 Yes button was clicked.
vbNo 7 No button was clicked.

Commented:
Solocommand,
Is there  anything else you need to know?
Appari has given you excellent info and code.
Please respond.
Points reduced for a split.  You can now accept one experts comment as an answer.  After that, make another question in this topic area for the other expert.  Make the question for the amount of points intended.  Label the question, "Question for (Expert Name) and post this question number in the base of the question, i.e. For your assistance in question # --------


Computer101
E-E Admin

Author

Commented:
actually, no. you guys answers my questions; i'm going to split points between you two. Thank you so much for your help!

Author

Commented:
ah
thanks

appari, these are yours, mccainz2: points in VB topic for you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial