Results 1 to 7 of 7

Thread: [RESOLVED] VB3 - Common Dialog - File Save Open/Close error traps

  1. #1

    Thread Starter
    Member MorkenTheMonk's Avatar
    Join Date
    Sep 2017
    Location
    Scotland, UK
    Posts
    42

    Resolved [RESOLVED] VB3 - Common Dialog - File Save Open/Close error traps

    Been using an old VB3 application for many years, however we've just discovered that there are no error-traps in the File - Save dialog using Open fNum and Close fNum. Quick search in VB3 Help doesn't suggest Open or Close return any errors if the file-creation/write process fails (e.g. read-only permissions to the selected location). This leads to uncertainty as to whether a file has ACTUALLY successfully saved or not!?

    Anyone able to offer any ideas on how to get around this problem in VB3?

    Thanks in advance,

    MorkenTheMonk

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: VB3 - Common Dialog - File Save Open/Close error traps

    The file dialog will not return any error on opening or closing a file as that is not what opens or closes the file.
    Whatever code you have that opens and writes to the file is where you would see an error returned if there is a problem.

    All the dialog does is allows the user to browse to location and select a file or filename. the dialog returns what the user selected or optionally an error if they hit cancel. Once you get the path\filename then your code must take over. If you are getting errors but not seeing them that would seem to indicate the use of On Error Resume Next or an error trap with no message in it to alert the user of a problem.

  3. #3
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: VB3 - Common Dialog - File Save Open/Close error traps

    right, unless you have on error resume next the app should crash / throw up an error message if the file operation fails.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: VB3 - Common Dialog - File Save Open/Close error traps

    Code:
    Open Filename for Output as #FileNum
    If file location is read only the line above will throw an error as will the one below
    Code:
    Open Filename for Append as #FileNum
    Code:
    Open Filename for Input as #FileNum
    Will not throw an error if the file is read only but will throw an error if the file does not exist.

    The same is true for binary and random methods, Any mode that would open the file for write access will error out if the user does not have write permissions.

    If you still need help then you should show your code for the routine that opens and writes to the file.

  5. #5

    Thread Starter
    Member MorkenTheMonk's Avatar
    Join Date
    Sep 2017
    Location
    Scotland, UK
    Posts
    42

    Re: VB3 - Common Dialog - File Save Open/Close error traps

    Thanks guys...

    I'm using simple "Open fName as Output as fNum" code followed by a bunch of "Write #fNum, <variable>" followed by a "Close fNum" statement.

    The specific example is when we use this to write to a network-mapped drive (X, although we can Open a file from there, and browse to the location during Save, the user may not have permissions to WRITE to the drive. Currently, when our user clicks [SAVE] our VB3 application does NOT produce an error message to say that it was unable to write the file - instead it merely closes the File Dialog as if all was well!

    Just spotted that at the top of the Save procedure we have the code "On Error Resume Next" which is presumably why the code simply carries on regardless. Is it this that prevents us seeing an error message?

    Cheers...

    MorkenTheMonk

  6. #6
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,906

    Re: VB3 - Common Dialog - File Save Open/Close error traps

    Yes, "On Error Resume Next" ignores all problems.
    Better have decent error handler.

    Air Code Ahead!
    Code:
    Private Sub WriteFile(fName As String)
    Dim fNum As Integer
    
    On Error GoTo errHandler
    
    fNum = FreeFile
    Open fName as Output as fNum
    ' Bunch of Write statements
    Close fNum
    Exit Sub
    
    errHandler:
      MsgBox "The following error occured: " & vbLf & "Error: " & Err.Number & vbLf & "Description: " & Err.Description & vbLf & "In Sub WriteFile()" , vbCritical
    End Sub

  7. #7

    Thread Starter
    Member MorkenTheMonk's Avatar
    Join Date
    Sep 2017
    Location
    Scotland, UK
    Posts
    42

    Re: VB3 - Common Dialog - File Save Open/Close error traps

    Thanks for the replies guys.

    I've now added an Error handler (tweaked Arnoutdv's example above to cater for VB3's creaky old MsgBox and Err statements), and tested by attempting to overwrite a Read Only file... works perfectly, and now at least informs the user that their Save FAILED.

    Good work... thanks!
    __________________________________________________________________________________________
    MorkenTheMonk
    SCOTLAND

    (Stuck supporting a 16-bit VB3 application over 20 years old! Deep Joy!!!)

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