Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2003-10-28
6
Medium Priority
?
1,047 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 8

Expert Comment

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

0
 
LVL 11

Expert Comment

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

Next X
   
0
 
LVL 11

Accepted Solution

by:
dbrckovi earned 500 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.

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Expert Comment

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

Author Comment

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

Expert Comment

by:dbrckovi
ID: 9639678
Thanx!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

597 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