Hi,
I was wondering if anyone would be so kind to have a look at my code and tell me where I am going wrong. My code has calls two queries the first populates the email add and the subject line and the other query fills the html template I have borrowed a template function to call it from an external source. it sends single emails (1st query) multiple rows in the html (2nd query). It all seems to work fine for the first email and then it just seem to get suck. I have looked everywhere in this forum and all over the internet to no avail.
Any help will be much appreciated
Many Thanks
Diehippy
P.S here is the code
Private Sub Command4_Click()
On Error Resume Next
Dim objOutlook As Object
Dim objEmail As Object
Dim MyDb As DAO.Database
Dim rsEmail As DAO.Recordset
Dim PER As DAO.Recordset
Dim sMessageBody, strCC
Dim apetext As Variant
Dim abdtext As Variant
Set MyDb = CurrentDb()
apetext = MyDb.QueryDefs("absence_parent_email_master").SQL
apetext = Replace(apetext, "@date", "'" & Me.AbsenceDate & "'")
MyDb.QueryDefs("absence_parent_email").SQL = apetext
Set rsEmail = MyDb.OpenRecordset("absence_parent_email", dbOpenSnapshot)
Set objOutlook = CreateObject("Outlook.application", "localhost")
Set objEmail = objOutlook.CreateItem(olMailItem)
sMessageBody = ReturnTemplate("ParentEmail")
abdtext = MyDb.QueryDefs("absence_per_day_master").SQL
abdtext = Replace(abdtext, "@date", "'" & Me.AbsenceDate & "'")
MyDb.QueryDefs("absence_per_day").SQL = abdtext
Set PER = MyDb.OpenRecordset("absence_per_day", dbOpenSnapshot)
'replace variables
sMessageBody = Replace(sMessageBody, "@FORENAME@", PER!FORENAME)
sMessageBody = Replace(sMessageBody, "@SURNAME@", PER!SURNAME)
sMessageBody = Replace(sMessageBody, "@PARENT_TITLE@", PER!PARENT_TITLE)
sMessageBody = Replace(sMessageBody, "@PARENT_FORENAME@", PER!PARENT_FORENAME)
sMessageBody = Replace(sMessageBody, "@PARENT_SURNAME@", PER!PARENT_SURNAME)
sMessageBody = Replace(sMessageBody, "@TIMESTAMP@", Format(Now(), "dddd, d mmmm yyyy at h:nn am/pm"))
sMessageBody = Replace(sMessageBody, "@REG_DATE@", PER!REG_DATE)
sMessageBody = Replace(sMessageBody, "@DAY@", PER!Day)
sMessageBody = Replace(sMessageBody, "@STARTTIME@", PER!STARTTIME)
sMessageBody = Replace(sMessageBody, "@ENDTIME@", PER!ENDTIME)
sMessageBody = Replace(sMessageBody, "@COURSETITLE@", PER!COURSETITLE)
sMessageBody = Replace(sMessageBody, "@REGMARKDESCR@", PER!REGMARKDESCR)
With rsEmail
With PER
.MoveFirst
.MoveFirst
Do Until rsEmail.EOF
Do Until PER.EOF
With objEmail
If IsNull(rsEmail!parent_email) = False Then
.To = rsEmail!parent_email
.SentOnBehalfOfName = "emailaddress"
.Subject = "Student Absence Notification - " & rsEmail!FORENAME & " " & rsEmail!SURNAME & " ID:" & rsEmail!person_code
.Importance = olImportanceHigh
.HTMLbody = sMessageBody
.display
'DoCmd.SendObject acSendNoObject, , , _
'sToName, , , sSubject, HTMLbody, False, False
End If
End With
.MoveNext
.MoveNext
Loop
Loop
End With
End With
Set MyDb = Nothing
Set rsEmail = Nothing
Set PER = Nothing
Set objOutlook = Nothing
Set objEmail = Nothing
End Sub
I was wondering if anyone would be so kind to have a look at my code and tell me where I am going wrong. My code has calls two queries the first populates the email add and the subject line and the other query fills the html template I have borrowed a template function to call it from an external source. it sends single emails (1st query) multiple rows in the html (2nd query). It all seems to work fine for the first email and then it just seem to get suck. I have looked everywhere in this forum and all over the internet to no avail.
Any help will be much appreciated
Many Thanks
Diehippy
P.S here is the code
Private Sub Command4_Click()
On Error Resume Next
Dim objOutlook As Object
Dim objEmail As Object
Dim MyDb As DAO.Database
Dim rsEmail As DAO.Recordset
Dim PER As DAO.Recordset
Dim sMessageBody, strCC
Dim apetext As Variant
Dim abdtext As Variant
Set MyDb = CurrentDb()
apetext = MyDb.QueryDefs("absence_parent_email_master").SQL
apetext = Replace(apetext, "@date", "'" & Me.AbsenceDate & "'")
MyDb.QueryDefs("absence_parent_email").SQL = apetext
Set rsEmail = MyDb.OpenRecordset("absence_parent_email", dbOpenSnapshot)
Set objOutlook = CreateObject("Outlook.application", "localhost")
Set objEmail = objOutlook.CreateItem(olMailItem)
sMessageBody = ReturnTemplate("ParentEmail")
abdtext = MyDb.QueryDefs("absence_per_day_master").SQL
abdtext = Replace(abdtext, "@date", "'" & Me.AbsenceDate & "'")
MyDb.QueryDefs("absence_per_day").SQL = abdtext
Set PER = MyDb.OpenRecordset("absence_per_day", dbOpenSnapshot)
'replace variables
sMessageBody = Replace(sMessageBody, "@FORENAME@", PER!FORENAME)
sMessageBody = Replace(sMessageBody, "@SURNAME@", PER!SURNAME)
sMessageBody = Replace(sMessageBody, "@PARENT_TITLE@", PER!PARENT_TITLE)
sMessageBody = Replace(sMessageBody, "@PARENT_FORENAME@", PER!PARENT_FORENAME)
sMessageBody = Replace(sMessageBody, "@PARENT_SURNAME@", PER!PARENT_SURNAME)
sMessageBody = Replace(sMessageBody, "@TIMESTAMP@", Format(Now(), "dddd, d mmmm yyyy at h:nn am/pm"))
sMessageBody = Replace(sMessageBody, "@REG_DATE@", PER!REG_DATE)
sMessageBody = Replace(sMessageBody, "@DAY@", PER!Day)
sMessageBody = Replace(sMessageBody, "@STARTTIME@", PER!STARTTIME)
sMessageBody = Replace(sMessageBody, "@ENDTIME@", PER!ENDTIME)
sMessageBody = Replace(sMessageBody, "@COURSETITLE@", PER!COURSETITLE)
sMessageBody = Replace(sMessageBody, "@REGMARKDESCR@", PER!REGMARKDESCR)
With rsEmail
With PER
.MoveFirst
.MoveFirst
Do Until rsEmail.EOF
Do Until PER.EOF
With objEmail
If IsNull(rsEmail!parent_email) = False Then
.To = rsEmail!parent_email
.SentOnBehalfOfName = "emailaddress"
.Subject = "Student Absence Notification - " & rsEmail!FORENAME & " " & rsEmail!SURNAME & " ID:" & rsEmail!person_code
.Importance = olImportanceHigh
.HTMLbody = sMessageBody
.display
'DoCmd.SendObject acSendNoObject, , , _
'sToName, , , sSubject, HTMLbody, False, False
End If
End With
.MoveNext
.MoveNext
Loop
Loop
End With
End With
Set MyDb = Nothing
Set rsEmail = Nothing
Set PER = Nothing
Set objOutlook = Nothing
Set objEmail = Nothing
End Sub