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?
Printable View
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?
You should tell us what do you use and how do you do it. Also, showing your code may help.
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
Use CancelError like this
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.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
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.
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.Quote:
Originally Posted by RhinoBull
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. :confused:
Rhino - Thank you very much!
Weird. Every time I hit cancel it just cancels it.Quote:
Originally Posted by jcis
Did you open a file normally, then opened Commondlg again and pressed cancel?Quote:
Originally Posted by takamine334
yeah, it doesn't do anything...just cancels like its supposed to