Solved

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

Posted on 2003-10-28
6
875 Views
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
 
 
  Email.MoveFirst
  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..........................................

    Email.MoveNext
   
    'Reset Arrays
    Erase varpeeps
    Erase varAttach
  Loop
   
  DoCmd.SetWarnings True
  dbAddress.Close
  MsgBox ("Complete")
End Sub
0
Comment
Question by:gybjw
6 Comments
 
LVL 8

Expert Comment

by:ampapa
Comment Utility
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

0
 
LVL 11

Expert Comment

by:dbrckovi
Comment Utility
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  
     Else
         Err = 0                                'just clear the error value
     End If

Next X
   
0
 
LVL 11

Accepted Solution

by:
dbrckovi earned 125 total points
Comment Utility
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.

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 16

Expert Comment

by:JohnBPrice
Comment Utility
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
endif
0
 

Author Comment

by:gybjw
Comment Utility
Many thanks dbrckovi I never realised it could be so simple!
0
 
LVL 11

Expert Comment

by:dbrckovi
Comment Utility
Thanx!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

762 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now