I'm still having to do a little guesswork because I' m not sure I have all the details I need.
Still - I'll go ahead and tell you how I might approach this.
I'll be making some assumptions about what data is in your database etc.
First of all - let me warn you that if you are using Outlook, you might run into the problem of Outlook not wanting to allow Access to send the email [security in Outlook]. You might get a message from Outlook saying something like, " . . . a program is attempting to send an email in your name . . .".
If you click OK - Outlook will send the email. But that defeats the whole 'automation' thing. But if you're ok with that then there's no problem.
There is software you can purchase to override/bypass Outlook Security - or wriggle through the gaps.
What I ended up doing was writing my data to folders on our network that the respective people regularly check anyway for other work they have out there.
That being said . . .
If I wanted to automatically email people when their licenses are up for renewal here's a gist of what I'd do:
(I'm going to assume for this post that the person's email address and license expiration date are in the same Table [Table1]).
1. Create a macro named 'AutoExec'.
2. Put a 'RunCode' Action in the Macro - to run a Function that has code that will:
. . . i. Find all records where license needs to be renewed.
. . . ii. Get the email addresses for all those people.
. . . iii. Run Code that will email each person on the list.
I'm putting two functions in here.
The first one will show you how to get data [like your email addresses] out of your table and store it in a variable.
The second one will show you how to send an email [with attachments If you want to attach anything ever].
Run each of these in your own functions till you're comfortable with how they work.
Then you can put the code from 'EmailAttach()' into the 'Get_DB_Values()' function -
[into the Do While Loop]
so that every time an email address is found - an email is sent to it.
Both segments of code will be in a single function.
This single function will be called from your AutoExec Macro that will run automatically whenever you open your database.
[IF you want to open the DB in Design Mode when you have an AutoExec Macro - hold down the shift key].
When you do that - your 'Get_DB_Values' code will retrieve email addresses one at a time and and you will send the email address across to the 'EmailAttach' portion of the code to send the email to that email address.
1.
Code:
Function Get_DB_Values()
'Assumes that you have a Table1 and that Field1 is Text and Field2 is a Number.
' . . . just for testing purposes.
' . . . You should use your actual Table & field names & set your variables to match the data types of your table fields.
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strField1 As String
Dim intField2 as Integer
On Error GoTo Error_Handle
Set db = CurrentDb
'Your strSQL should only return rows of data that meet your date requirements -
'so that you only get email addresses for people whose licenses are almost expired.
'Eg something like this:
'strSQL = "Select * From [Table1] Where [ExpiryDate]> # " & ExpDate & "#;"
strSQL = "Select * From [Table1]"
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
With rs
'This Do While loop goes through all the records in strSQL.
Do While Not rs.EOF
strField1 = rs![Field1]
intField2 = rs![Field2]
'I put this little If Then here so you can look at one value.
'substitute these with your own values.
If intField2 = 5 Then
MsgBox strBranch
End If
.MoveNext 'Move to next record in recordset.
Loop 'Back to 'Do While' to check if we are at the end of the file.
Exit_Get_DB_Values:
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
Set db = Nothing
Exit Function
Error_Handle:
Resume Exit_Get_DB_Values
End With
End Function
2.
Code:
Function EmailAttach() As Integer
Dim strRecipient, strBody, strSubject, astAttach(2) As String
Dim i, intCount, intSend As Integer
Dim oLook, oMail As Object
'You can put the email address that you pull out of Table1 in the previous code example into
' . . . strRecipient - below.
strRecipient = "name@domain.net" 'Put your own email address from your Table here.
strSubject = "Put your subject text here."
strBody = "Put your Body text here."
'I Used the following to create an array of two files to attach.
intSend = True
intAcount = 2
astAttach(0) = "c:\FileName.doc"
astAttach(1) = "c:\FileName.xls"
On Error GoTo errEmailAttach
Set oLook = CreateObject("Outlook.Application")
Set oMail = oLook.CreateItem(0)
With oMail
.To = strRecipient
.Body = strBody
.Subject = strSubject
.ReadReceiptRequested = True 'You can set this to False if you want.
If intCount <> 0 Then
For i = 1 To intCount
.Attachments.Add (astAttach(i - 1))
Next
End If
If intSend = True Then
.Send
Else
.Display
End If
End With
Set oMail = Nothing
Set oLook = Nothing
EmailAttach = True
Exit Function
errEmailAttach:
MsgBox "Error Message: " & Err.Description & Chr$(10) & Chr$(13) & _
"Your email may not have been sent.", vbCritical, "Error"
On Error Resume Next
Set oMail = Nothing
Set oLook = Nothing
EmailAttach = False
End Function
I hope this makes sense. It seems like a really long post!!
Let me know how it goes.