-
Ok I've made this work before but can't get it to do it this time. When the user hits X on the form the user is aske if he/she wants to save changes yes and no work fine but the form still unloads if you hit cancel...
code:
Private Sub Form_Unload(Cancel As Integer)
If NoteDirty Then
Dim z
z = MsgBox("Would you like to save changes?", vbYesNoCancel, "Save changes?")
If z = 6 Then
mnuFileSave_Click
ElseIf z = 2 Then
Exit Sub
Else
'oh well
End If
End If
End Sub
Its a replacement for notepad since mine is screwed for some reason....
Thankz
-
try
Code:
msgbox "need to save it ?", vboksavecancel,"save"
if vbok = true then
savefile
elseif
exit
else
end if
off the top of my head , so if it dont work sorry
-
Cancel = 1
You gotta set the Cancel variable to make sure the program understands you're canceling the unload.
like this
Code:
Private Sub Form_Unload(Cancel As Integer)
If NoteDirty Then
Dim z
z = MsgBox("Would you like to save changes?", vbYesNoCancel, "Save changes?")
If z = 6 Then
mnuFileSave_Click
ElseIf z = 2 Then
Exit Sub
Else
'oh well
Cancel = 1 ' <---------
End If
End If
End Sub
-
Right Idea, just the wrong event. Use the queryunload event of the form.
Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim lResult As Long
If UnloadMode = vbFormControlMenu Then
lResult = MsgBox("Would you like to save changes?", vbYesNoCancel, "Save changes?")
Select Case lResult
Case vbYes
'Do save here
Case vbNo
'Just let it exit anyway
Case vbCancel
'Dont close
Cancel = True
End Select
End If
End Sub
-
THankz Guys
But why is it better to use Form_QuerryUnload?
-
With the query unload event you can tell how your program is being unloaded.
-
True, but with the sample code ("If UnloadMode = vbFormControlMenu Then") you only ask to save changes when the user presses the X-button, but when windooz stops (or the program is closed thru the taskmanager) it won't be asked, so in this case it doesn't really matter whether to use the query_unload or the unload event, you always want to save changes.