?
Solved

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

Posted on 2003-10-28
6
Medium Priority
?
991 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

752 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