Good morning, all:
So I'm quite new to VBA and just joined the site today, so I'm not sure I've posted this in the right place.
I created a Combo Box to send group emails. The code is below. It worked great. However, the client wants to be able to send it to multiple groups at once, so I've now changed it to a List Box. Can anyone help me with changing the code so that if she selects more than one group, it will shoot out emails to all of them at once?
Sorry for the constant loops, but I posted this somewhere else and someone had suggested that I condense the code first with SQL...unfortunately I'm not great with that, either, so this was the best way for me to go...so if anyone has a suggestion of how to code it better, I'm not above "cleaning" it up.
So I'm quite new to VBA and just joined the site today, so I'm not sure I've posted this in the right place.
I created a Combo Box to send group emails. The code is below. It worked great. However, the client wants to be able to send it to multiple groups at once, so I've now changed it to a List Box. Can anyone help me with changing the code so that if she selects more than one group, it will shoot out emails to all of them at once?
Sorry for the constant loops, but I posted this somewhere else and someone had suggested that I condense the code first with SQL...unfortunately I'm not great with that, either, so this was the best way for me to go...so if anyone has a suggestion of how to code it better, I'm not above "cleaning" it up.
Code:
Private Sub btnSend_Click()
Dim OlApp As Object
Dim OlMail As Object
Dim rs As DAO.Recordset
Dim ToRecipient As String
Set OlApp = CreateObject("Outlook.Application")
Set OlMail = OlApp.CreateItem(olMailItem)
If Me.cmbGroup.Column(1) = "Berlin Group" Then
Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryBer")
Do While rs.EOF = False
OlMail.Recipients.Add rs!Email
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Else
If Me.cmbGroup.Column(1) = "Mumbai Group" Then
Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryMum")
Do While rs.EOF = False
OlMail.Recipients.Add rs!Email
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Else
If Me.cmbGroup.Column(1) = "Toronto Group" Then
Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryTor")
Do While rs.EOF = False
OlMail.Recipients.Add rs!Email
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Else
If Me.cmbGroup.Column(1) = "Detroit Group" Then
Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryDet")
Do While rs.EOF = False
OlMail.Recipients.Add rs!Email
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Else
If Me.cmbGroup.Column(1) = "Chicago Group" Then
Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryChi")
Do While rs.EOF = False
OlMail.Recipients.Add rs!Email
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Else
If Me.cmbGroup.Column(1) = "Munich Group" Then
Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryMun")
Do While rs.EOF = False
OlMail.Recipients.Add rs!Email
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Else
If Me.cmbGroup.Column(1) = "Delhi Group" Then
Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryDel")
Do While rs.EOF = False
OlMail.Recipients.Add rs!Email
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
End If
End If
End If
End If
End If
End If
End If
OlMail.Subject = " "
OlMail.Display
End Sub