Consulting

Results 1 to 6 of 6

Thread: Error Handling

  1. #1
    VBAX Regular
    Joined
    Nov 2005
    Posts
    39
    Location

    Error Handling

    OK, just as I think I am getting good and projects start working like I think they should. I find the next wall.

    I have the following code:
    On Error GoTo ErrorHandler
    Workbooks.Open Filename:=stDirectory & Format(stRptDate, "yyyymmdd") & ".csv"
    When the file is not found goes to the ErrorHandler:
    ErrorHandler:
    ' Check for error, then show message.
    If Err.Number = 1004 And stRptDate = stDate Then
        msg = "Error: The File can not be opened. " & _
        vbNewLine & " - Click OK to Continue to the next File" & vbNewLine & _
        " - Click CANCEL to Stop Processing"
        msg = MsgBox(msg, vbOKCancel, "ERROR:")
        If msg = vbOK Then
            Err.Clear
            GoTo SkipFile
        End If
    Else
        msg = "Error # " & Str(Err.Number) & " was generated by " _
        & Err.Source & Chr(13) & Err.Description
        MsgBox msg, , "ERROR:"
    End If
    The idea is if the file is not found. You can continue the process (skipping the file). This works great the first time. But after that I just get the debugging popup box and is stops on the OPEN statement.

    How do I make the ON ERROR command work for more than 1 error in the execution?

  2. #2
    VBAX Master stanl's Avatar
    Joined
    Jan 2005
    Posts
    1,141
    Location
    In the particular situation you outlined, you might consider a more preventative approach. Using a simple function like below allows you prevent going to an error handler in the first place, so you could code like

    If FileExists(fname) Then processfile
    Else
    Skipfile
    End If
    .02 Stan

    Private Function FileExists(fname) As Boolean
    '   Returns TRUE if the file exists
        Dim x As String
        x = Dir(fname)
        If x <> "" Then FileExists = True _
            Else FileExists = False
    End Function

  3. #3
    VBAX Regular
    Joined
    Nov 2005
    Posts
    39
    Location
    Thanks for the suggestion Stan. I have done just that in previous projects. However, the file is not being opened from a local or network drive. The file is actually being opened from a web page. I guess I should have mentioned that in the original post.

    The variable stDirectory has something along the lines of http://utilreports/Daily. So the full path name of the file would be http://utilreports/Daily20060927.csv.

    If I use the DIR command I get an invalid file name.

  4. #4
    VBAX Master stanl's Avatar
    Joined
    Jan 2005
    Posts
    1,141
    Location
    Quote Originally Posted by GMan
    The variable stDirectory has something along the lines of http://utilreports/Daily. So the full path name of the file would be http://utilreports/Daily20060927.csv.

    If I use the DIR command I get an invalid file name.
    For web files, you can cook something up with the Shell, or the FSO.

    ;pseudo-code
    url = "http://utilreports/Daily"
    file = "Daily20060827.csv"
    set objShell = CreateObject("Shell.Application")
    set objFolder = objShell.NameSpace(url)
            if (not objFolder is nothing) then
                dim objFolderItem
     
                set objFolderItem = objFolder.ParseName(file)
     
                if (not objFolderItem is nothing) then
                    'process file here
                end if
            end if

  5. #5
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by GMan
    OK, just as I think I am getting good and projects start working like I think they should. I find the next wall.

    I have the following code:
    On Error GoTo ErrorHandler
    Workbooks.Open Filename:=stDirectory & Format(stRptDate, "yyyymmdd") & ".csv"
    When the file is not found goes to the ErrorHandler:
    ErrorHandler:
    ' Check for error, then show message.
    If Err.Number = 1004 And stRptDate = stDate Then
        msg = "Error: The File can not be opened. " & _
        vbNewLine & " - Click OK to Continue to the next File" & vbNewLine & _
        " - Click CANCEL to Stop Processing"
        msg = MsgBox(msg, vbOKCancel, "ERROR:")
        If msg = vbOK Then
            Err.Clear
            GoTo SkipFile
        End If
    Else
        msg = "Error # " & Str(Err.Number) & " was generated by " _
        & Err.Source & Chr(13) & Err.Description
       MsgBox msg, , "ERROR:"
    End If
    The idea is if the file is not found. You can continue the process (skipping the file). This works great the first time. But after that I just get the debugging popup box and is stops on the OPEN statement.

    How do I make the ON ERROR command work for more than 1 error in the execution?
    As to this question: Replace the GoTo SkipFile with Resume SkipFile
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  6. #6
    VBAX Regular
    Joined
    Nov 2005
    Posts
    39
    Location
    Thanks Stan for the reply. I am going to work on this today and hope to have it resolved.

    Also, thank you John. I will certainly remember this for future projects. I guess that problem with being self taught in VBA strickly by trial and error means I miss some easy things from time to time.

    Don't know what I would do without my friends from VBAExpress!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •