Access VBA - 'On Error' Send Email Help

Cjdash

New Member
Joined
Jun 1, 2018
Messages
7
Hi,

I have the below code that picks up a file based on the contents of a Excel range. However, there will be occasions where a file won't be present. On these occasions I want the code to ping an email to me to let me know. As it stands, the below is sending me 3 emails rather than 1, can anyone help?

Code:
Function ImportTLogAuto()


    DoCmd.SetWarnings False
    
    DoCmd.RunSQL "DELETE FROM TLog"


    Dim objDialog As Object
    Dim varFile As Variant
    Dim strsql As String


    Dim xlApp As Excel.Application
    Dim xlWB As Excel.Workbook
    Dim xlSheet As Excel.Worksheet


    Set xlApp = New Excel.Application
With xlApp
    Set xlWB = .Workbooks.Open("C:\Program Files\Morning Imports.xlsm", , False)
    Set xlSheet = xlWB.Worksheets("PickUp") 'Name of tab you want to select from
End With
 
    varFile = xlSheet.Range("D9")
    
On Error GoTo NoTLog


        DoCmd.TransferText acImportDelim, "TLog Import", "TLog", varFile


        strsql = "UPDATE TLog SET TLog.Filename = '" & varFile & "' " _
               & "WHERE (((TLog.Filename) Is Null));"


        CurrentDb.Execute strsql


    DoCmd.OpenQuery "Qry_Append_RecordLines"
    DoCmd.OpenQuery "Qry_Append_Record61"


NoTLog:
    Call Error_Email("404 - TLog Not Found")


Resume Next


End Function
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,836
Office Version
  1. 2019
Platform
  1. Windows
Error handling with Resume Next needs to be thought through carefully. You are basically asking your code to continue execution where it left of when the error occurred - which will mean that eventually it gets back to the Call Error_Email() line. Basically, though, that error handling looks wrong. Why did you choose Resume Next here?
 

Forum statistics

Threads
1,141,315
Messages
5,705,699
Members
421,406
Latest member
kluna90

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top