Kemidan2014
Board Regular
- Joined
- Apr 4, 2022
- Messages
- 229
- Office Version
- 365
- Platform
- Windows
I am working on some VBA code for a button to create an Email with a simple message to email multiple people in our organization as a reminder for some action to be taken.
what i have so far works but it is skipping 2 of 6 emails that meet the criteria i am asking for. and i do not understand why.
The goal. Populate the "To" field of my outlook email with email addresses based on matching values
In my form i have a drop down combo box called hotzone. the list values match the TABLE i am pulling emails from which has a column also labelled Hotzone.
basically im asking it "if record in Emaillist table column Hotzone matches form hotzone field then put the email address in the "To" field.
This is still in its building and testing phase. this code is doing what i am asking it to which is when i switch the hotzone field in the form its giving me different emails that match the criteria. but not ALL the emails that match the criteria. did Debug.print and the immediate window matches the To field.
what i have so far works but it is skipping 2 of 6 emails that meet the criteria i am asking for. and i do not understand why.
The goal. Populate the "To" field of my outlook email with email addresses based on matching values
In my form i have a drop down combo box called hotzone. the list values match the TABLE i am pulling emails from which has a column also labelled Hotzone.
basically im asking it "if record in Emaillist table column Hotzone matches form hotzone field then put the email address in the "To" field.
This is still in its building and testing phase. this code is doing what i am asking it to which is when i switch the hotzone field in the form its giving me different emails that match the criteria. but not ALL the emails that match the criteria. did Debug.print and the immediate window matches the To field.
VBA Code:
Private Sub Command258_Click()
Dim O As Outlook.Application
Dim M As Outlook.MailItem
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim address As String
Set db = CurrentDb
Set rst = CurrentDb.OpenRecordset("Select * From emaillist")
'Dim msg As String
'msg = "Insert complicated message here"
Set O = New Outlook.Application
Set M = O.CreateItem(olMailItem)
With rst
Do While Not .EOF
If rst!hotzone = Me.hotzone Then
address = address & rst!Email & ";"
rst.MoveNext
End If
rst.MoveNext
Loop
End With
Debug.Print address
With M
.To = address
.BodyFormat = olFormatHTML
.HTMLBody = "Test Message" & Me.hotzone
.Subject = "Attention Out of Spec Condition"
'.Send
.Display
End With
Set strto = Nothing
Set M = Nothing
Set O = Nothing
End Sub