Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / January 2006

Tip: Looking for answers? Try searching our database.

Run query from open and send email

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Qaspec - 06 Jan 2006 15:22 GMT
I would like to run a query whenever the DB is opened. The query is named
"RemQry". It returns records where the days transpired is greater than 2. It
contains the owners of the records and the email addresses. Once "RemQry"
runs I would like to programatically send an email for each record to the
corresponding email in the record. I have a module to send an email but it is
not connected to the query in any way.

Option Explicit

Sub SendMessage(Optional AttachmentPath)
  Dim objOutlook As Outlook.Application
  Dim objOutlookMsg As Outlook.MailItem
  Dim objOutlookRecip As Outlook.Recipient
  Dim objOutlookAttach As Outlook.Attachment

  ' Create the Outlook session.
  Set objOutlook = CreateObject("Outlook.Application")

  ' Create the message.
  Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

  With objOutlookMsg
     ' Add the To recipient(s) to the message.
    Set objOutlookRecip = .Recipients.Add("mark.griffin@percepta-crm.com")
     objOutlookRecip.Type = olTo

     ' Add the CC recipient(s) to the message.
     Set objOutlookRecip = .Recipients.Add("mark.griffin@percepta-crm.com")
     objOutlookRecip.Type = olCC

     ' Set the Subject, Body, and Importance of the message.
     .Subject = "CRC Escalated Case Reminder"
     .Body = "It has been at least 2 days since this case has been
escalated. There is no record that a call back has been made. If this is an
error please notify the sender to update the database accordingly. If a call
back is still outstanding please place the call as soon as possible and
notify the sender to update the database. Thank You.  "
     .Importance = olImportanceHigh  'High importance

     ' Add attachments to the message.
     If Not IsMissing(AttachmentPath) Then
        Set objOutlookAttach = .Attachments.Add(AttachmentPath)
     End If

     ' Resolve each Recipient's name.
     For Each objOutlookRecip In .Recipients
        objOutlookRecip.Resolve
        If Not objOutlookRecip.Resolve Then
        objOutlookMsg.Display
     End If
     Next
     .Send

  End With
  Set objOutlookMsg = Nothing
  Set objOutlook = Nothing
End Sub
 
Tim Ferguson - 06 Jan 2006 18:06 GMT
> I would like to run a query whenever the DB is opened.

Then call it from an AutoExec macro: see help. If you need to call VBA
code, you'll need the RunCode action.

> Once "RemQry" runs I would like to programatically send an
> email for each record to the corresponding email in the record. I have
> a module to send an email but it is not connected to the query in any
> way.

 ' get the records into recordset
 set rs = querydefs("remqry").openrecordset( _
     dbopensnapshot, dbforwardonly)

 ' don't do anything if no records returned
 do while not rs.eof

   ' call your message sending subroutine
   SendMessageTo rs!subjectemailaddress

   ' move to the next record. The loop will exit
   ' when there are no records left
   rs.movenext

 ' go round again for the next one
 loop

 ' tidy up
 rs.close

Hope that helps

Tim F
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.