add users to email who are selected with checkbox (1 Viewer)

megatronixs

Registered User.
Local time
Today, 01:26
Joined
Aug 17, 2012
Messages
719
Hi all,

I have a form with a subform in it that gets feed by a query. I have in the subform as datasheet view the participants for a training (connected with the event_id). I use it as well to send email to all the participants.
I created a new functionality that will check if the participant did attend the training, if not, then there is a check box that stays blank. I would like to loop now trough the list and send only email to the team leaders where the check box is blank. I tried in the below code to, but the checkbox is always -1. The subform name is: "frm_recipient_subform" and the checkbox: "attendance"
This is the code I have so far:
Code:
Private Sub btn_unconfirmed_absence_Click()
Dim objItem             As Object
    Dim db                  As DAO.Database
    Dim MailList            As DAO.Recordset
    Dim oMail               As Outlook.MailItem
    Dim oApp                As Object
    Dim create_event_table As Recordset
    Set create_event_table = CurrentDb.OpenRecordset("tbl_create_event")
    Set oMail = objItem
    Set oApp = CreateObject("Outlook.application")
    Set oMail = oApp.CreateItem(olMailItem)
    Set db = CurrentDb()
    Set MailList = db.OpenRecordset("MyEmailAddresses")
    Dim training_name_text As String
    Dim yesno As String

' this is where we loop through our list of addresses,
' adding them to e-mails and sending them.
Do Until MailList.EOF

        ' This creates the e-mail
        ' We need to move it BEFORE we start the loop, since
        ' we don't want to make a bunch of e-mails, we just want one.
        ' this is where we loop through our list of addresses,
        ' and we add them to the RECIPIENTS collection
        Do Until MailList.EOF
            ' This adds the address to the list of recipients
If Forms!frm_created_event_form!frm_recipient_subform.Form!attendance = 0 Then
            oMail.Recipients.Add MailList("E-Mail Address")
End If
            'OnError GoToNext
        'And on to the next one...
        MailList.MoveNext
 
Loop
            'And now that we've addressed it, we can finish composing the rest of the fields.
            'This gives it a subject, sent on behalf of
                oMail.Subject = "Canceled Training: " & Me.training_name_text
                'oMail.SentOnBehalfOfName = "on behalf from"
                'This gives it the body
                oMail.HTMLBody = "<HTML><HEAD><Font Size= 5><style> table, th, td </style> </HEAD> <BODY><br><p>" & _
                "*** This is an automatically generated email, please do not reply ***<br><br>" & _
                "Please be aware that the below training has been canceled:<br><br>" & _
                "<TABLE><TR><TD>Training Name:</TD><TD>" & Me.training_name_text & "</TD></TR><TR>" & _
                "<TD>Date:</TD><TD>" & Me.training_date_start & " - " & Me.training_end_date & "</TD></TR><TR>" & _
                "<TD>Training Location:</TD><TD>" & Me.training_location_text & "</TD></TR><TR>" & _
                "<TD>Start Time:</TD><TD>" & Me.training_time_start & "</TD></TR><TR>" & _
                "<TD>End Time:</TD><TD>" & Me.training_end_time & "</TD></TR><TR>" & _
                "<TD>Trainer:</TD><TD>" & Me.trainer_1_name_text & "</TD></TR><TR></TD></TABLE> </BODY> </HTML>"
'This displays it!
    oMail.Display
Loop
'Cleanup after ourselves
    Set oApp = Nothing
    MailList.Close
    Set MailList = Nothing
    db.Close
    Set db = Nothing
End Sub
(the email body I still need to change)

Greetings.
 

Minty

AWF VIP
Local time
Today, 00:26
Joined
Jul 26, 2013
Messages
10,368
Is the checkbox unbound? If it is it will be the same value on every record. You need to add the checkbox as a field to your underlying query, then you can set it per record.
 

megatronixs

Registered User.
Local time
Today, 01:26
Joined
Aug 17, 2012
Messages
719
Hi Minty,

The query is based on a few tables and in one of the tables there is the checkbox for the attendance.
Maybe I missed something.

Greetings.
 

Minty

AWF VIP
Local time
Today, 00:26
Joined
Jul 26, 2013
Messages
10,368
Your loop structures look a bit suspect to me you appear to be doing the same loop twice at the beginning?
 

megatronixs

Registered User.
Local time
Today, 01:26
Joined
Aug 17, 2012
Messages
719
hi,

I copied most of the code from internet, no clue if it was fine or not, it simply works

Greetings.
 

Minty

AWF VIP
Local time
Today, 00:26
Joined
Jul 26, 2013
Messages
10,368
Having had a slightly closer look, I can't see how the MyEmailAddresses table is linked to your attendance records on your form.

Why don't you bring the email address in to your underlying query for the form, then everything is in one place.

You should always try and work out what downloaded code is doing, for instance it's generally considered poor practice to pile a load of email addresss into the To: field as it exposes all of them to everyone you send it to. Much better to use the Bcc: field where the addresses as hidden from the recipients.
 

megatronixs

Registered User.
Local time
Today, 01:26
Joined
Aug 17, 2012
Messages
719
Hi,
I managed to solve it :)
I had a better look at the loop and then the parts of it, I took one of the loops out and it worked just fin, then played with the code till it was just right. I also found where the checkbox came from and getting the real value from it. As you can see from the below code, it is fixed now :)

Code:
[FONT="Arial"]Private Sub btn_unconfirmed_absence_Click()[/FONT]
[FONT="Arial"] [/FONT]
[FONT="Arial"]    Dim objItem                    As Object[/FONT]
[FONT="Arial"]    Dim db                           As DAO.Database[/FONT]
[FONT="Arial"]    Dim MailList                     As DAO.Recordset[/FONT]
[FONT="Arial"]    Dim oMail                        As Outlook.MailItem[/FONT]
[FONT="Arial"]    Dim oApp                        As Object[/FONT]
[FONT="Arial"]    Dim create_event_table     As Recordset[/FONT]
[FONT="Arial"] [/FONT]
[FONT="Arial"]    Set create_event_table = CurrentDb.OpenRecordset("tbl_create_event")[/FONT]
[FONT="Arial"]    Set oMail = objItem[/FONT]
[FONT="Arial"]    Set oApp = CreateObject("Outlook.application")[/FONT]
[FONT="Arial"]    Set oMail = oApp.CreateItem(olMailItem)[/FONT]
[FONT="Arial"]    Set db = CurrentDb()[/FONT]
[FONT="Arial"]    Set MailList = db.OpenRecordset("MyEmailAddresses")[/FONT]
[FONT="Arial"]    Dim training_name_text As String[/FONT]
[FONT="Arial"]    Dim yesno As String[/FONT]
[FONT="Arial"] [/FONT]
[FONT="Arial"]        ' This creates the e-mail[/FONT]
[FONT="Arial"]        ' We need to move it BEFORE we start the loop, since[/FONT]
[FONT="Arial"]        ' we don't want to make a bunch of e-mails, we just want one.[/FONT]
[FONT="Arial"] [/FONT]
[FONT="Arial"]        ' this is where we loop through our list of addresses,[/FONT]
[FONT="Arial"]        ' and we add them to the RECIPIENTS collection[/FONT]
[FONT="Arial"] [/FONT]
[FONT="Arial"]        Do Until MailList.EOF[/FONT]
[FONT="Arial"] [/FONT]
[FONT="Arial"]            ' This adds the address to the list of recipients if the attendance check box is false[/FONT]
[FONT="Arial"] [/FONT]
[FONT="Arial"]If MailList("attendance") = False Then[/FONT]
[FONT="Arial"] [/FONT]
[FONT="Arial"]            oMail.Recipients.Add MailList("E-Mail Address")[/FONT]
[FONT="Arial"] End If[/FONT]
[FONT="Arial"] [/FONT]
[FONT="Arial"]        'And on to the next one...[/FONT]
[FONT="Arial"]        MailList.MoveNext[/FONT]
[FONT="Arial"]      [/FONT]
[FONT="Arial"]Loop[/FONT]
[FONT="Arial"]            'And now that we've addressed it, we can finish composing the rest of the fields.[/FONT]
[FONT="Arial"] [/FONT]
[FONT="Arial"]            'This gives it a subject, sent on behalf of[/FONT]
[FONT="Arial"]                oMail.Subject = "Canceled Training: " & Me.training_name_text[/FONT]
[FONT="Arial"]                'oMail.SentOnBehalfOfName = "on behalf from"[/FONT]
[FONT="Arial"] [/FONT]
[FONT="Arial"]                'This gives it the body[/FONT]
[FONT="Arial"]                oMail.HTMLBody = "<HTML><HEAD><Font Size= 5><style> table, th, td </style> </HEAD> <BODY><br><p>" & _[/FONT]
[FONT="Arial"]                "*** This is an automatically generated email, please do not reply ***<br><br>" & _[/FONT]
[FONT="Arial"]                "Please be aware that the below training has been canceled:<br><br>" & _[/FONT]
[FONT="Arial"]                "<TABLE><TR><TD>Training Name:</TD><TD>" & Me.training_name_text & "</TD></TR><TR>" & _[/FONT]
[FONT="Arial"]                "<TD>Date:</TD><TD>" & Me.training_date_start & " - " & Me.training_end_date & "</TD></TR><TR>" & _[/FONT]
[FONT="Arial"]                "<TD>Training Location:</TD><TD>" & Me.training_location_text & "</TD></TR><TR>" & _[/FONT]
[FONT="Arial"]                "<TD>Start Time:</TD><TD>" & Me.training_time_start & "</TD></TR><TR>" & _[/FONT]
[FONT="Arial"]                "<TD>End Time:</TD><TD>" & Me.training_end_time & "</TD></TR><TR>" & _[/FONT]
[FONT="Arial"]                "<TD>Trainer:</TD><TD>" & Me.trainer_1_name_text & "</TD></TR><TR></TD></TABLE> </BODY> </HTML>"[/FONT]
[FONT="Arial"] [/FONT]
[FONT="Arial"]'This displays it![/FONT]
[FONT="Arial"]    oMail.Display[/FONT]
[FONT="Arial"] [/FONT]
[FONT="Arial"]'Cleanup after ourselves[/FONT]
[FONT="Arial"]    Set oApp = Nothing[/FONT]
[FONT="Arial"]    MailList.Close[/FONT]
[FONT="Arial"]    Set MailList = Nothing[/FONT]
[FONT="Arial"]    db.Close[/FONT]
[FONT="Arial"]    Set db = Nothing[/FONT]
[FONT="Arial"]End Sub[/FONT][FONT="Arial"][/FONT]

Greetings.
 

Users who are viewing this thread

Top Bottom