Text File Save Sub -- Can't call from inside a Select Case 
Author Message
 Text File Save Sub -- Can't call from inside a Select Case

I have only been programming for three months, so please excuse my
ignorance, but this is one of those "simple" problems that seem more vexing
than advanced techniques.

I created a text browser using the intrinsic text box.  I wanted to pop up a
msgBox when the Close and Exit menu items were selected, allowing the user
to save changes, close the file without saving, or cancel the operation.
Rather than coding the save procedure in each event, I put it in a sub.
Calling the sub from the mnuItemSave event works fine.  In the menuItemClose
and mnuItemExit events I wanted to use a Select Case (using the msgBoxResult
as the case expression, eg.

 I can't call the sub.  I get a compile error telling me that a statement is
expected.

What am I missing here?  Is there a statement syntax for calling the sub?
Is it bad practice to do so in this manner?  What if I define a CTextToSave
class (with properties for a wrap string and filename etc.) and include a
SaveText method, (properly instancing an object and so on).  Will I get a
similar situation?

I understand that there are better strategies using other tools, such as the
rich text box and/or scriping DLL file system object, but I am trying to
learn the fundamentals.



Sun, 16 Sep 2001 03:00:00 GMT  
 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)



Sun, 16 Sep 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. VBScript Select Case vs VB Select Case

2. VBA sub won't call a VB sub

3. Problem with 'Select Case'

4. HOWTO: Call JavaScript function from inside a VBScript Sub

5. Calling a MS-DOS Command inside a Sub

6. calling a sub inside a usercontrol from a module

7. calling a sub inside a usercontrol from a module

8. calling a sub inside a usercontrol from a module

9. Call a function within Select case statment

10. 'Canned' data in VB program

11. Local function/sub inside function/sub

12. "select...case", multiples cases not possible ?

 

 
Powered by phpBB® Forum Software