+ Reply to Thread
Results 1 to 8 of 8

Thread: Incorrect error Handling

  1. #1
    Valued Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Incorrect error Handling

    I have some error handling code that is supose to halt or termintate the program in the case that a file cannot be found - this would happen if the date extension of the file does not exist for the date the user selected via the date picker for a textbox. Unfortunately it does not work correctly and causes the code to run continuously without stopping ( a loop).

    Please see the error handling below, for any suggestions:

    Private Sub cmdReturnMTMPortfolio_Click()
        Dim appXL As Excel.Application
        Dim strFilePath As String
        Dim strFileName As String
        Dim strFilePrefix As String
        Dim strFullFileName As String
        Dim strFile As String
        Dim asofDATE As String
        Dim txtDate As TextBox
        Dim wbk As Excel.Workbook
                
        On Error GoTo HandleErr
        strFilePrefix = "mtm_report_asof_"
        strFilePath = "N:\XXXXXXX\YYYYYY\Assignments and Projects\FCIP Program\Spreadsheet Automation\Product\Excel Automation\Robin\Communication\Portfolio List\"
           
        asofDATE = Me.txtDate.Value
        asofDATE = Format(Me.txtDate.Value, "yyyymmdd")
        
        Set appXL = New Excel.Application
        
        strFullFileName = strFilePrefix & asofDATE & ".xls"
        
        Set wbk = appXL.Workbooks.Open(strFilePath & strFullFileName)
        
        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "tempMTMReportData", strFilePath & strFullFileName, True, "MTM Detail!"
        DoCmd.OpenQuery "qryCPNamesForMTMDailyReportParentandChild"
        DoCmd.Close acQuery, "qryCPNamesForMTMDailyReportParentandChild"
        MsgBox "Your MTM file has been imported."
              
    ExitHere:
        On Error Resume Next
        appXL.ScreenUpdating = True
        appXL.Quit
        Set appXL = Nothing
        Exit Sub
    HandleErr:
        MsgBox Err & ": " & Err.Description
        Resume ExitHere
        Resume
        
    End
    Sub

  2. #2
    Forum Guru davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2007
    Posts
    1,879

    Re: Incorrect error Handling

    Take out the on error resume next line.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Valued Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Incorrect error Handling

    Thanks Dave for suggestion. But that does not work.

  4. #4
    Forum Guru davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2007
    Posts
    1,879

    Re: Incorrect error Handling

    Also take out the Resume line. Do you know how Resume works? If not, you need to read up on it before you use your code. It is a dangerous way of ignoring errors instead of really dealing with them.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  5. #5
    Valued Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Incorrect error Handling

    Dave thanks. I got rid of both lines you said. it still does not work. All I want to do is check to see if the file exist. If it does not a prompt error message should pop-up.

  6. #6
    Forum Guru davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2007
    Posts
    1,879

    Re: Incorrect error Handling

    You can check if a file exists using the dir function:

    If Dir("C:\yourfile.xls") = "" Then MsgBox "File does not exist"
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  7. #7
    Valued Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Incorrect error Handling

    Dave thanks. It's not really working, but I am getting closer. It pops up even if the file exist? Not sure whats wrong here.

    Private Sub cmdReturnMTMPortfolio_Click()
        Dim appXL As Excel.Application
        Dim strFilePath As String
        Dim strFileName As String
        Dim strFilePrefix As String
        Dim strFullFileName As String
        Dim strFile As String
        Dim asofDATE As String
        Dim txtDate As TextBox
        Dim wbk As Excel.Workbook
                
        strFilePrefix = "mtm_report_asof_"
        strFilePath = "N:\XXXXXXX\YYYYYYY\Assignments and Projects\ZZZZZZZZZ\Spreadsheet Automation\Product\Excel Automation\Robin\Communication\Portfolio List\"
           
        asofDATE = Me.txtDate.value
        asofDATE = Format(Me.txtDate.value, "yyyymmdd")
        
        Set appXL = New Excel.Application
        
        strFullFileName = strFilePrefix & asofDATE & ".xls"
        If Dir(strFullFileName) = "" Then MsgBox "File does not exist"
        
        Set wbk = appXL.Workbooks.Open(strFilePath & strFullFileName)
        
        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "tempMTMReportData", strFilePath & strFullFileName, True, "MTM Detail!"
        DoCmd.OpenQuery "qryCPNamesForMTMDailyReportParentandChild"
        DoCmd.Close acQuery, "qryCPNamesForMTMDailyReportParentandChild"
        MsgBox "Your MTM file has been imported."
              
       
    End Sub

  8. #8
    Forum Guru davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2007
    Posts
    1,879

    Re: Incorrect error Handling

    I'd suggest putting a code break (click a red dot onto the left margin) on the If Dir(... line. Then when the code runs, it will stop before executing that line. Next while still in debug mode, go to the immediate window and type Print strFullFileName. Press enter and this will show the path the database expects the file to be at. Next using the windows explorer go to the actual file, copy the path and paste it into the immediate window to compare the two strings. Do they match exactly?
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0