Results 1 to 6 of 6

Thread: Error trapping.. stupid question

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    54

    Error trapping.. stupid question

    when i open a file for output or input whats the function to make sure there were no problems opening it?

    like
    open "C:\Jayiscool.txt" for ouput as #1
    ?

    i know its dumb but i cant seem to find an answer..

    thanks in advance for any help

    ~Jay

  2. #2
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375
    I believe that if there is a problem opening the file, VB will give you an error. I don't know the #, but it should say "File Already Open" or something similar...
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  3. #3
    ATommasi
    Guest
    I would use 'On Error' like this

    Code:
    private sub yoursub()
      On Error GoTo ErrorHandler
      open "C:\Jayiscool.txt" for ouput as #1 'If there is an error opening the file, it will go the the ErrorHandler 
    
      On Error Resume Next 'No error opening file, continue working as usual
         'do stuff here 
         exit sub
    
    ErrorHandler:
      msgbox "There was a problem opening the file"
    
    end sub

  4. #4
    Addicted Member Stephenb's Avatar
    Join Date
    Nov 2000
    Location
    Phoenix, Arizona.
    Posts
    172
    I wrote this routine to get a list of servers from a text file for a project quite some time ago. I believe the error routine might be what you are looking for.

    Code:
    Public Sub GetFile()
    Dim intCount
    Dim strFilestream 
    Dim strSource, strDestination, MyPath, strINIFile, AppLocation 
    Dim strDATE, strDateStamp, errExplanation 
    Dim msg
    Public colHolidays As New Collection  ' Creating a collection for the Holiday dates.
    On Error GoTo ErrorControl
    
    AppLocation = App.Path
    strINIFile = "\Chronos.cfg"
    MyPath = AppLocation
    MyPath = MyPath + strINIFile
    
        Open MyPath For Input As #1 ' Open file.
            Do While Not EOF(1) ' Loop until end of file.
            Input #1, strFilestream ' Read line into variable.
                If IsNumeric(strFilestream) Then
                    colHolidays.Add strFilestream 'Add a date into the Holiday collection
                End If
            Loop
        Close #1    ' Close file.
        
        Exit Sub
    
    ErrorControl:
    If Err.Number <> 0 Then     ' Quick error description
        msg = "Error #" & Str(Err.Number) & " was generated by " _
        & Err.Source & vbCr & vbLf & Err.Description
     End If
     
     'Write Error to Error File.
     Open AppLocation & "\error.log" For Append As #2 ' Open file.
         Write #2, strDateStamp ' write date stamp to error log
         Write #2, msg     ' write complete error message to log.
         Write #2,         ' write blank line.
     Close #2
     
     ' Error message being more descriptive.
     If Err.Number = 75 Then
        errExplanation = "Path or Filename may not exist."
     End If
    MsgBox msg & Chr(13) & errExplanation, vbCritical, "Program Error"
    
    End Sub
    You can get a list of error codes and their descriptions from the MSDN.

    HTH
    Stephen Boston
    Onward!
    VB6 Pro SP4, VBScript
    A+ Certified Techncian.
    No matter where you go, there you are.

  5. #5
    Megatron
    Guest
    Code:
    On Error Goto ERRH
    
    '// code here
    
    Exit Sub
    ERRH:
    MsgBox "Error opening file"

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    54
    Okay that code stephenb... was perfect!
    thank you all..

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