ignore error message and continue if path not found in VBA?

Posted on 2003-10-28
Last Modified: 2010-08-05
I have the following piece of code that looks for various files, attaches them and then emails them. Problem is that some files are only there occaisionally (ie not every week), so when it can't find the path (varAttach(x - 1) = Email("Path") & " " & Email("Account name") & " " & Email("Account Number" & x) & " September 2003.xls") i get an error message and have to start again. I need a piece of code that will skip a path it can't find and move on to the next path without bringing up an error message.

PS any answers as simple as possible I'm not very good at VBA!

Many thanks in advance.

Private Sub Command0_Click()

  Dim varpeeps(10) As Variant
  Dim varAttach(10) As Variant
  Dim xlapp As Object
  Dim dbAddress As Database
  Dim Email As Recordset
  Dim x As Integer
  Dim DateBox As String
  Set dbAddress = CurrentDb
  Set Email = dbAddress.OpenRecordset("Email Addresses")
  DateBox = Forms![Form1]!DateBox
  DoCmd.SetWarnings False
  Do Until Email.EOF
    For x = 1 To 7
      If IsNull(Email("Email Address " & x)) = False Then
        varpeeps(x - 1) = Email("Email Address " & x)
      End If
    Next x
    x = 0
    For x = 1 To 8
      If IsNull(Email("Account Number" & x)) = False Then
      varAttach(x - 1) = Email("Path") & " " & Email("Account name") & " " & Email("Account Number" & x) & " September 2003.xls"
      End If
    Next x
    x = 0
    Call SendNotesMail..........................................

    'Reset Arrays
    Erase varpeeps
    Erase varAttach
  DoCmd.SetWarnings True
  MsgBox ("Complete")
End Sub
Question by:gybjw

Expert Comment

ID: 9633652
Will this work? I think it might.

    For x = 1 To 8
      If IsNull(Email("Account Number" & x)) = False Then
      if dir(Email("Path") & " " & Email("Account name") & " " & Email("Account Number" & x) & " September 2003.xls") ="" then next
      varAttach(x - 1) = Email("Path") & " " & Email("Account name") & " " & Email("Account Number" & x) & " September 2003.xls"
      End If
    Next x
    x = 0

LVL 11

Expert Comment

ID: 9633695
The simples way is to put the following line on the top of your Sub

On Error Resume Next

This will prevent your program to report an error every time it doesn't find a file, but it will only assign an error value to a variable err.

So the way you wuol use this is something like that:

On Error Resume Next

For x = 1 To 10

   Open file(x) For Input As #1              'generates error if file is missing
     If Err = 0 then
         'send that file through mail        
         Close #1  
         Err = 0                                'just clear the error value
     End If

Next X
LVL 11

Accepted Solution

dbrckovi earned 125 total points
ID: 9633739
Instead of putting the line "On Error Resume Next"  on top of the Sub you can put it just before the piece of code where you would like it to take effect.

If you put it at the top of the sub, then every other possible error in that Sub would be ignored, and that can couse a lot of problems when debugging your program.

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

LVL 16

Expert Comment

ID: 9633794
Or you can check in advance with

dim fs as new filesystemobject ' you need to add a reference to microsoft scripting runtime

if fs.fileexists (Email("Path") & " " & Email("Account name") & " " & Email("Account Number" & x) & " September 2003.xls") then
    'do your email

Author Comment

ID: 9633892
Many thanks dbrckovi I never realised it could be so simple!
LVL 11

Expert Comment

ID: 9639678

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Write data from SQL to CSV file - position 1 61
Problem to skip loop 6 57
Windows 10 start screen issues 9 55
Sending a email via excel using vba 6 82
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question