CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2000
    Posts
    56

    Catching a file open error?

    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

  2. #2
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Catching a file open error?


    on error Goto ErrHndlr
    Open Filename .....
    on error Goto 0
    ....
    Exit Sub
    ErrHndlr:
    Msgbox ....





  3. #3
    Join Date
    Jan 2000
    Posts
    56

    Re: Catching a file open error?

    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

  4. #4
    Join Date
    Jan 2000
    Posts
    56

    Re: Catching a file open error?

    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

  5. #5
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Catching a file open error?

    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





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

Featured