
Text File Save Sub -- Can't call from inside a Select Case
Well, I am going to suggest a different technique, bearing in mind your
recent entry to programming.
Visual Basic forms trigger two events when unloading:
- Form_Unload
- Form_QueryUnload
In the second event procedure VB passes in two parameters, one of which
you can use to cancel the unload - hence the "Yes/No/Cancel" messages
we see all the time. Both events are also triggered when you use the
"Close Box" and the system menu to close a form and also when Windows
itself is closed, so this is bullet-proof.
So: Use your interface elements, e,g, your exit menu, to simply run
the command "Unload Me", and use the QueryUnload event to check whether
data has changed and whether your user wants to save the changes. You
can still call your "save" procedure from there - a separate sub
routine is good planning.
That leaves you with the problem of dealing with the message box. I
hope the example code below will help. I'm assuming VB 5.0 or 6.0
here, BTW.
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim lReturn As VbMsgBoxResult
'just exit and continue with the unload if there
'are no changes - "DataChanged" would normally be
'a form-level boolean variable, set to True when
'changes are made
If Not m_bDataChanged Then Exit Sub
'otherwise, get user input ...
lReturn = MsgBox("Save changes?", vbYesNoCancel)
If lReturn = vbYes Then
'... call your own "save" routine ...
Call MySaveDataRoutine
ElseIf lReturn = vbNo Then
'... do nothing and continue unloading ...
ElseIf lReturn = vbCancel Then
'... or STOP the form unloading, even if the user is
'unloading Windows.
Cancel = True
Else
'there is no "Else" because of "vbYesNoCancel" in
'the MsgBox call - and all posibilities are tested
'above
End If
End Sub
--
Peter
(Please reply to the newsgroup)