Results 1 to 5 of 5
  1. #1
    Imagemo is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    May 2019
    Posts
    9

    Emailing Groups through Multiple Selections with List Box

    Hello...

    So recently I set up a combo box that could email out items to a group. The client wants it changed so that she can select multiple groups and email them at the same time. Is there an easy way of going from a combo box to a list box without having to change too much of the code. The code that I used with the combo box is below:

    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.lstGroup.Column(1) = "Group A" Then
    Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryA")
    Do While rs.EOF = False
        OlMail.Recipients.Add rs!Email
        rs.MoveNext
    Loop
    rs.Close
    Set rs = Nothing
    Else
    If Me.lstGroup.Column(1) = "Group B" Then
    Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryB")
    Do While rs.EOF = False
        OlMail.Recipients.Add rs!Email
        rs.MoveNext
    Loop
    rs.Close
    Set rs = Nothing
    Else
    If Me.lstGroup.Column(1) = "Group C" Then
    Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryC")
    Do While rs.EOF = False
        OlMail.Recipients.Add rs!Email
        rs.MoveNext
    Loop
    rs.Close
    Set rs = Nothing
    Else
    If Me.lstGroup.Column(1) = "Group D" Then
    Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryD")
    Do While rs.EOF = False
        OlMail.Recipients.Add rs!Email
        rs.MoveNext
    Loop
    rs.Close
    Set rs = Nothing
    Else
    If Me.lstGroup.Column(1) = "Group E" Then
    Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryE")
    Do While rs.EOF = False
        OlMail.Recipients.Add rs!Email
        rs.MoveNext
    Loop
    rs.Close
    Set rs = Nothing
    Else
    If Me.lstGroup.Column(1) = "Group F" Then
    Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryF")
    Do While rs.EOF = False
        OlMail.Recipients.Add rs!Email
        rs.MoveNext
    Loop
    rs.Close
    Set rs = Nothing
    Else
    If Me.lstGroup.Column(1) = "Group G" Then
    Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryG")
    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


  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    You just need to adapt your existing code to include a loop through the ItemsSelected collection of the listbox: https://docs.microsoft.com/en-us/off....itemsselected

    Cheers,
    Vlad

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Have to loop through listbox SelectedItems to reference each selected item. An example of looping code in http://allenbrowne.com/ser-50.html

    Inside your loop you would have code to open and loop recordset using the SelectedItem as filter criteria.

    Your code would be easier to read if you used indentation.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    Imagemo is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    May 2019
    Posts
    9
    So I set up a "For Each" Loop around my previous code...and it does open Outlook, but it seems to be sending it only to the same group several times. I'm sure it's because I'm not properly coding the loop, but I can't figure out where.

    Code:
    Private Sub btnSend_Click()
    
    Dim OlApp As Object
    Dim OlMail As Object
    Dim rs As DAO.Recordset
    Dim ToRecipient As String
    Dim varItem As Variant
      
    Set OlApp = CreateObject("Outlook.Application")
    Set OlMail = OlApp.CreateItem(olMailItem)
    
    
    For Each varItem In Me.lstGroup.ItemsSelected
        If Me.lstGroup.Column(1) = "Group A" Then
            Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryA")
            Do While rs.EOF = False
            OlMail.Recipients.Add rs!Email
            rs.MoveNext
        Loop
        rs.Close
        Set rs = Nothing
        Else
            If Me.lstGroup.Column(1) = "Group B" Then
            Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryB")
            Do While rs.EOF = False
            OlMail.Recipients.Add rs!Email
            rs.MoveNext
            Loop
            rs.Close
            Set rs = Nothing
            Else
                If Me.lstGroup.Column(1) = "Group C" Then
                Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryC")
                Do While rs.EOF = False
                OlMail.Recipients.Add rs!Email
                rs.MoveNext
                Loop
                rs.Close
                Set rs = Nothing
                Else
                    If Me.lstGroup.Column(1) = "Group D" Then
                    Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryD")
                    Do While rs.EOF = False
                    OlMail.Recipients.Add rs!Email
                    rs.MoveNext
                    Loop
                    rs.Close
                    Set rs = Nothing
                    Else
                        If Me.lstGroup.Column(1) = "Group E" Then
                        Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryE")
                        Do While rs.EOF = False
                        OlMail.Recipients.Add rs!Email
                        rs.MoveNext
                        Loop
                        rs.Close
                        Set rs = Nothing
                        Else
                            If Me.lstGroup.Column(1) = "Group F" Then
                            Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryF")
                            Do While rs.EOF = False
                            OlMail.Recipients.Add rs!Email
                            rs.MoveNext
                            Loop
                            rs.Close
                            Set rs = Nothing
                            Else
                                If Me.lstGroup.Column(1) = "Group G" Then
                                Set rs = CurrentDb.OpenRecordset("SELECT Email FROM qryG")
                                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
    Next varItem
    
    
    OlMail.Subject = " "
    OlMail.Display
    
    
    
    
    End Sub

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    This code now looks very familiar. Suggested much simpler structure in your other thread. Modified to use listbox:

    Code:
    Dim OlApp As Object
    Dim OlMail As Object
    Dim rs As DAO.Recordset
    Dim varItem As Variant
      
    Set OlApp = CreateObject("Outlook.Application")
    Set OlMail = OlApp.CreateItem(olMailItem)
    With Me.lstGroup
        For Each varItem In .ItemsSelected
            If Not IsNull(varItem) Then
                Set rs = CurrentDb.OpenRecordset("SELECT Email FROM tableORquery WHERE Group = '" & .ItemData(varItem) & "'")
                Do While rs.EOF = False
                    OlMail.Recipients.Add rs!Email
                    rs.MoveNext
                Loop
                rs.Close
            End If
        Next
    End With
    OlMail.Subject = " "
    OlMail.Display
    Set rs = Nothing
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Emailing Groups with Combo Box in VBA
    By Imagemo in forum Access
    Replies: 7
    Last Post: 05-23-2019, 11:04 AM
  2. Replies: 3
    Last Post: 12-14-2016, 03:14 AM
  3. Handling Multiple Selections in a List Box
    By Zetony in forum Access
    Replies: 2
    Last Post: 11-02-2011, 02:21 PM
  4. Replies: 2
    Last Post: 05-27-2011, 08:12 AM
  5. Multiple Selections from a List Box
    By oleBucky in forum Forms
    Replies: 4
    Last Post: 05-06-2011, 08:24 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums