Results 1 to 10 of 10

Thread: [RESOLVED] Error when I hit cancel...how do I handle this?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    148

    Resolved [RESOLVED] Error when I hit cancel...how do I handle this?

    When I open a file to load into a listbox, if I hit cancel, I get an error. What's the best way to handle this error?

  2. #2

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    148

    Re: Error when I hit cancel...how do I handle this?

    VB Code:
    1. Private Sub mnuOpen_Click()
    2. On Error GoTo EH
    3.  
    4. List1.Clear
    5. Me.CommonDialog1.InitDir = App.Path
    6. Me.CommonDialog1.Filter = "MyLivePromoter Files (*.jmg)|*.jmg"
    7. Me.CommonDialog1.ShowOpen
    8.     If Me.CommonDialog1.FileName & "" <> "" Then
    9.         ' check that file exist
    10.         Set fs2 = New FileSystemObject '
    11.  
    12.         If fs2.FileExists(CommonDialog1.FileName) Then
    13.             Me.txtFile.Text = CommonDialog1.FileName
    14.               Label3.Caption = txtFile.Text
    15.            
    16.         Else
    17.             MsgBox CommonDialog1.FileName & " doesn't exist", vbCritical, "File not found"
    18.         End If
    19.     End If
    20.    
    21.     Set fs2 = New FileSystemObject
    22. Set ts2 = fs2.OpenTextFile(Me.txtFile.Text, ForReading, False)
    23.     While Not ts2.AtEndOfStream
    24.    
    25. Me.List1.AddItem (ts2.ReadLine)
    26.  
    27. Wend
    28.  
    29. ts2.Close
    30. Set ts2 = Nothing
    31.  
    32. EH:
    33. Call MsgBox("We have encountered an error and must shut down!", vbCritical Or vbSystemModal, App.Title)
    34. Unload Me
    35. End Sub

    Now, obviously I don't want the program to shut down, but that message box looks better than a run time error

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Error when I hit cancel...how do I handle this?

    Use CancelError like this
    VB Code:
    1. Private Sub mnuOpen_Click()
    2. On Error GoTo EH
    3.  
    4.     List1.Clear
    5.     Me.CommonDialog1.InitDir = App.Path
    6.     Me.CommonDialog1.Filter = "MyLivePromoter Files (*.jmg)|*.jmg"
    7.     Me.CommonDialog1.CancelError = True
    8.     Me.CommonDialog1.ShowOpen
    9.     If Me.CommonDialog1.FileName <> "" Then
    10.         ' check that file exist
    11.         Set fs2 = New FileSystemObject '
    12.  
    13.         If fs2.FileExists(CommonDialog1.FileName) Then
    14.             Me.txtFile.Text = CommonDialog1.FileName
    15.               Label3.Caption = txtFile.Text
    16.  
    17.         Else
    18.             MsgBox CommonDialog1.FileName & " doesn't exist", vbCritical, "File not found"
    19.         End If
    20.     End If
    21.  
    22.     Set fs2 = New FileSystemObject
    23.     Set ts2 = fs2.OpenTextFile(Me.txtFile.Text, ForReading, False)
    24.  
    25.     While Not ts2.AtEndOfStream
    26.         Me.List1.AddItem (ts2.ReadLine)
    27.     Wend
    28.     End If
    29.  
    30.     ts2.Close
    31.     Set ts2 = Nothing
    32.  
    33.     Exit Sub
    34. EH:
    35. End Sub
    If you press cancel the error will be fired and you will se the msgbox with the error, but just in design mode, if you want it to react as a real exe, change in the Vb IDE to "Break in class module errors" (or something like that) by doing: right click in the code area / Alternate / Break in class module errors. My VB is in spanish, so I don't know if thats exactly what it says there.
    Its not recomended leaving that configuration, I just tell you how to see the program reacting as a compiled program, don't change that permanently.
    The normal configuration for programming purposes is "Break on All Errors", use that one always, else you won't know when your app fires an error that's being captured by the error handler, and it shouldn't. In that case you won't even notice that there was an error.
    Last edited by jcis; Jan 15th, 2006 at 10:58 PM.

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Error when I hit cancel...how do I handle this?

    Since CommonDialog is used then whole idea of checking if file is valid is pretty much useless - if file isn't there then it won't show up, as simple as that. Having said that you don't need entire section with msgbox at all. You may aslo set CancelError to False.
    The following is what you may use instead:
    VB Code:
    1. With CommonDialog1
    2.     .InitDir = App.Path
    3.     .Filter = "MyLivePromoter Files (*.jmg)|*.jmg"
    4.     .ShowOpen
    5.     If .FileName = "" Then
    6.         Exit Sub
    7.     Else
    8.         txtFile.Text = .FileName
    9.         Label3.Caption = .FileName
    10.        
    11.         Set fs2 = New FileSystemObject
    12.         Set ts2 = fs2.OpenTextFile(Me.txtFile.Text, ForReading, False)
    13.  
    14.         Do While Not ts2.AtEndOfStream
    15.             List1.AddItem (ts2.ReadLine)
    16.         Loop
    17.  
    18.         ts2.Close
    19.         Set ts2 = Nothing
    20.     End If
    21. End With

  6. #6
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Error when I hit cancel...how do I handle this?

    Quote Originally Posted by RhinoBull
    Since CommonDialog is used then whole idea of checking if file is valid is pretty much useless - if file isn't there then it won't show up, as simple as that. Having said that you don't need entire section with msgbox at all. You may aslo set CancelError to False.
    The following is what you may use instead:
    VB Code:
    1. With CommonDialog1
    2.     .InitDir = App.Path
    3.     .Filter = "MyLivePromoter Files (*.jmg)|*.jmg"
    4.     .ShowOpen
    5.     If .FileName = "" Then
    6.         Exit Sub
    7.     Else
    8.         txtFile.Text = .FileName
    9.         Label3.Caption = .FileName
    10.        
    11.         Set fs2 = New FileSystemObject
    12.         Set ts2 = fs2.OpenTextFile(Me.txtFile.Text, ForReading, False)
    13.  
    14.         Do While Not ts2.AtEndOfStream
    15.             List1.AddItem (ts2.ReadLine)
    16.         Loop
    17.  
    18.         ts2.Close
    19.         Set ts2 = Nothing
    20.     End If
    21. End With
    I tested and it works only for the first time.
    1) Open a file with common dialog
    2) Open common dialog again and select cancel: FileName is not "", and the file is loaded even if I delete the filename manually.
    Last edited by jcis; Jan 15th, 2006 at 11:30 PM.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    148

    Re: Error when I hit cancel...how do I handle this?

    Rhino - Thank you very much!

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    148

    Re: Error when I hit cancel...how do I handle this?

    Quote Originally Posted by jcis
    I tested and it works only for the first time.
    1) Open a file with common dialog
    2) Open common dialog again and select cancel: FileName is not "", and the file is loaded even if I delete the filename manually.
    Weird. Every time I hit cancel it just cancels it.

  9. #9
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Error when I hit cancel...how do I handle this?

    Quote Originally Posted by takamine334
    Weird. Every time I hit cancel it just cancels it.
    Did you open a file normally, then opened Commondlg again and pressed cancel?

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    148

    Re: [RESOLVED] Error when I hit cancel...how do I handle this?

    yeah, it doesn't do anything...just cancels like its supposed to

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width