solocommand
asked on
messageboxes / breaking out of subs - two part question
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!
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!
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
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
ASKER
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
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
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
thanks appari, Ive been writing so much boolean logic today I forgot the damn thing returns 'yes' or 'no'! :)
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
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
ASKER
mccainz2: what about if it was vbyesnocancel, and they chose cancel; how would i stop the form_unload event?
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Solocommand,
Is there anything else you need to know?
Appari has given you excellent info and code.
Please respond.
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
Computer101
E-E Admin
ASKER
actually, no. you guys answers my questions; i'm going to split points between you two. Thank you so much for your help!
ASKER
ah
thanks
appari, these are yours, mccainz2: points in VB topic for you
thanks
appari, these are yours, mccainz2: points in VB topic for you
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?