|
-
Jan 15th, 2006, 10:03 PM
#1
Thread Starter
Addicted Member
[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?
-
Jan 15th, 2006, 10:07 PM
#2
Re: Error when I hit cancel...how do I handle this?
You should tell us what do you use and how do you do it. Also, showing your code may help.
-
Jan 15th, 2006, 10:28 PM
#3
Thread Starter
Addicted Member
Re: Error when I hit cancel...how do I handle this?
VB Code:
Private Sub mnuOpen_Click()
On Error GoTo EH
List1.Clear
Me.CommonDialog1.InitDir = App.Path
Me.CommonDialog1.Filter = "MyLivePromoter Files (*.jmg)|*.jmg"
Me.CommonDialog1.ShowOpen
If Me.CommonDialog1.FileName & "" <> "" Then
' check that file exist
Set fs2 = New FileSystemObject '
If fs2.FileExists(CommonDialog1.FileName) Then
Me.txtFile.Text = CommonDialog1.FileName
Label3.Caption = txtFile.Text
Else
MsgBox CommonDialog1.FileName & " doesn't exist", vbCritical, "File not found"
End If
End If
Set fs2 = New FileSystemObject
Set ts2 = fs2.OpenTextFile(Me.txtFile.Text, ForReading, False)
While Not ts2.AtEndOfStream
Me.List1.AddItem (ts2.ReadLine)
Wend
ts2.Close
Set ts2 = Nothing
EH:
Call MsgBox("We have encountered an error and must shut down!", vbCritical Or vbSystemModal, App.Title)
Unload Me
End Sub
Now, obviously I don't want the program to shut down, but that message box looks better than a run time error
-
Jan 15th, 2006, 10:43 PM
#4
Re: Error when I hit cancel...how do I handle this?
Use CancelError like this
VB Code:
Private Sub mnuOpen_Click()
On Error GoTo EH
List1.Clear
Me.CommonDialog1.InitDir = App.Path
Me.CommonDialog1.Filter = "MyLivePromoter Files (*.jmg)|*.jmg"
Me.CommonDialog1.CancelError = True
Me.CommonDialog1.ShowOpen
If Me.CommonDialog1.FileName <> "" Then
' check that file exist
Set fs2 = New FileSystemObject '
If fs2.FileExists(CommonDialog1.FileName) Then
Me.txtFile.Text = CommonDialog1.FileName
Label3.Caption = txtFile.Text
Else
MsgBox CommonDialog1.FileName & " doesn't exist", vbCritical, "File not found"
End If
End If
Set fs2 = New FileSystemObject
Set ts2 = fs2.OpenTextFile(Me.txtFile.Text, ForReading, False)
While Not ts2.AtEndOfStream
Me.List1.AddItem (ts2.ReadLine)
Wend
End If
ts2.Close
Set ts2 = Nothing
Exit Sub
EH:
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.
-
Jan 15th, 2006, 11:03 PM
#5
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:
With CommonDialog1
.InitDir = App.Path
.Filter = "MyLivePromoter Files (*.jmg)|*.jmg"
.ShowOpen
If .FileName = "" Then
Exit Sub
Else
txtFile.Text = .FileName
Label3.Caption = .FileName
Set fs2 = New FileSystemObject
Set ts2 = fs2.OpenTextFile(Me.txtFile.Text, ForReading, False)
Do While Not ts2.AtEndOfStream
List1.AddItem (ts2.ReadLine)
Loop
ts2.Close
Set ts2 = Nothing
End If
End With
Last edited by RhinoBull; Jan 15th, 2006 at 11:09 PM.
-
Jan 15th, 2006, 11:17 PM
#6
Re: Error when I hit cancel...how do I handle this?
 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:
With CommonDialog1
.InitDir = App.Path
.Filter = "MyLivePromoter Files (*.jmg)|*.jmg"
.ShowOpen
If .FileName = "" Then
Exit Sub
Else
txtFile.Text = .FileName
Label3.Caption = .FileName
Set fs2 = New FileSystemObject
Set ts2 = fs2.OpenTextFile(Me.txtFile.Text, ForReading, False)
Do While Not ts2.AtEndOfStream
List1.AddItem (ts2.ReadLine)
Loop
ts2.Close
Set ts2 = Nothing
End If
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.
-
Jan 15th, 2006, 11:23 PM
#7
Thread Starter
Addicted Member
Re: Error when I hit cancel...how do I handle this?
Rhino - Thank you very much!
-
Jan 15th, 2006, 11:35 PM
#8
Thread Starter
Addicted Member
Re: Error when I hit cancel...how do I handle this?
 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.
-
Jan 15th, 2006, 11:38 PM
#9
Re: Error when I hit cancel...how do I handle this?
 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?
-
Jan 15th, 2006, 11:51 PM
#10
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|