Solved

I need an Access Macro that runs and emails a query upon the opening of the database.

Posted on 2014-07-23
2
337 Views
Last Modified: 2014-07-30
I need an Access Macro that runs and emails a query upon the opening of the database.
0
Comment
Question by:garyrobbins
  • 2
2 Comments
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 40215396
Run this (with appropriate modifications for your object names) from an AutoExec macro:
Private Function SendMailWithReportAttachment()
'Created by Helen Feddema 5-19-2000
'Last modified 14-Jan-2010

On Error GoTo ErrorHandler

   Dim dbs As DAO.Database
   Dim appOutlook As New Outlook.Application
   Dim itm As Outlook.MailItem
   Dim strFileName As String
   Dim strFilePath As String
   Dim strDBName As String

   Set dbs = CurrentDb
   strFilePath = Application.CurrentProject.Path
   strFileName = strFilePath & "Contacts.txt"
   
   'Save Contacts table as a text file in same folder as database
   DoCmd.TransferText acExportDelim, "Contacts Export Specification", _
      "tblContacts", strFileName, True
      
   'Create new mail message and attach text file to it
   Set itm = appOutlook.CreateItem(olMailItem)
   With itm
      .To = "John Doe"
      .Subject = "Contacts file you requested"
      .Body = "This file was exported from tblContacts on " & _
         Date & "." & vbCrLf & vbCrLf
      .Attachments.Add strFileName
      .Display
   End With
   
ErrorHandlerExit:
   Set appOutlook = Nothing
   Exit Sub

ErrorHandler:
   MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
   Resume ErrorHandlerExit

End Sub

Open in new window

0
 
LVL 31

Accepted Solution

by:
Helen_Feddema earned 500 total points
ID: 40215404
The previous comment's code sends a query saved to a text file.  If you want to send a report as a PDF, that can be done too.  Here is a procedure (for Access 2007 or higher) that saves a report as a PDF and emails it:
Private Function SendPDFReport() 
'Created by Helen Feddema 17-Jan-2010 
'Last modified 17-Jan-2010 
 
On Error GoTo ErrorHandler 
 
   Dim appOutlook As New Outlook.Application 
   Dim itm As Outlook.MailItem 
   Dim strFileName As String 
   Dim rpt As Access.Report 
   Dim strReport As String 
   Dim strCurrentPath As String 
   Dim strFileNameAndPath As String 
    
   strCurrentPath = Application.CurrentProject.Path & "\" 
   strReport = "rptProductPrices" 
   strFileName = "Product Prices.pdf" 
   strFileNameAndPath = strCurrentPath & strFileName 
    
   'Output report to PDF in current path 
   DoCmd.OutputTo objecttype:=acOutputReport, _ 
      objectname:=strReport, _ 
      outputformat:=acformatpdf, _ 
      outputfile:=strFileNameAndPath, _ 
      autostart:=False 
    
   'Create new mail message and attach text file to it 
   Set itm = appOutlook.CreateItem(olMailItem) 
   With itm 
      .To = "someone@xyz.com" 
      .Subject = "Daily report" 
      .Body = "Your message" 
      .Attachments.Add strFileNameAndPath 
      'To edit before sending 
      .Display 
      'To send automatically 
      '.Send 
   End With 
    
ErrorHandlerExit: 
   Exit Sub 
 
ErrorHandler: 
   MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description 
   Resume ErrorHandlerExit 
 
End Function

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

757 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

20 Experts available now in Live!

Get 1:1 Help Now