Click to See Complete Forum and Search --> : Catching a file open error?


JasVC
May 2nd, 2001, 12:27 PM
Hello! I'd like to be able to catch an error upon attempting to open a file. Currently, if the file im trying to open is already in use by another app, my app crashes. I'd rather keep my program running and post a message stating the file is currently in use.

Any thoughts?

Thanks!!

CR8YrF8

shree
May 2nd, 2001, 12:40 PM
on error Goto ErrHndlr
Open Filename .....
on error Goto 0
....
Exit Sub
ErrHndlr:
Msgbox ....

JasVC
May 2nd, 2001, 01:15 PM
Hmm ok here is what I have. This gives me an undefined label error in the line On Error GoTo ErrHndlr.

on error GoTo ErrHndlr
Open "myFile" for Append as #1
on error GoTo 0
print #1, "test"
Close #1
End Sub
ErrHndlr:
dim temp
temp = MsgBox("error", vbYesNo, "File Write error")



What am i doing wrong?
Thanks again

CR8YrF8

JasVC
May 2nd, 2001, 01:19 PM
Doh I've got it. I was using "End Sub" rather than "Exit Sub". End sub only goes at the actual end, and exit sub goes before the error handling to avoid unwanted execution.

Thanks

CR8YrF8

shree
May 2nd, 2001, 01:19 PM
Not End Sub, it should be Exit Sub
End Sub should be at the last of everything.


on error GoTo ErrHndlr
Open "myFile" for Append as #1
on error GoTo 0
print #1, "test"
Close #1
Exit Sub
ErrHndlr:
dim temp
temp = MsgBox("error", vbYesNo, "File Write error")
End Sub