Supress error if file not found.

Timoty

Registered User.
Local time
Today, 15:05
Joined
Jul 29, 2003
Messages
105
I have a buton that does a number of things including sending an email. However, if, for whatever reason, a file wasn't deleted on a previous action I get an error message. I tried to fix that but then if there is no file I get an error message that there is no file found. Is there a way to just supress the error message so that the code continues whether the file exists or not.

Dim strEmail As String
Dim rstEmail As DAO.Recordset
Dim EmailApp As Object
Dim NameSpace As Object
Dim EmailSend As Object

Set rstEmail = CurrentDb.OpenRecordset("Addresses")

Do Until rstEmail.EOF
strEmail = strEmail & rstEmail("EmailAddress") & ";"
rstEmail.MoveNext
Loop

If Right(strEmail, 1) = ";" Then
strEmail = Left(strEmail, Len(strEmail) - 1)
End If


strPath = DLookup("TextPath", "Filepath")

Kill strPath (this fixed my first problem but now if there is no file I get an error)

DoCmd.OutputTo acReport, "Email", acFormatRTF, strPath

Open strPath For Input As #1
Do Until EOF(1)
Line Input #1, strTemp
strBody = strBody & strTemp & vbCrLf
Loop
Close #1
Set EmailApp = CreateObject("Outlook.Application")
Set NameSpace = EmailApp.getNameSpace("MAPI")
Set EmailSend = EmailApp.CreateItem(0)

EmailSend.To = strEmail
EmailSend.subject = "EMERGENCY"
'EmailSend.Attachments.Add strPath
EmailSend.Display
EmailSend.Body = strBody

Set EmailApp = Nothing
Set NameSpace = Nothing
Set EmailSend = Nothing

Kill strPath
rstEmail.Close
Set rstEmail = Nothing
 
One possible way to solve your problem is to do a count of
the records in your recordset using "RecordsetCount" followed
by an "Exit Sub" if the recordsetcount = 0, else proceed with
your report.

e.g. If rst.recordsetcount = 0 then
Exit Sub
Else
......the rest of your code
End If

You can even pop up a message telling the user that the recordset
is empty or something to that effect, using a custome msgbox if there
is nothing to report.
 

Users who are viewing this thread

Back
Top Bottom