comma separated list

  • Thread starter Thread starter nilhguoc
  • Start date Start date
N

nilhguoc

Hello,
I have a table, "tblEmail" that contains email addresses
"primaryEmail." I would like to create a report that lists out all
emails in a comma separated list. Can anyone help me with this?

Thanks in advance!
 
This function should get you started. Create a new module and name it
something like basBulkEMail (just don't give it the same name as any existing
subroutine or function). You may need to add a reference (Tools > References)
to the "Microsoft DAO 3.6 Object Library", if you do not already have this
library selected. After saving the module, try compiling your project: Debug
Compile ProjectName. Correct any compile errors that you might find. If the
option becomes disabled, then this is a good sign; it means that your code
compiled without any errors.

You can test it out by opening the Immediate Window (Ctrl G) and entering:

?BulkEMail

Then try adding a textbox to the report. Set it's Can Grow property to Yes.
Set the Can Grow property for the section to Yes as well. Then set the
control source for the text box to: =BulkEMail()



Option Compare Database
Option Explicit

Function BulkEmail() As String
On Error GoTo ProcError

'Purpose: Return a string containing all the email addresses to mail to.
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim strOut As String
Dim lngLen As Long
Const conSEP = ";"

strSQL = "SELECT primaryEmail " _
& "FROM tblEmail " _
& "WHERE primaryEmail Is Not Null;"

Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL)

With rs
Do While Not .EOF
strOut = strOut & ![ContactName] & conSEP
.MoveNext
Loop
End With

lngLen = Len(strOut) - Len(conSEP)

If lngLen > 0 Then
BulkEmail = Left$(strOut, lngLen)
End If

' Debug.Print BulkEmail

ExitProc:
On Error Resume Next
rs.Close: Set rs = Nothing
db.Close: Set db = Nothing
Exit Function

ProcError:
MsgBox Err.Number & ": " & Err.Description, _
vbCritical, "Error in BulkEmail function..."
Resume ExitProc
End Function


Tom Wickerath
Microsoft Access MVP
https://mvp.support.microsoft.com/profile/Tom
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
 
code: LoopAndCombine
---


~~~
How to Create a General Module

1. from the database window, click on the Module tab
2. click on the NEW command button
3. type (or paste) the code in

once the code is in the module sheet, do
Debug,Compile from the menu

if there are no syntax/reference errors, nothing will appear to happen
-- this is good ;)

Make sure to give the module a good name when you save it. You can have
several procedures in a module, so I like to group them.

this function goes into a general module

'~~~~~~~~~~~~~~~~

Function LoopAndCombine( _
pTablename As String, _
pIDFieldname As String, _
pTextFieldname As String, _
pValueID As Long, _
Optional pWhere As String, _
Optional pDeli As String, _
Optional pNoValue as string) As String

'crystal 8-3-06
'strive4peace2006 at yahoo dot com

'NEEDS REFERENCE
'Microsoft DAO Library

'PARAMETERS
'pTablename --> tablename to get list from
'pIDFieldname --> fieldname to link on (ie: "BookID")
'pTextFieldname --> fieldname to combine (ie: "PageNumber")
'pValueID --> actual value of ID for this iteration ( ie: [BookID])
'pWhere, Optional --> more criteria (ie: "Year(PubDate) = 2006")
'pDeli, Optional --> delimiter other than comma (ie: ";")
'pNoValue, Optional --> value to use if no data (ie: "No Pages")

'Set up error handler
On Error GoTo Proc_Err

'dimension variables
Dim r As dao.Recordset, mAllValues As String, S As String
Dim mValueDeli As String

If Len(nz(pDeli,"")) > 0 Then _
mValueDeli = pDeli Else mValueDeli = ","

mAllValues = ""

S = "SELECT [" & pTextFieldname & "] " _
& " FROM [" & pTablename & "]" _
& " WHERE [" & pIDFieldname _
& "] = " & pValueID _
& IIf(Len(pWhere) > 0, " AND " & pWhere, "") _
& ";"

'open the recordset
Set r = CurrentDb.OpenRecordset(S, dbOpenSnapshot)

'loop through the recordset until the end
Do While Not r.EOF
If Not IsNull(r(pTextFieldname)) Then

'~~~~~~~~~~~~~~~~~~~~~~~~~ CHOOSE ONE

'---- if fieldname is numeric
'mAllValues = mAllValues _
& " " & r(pTextFieldname) & mValueDeli

'---- if fieldname is text
mAllValues = mAllValues _
& " '" & r(pTextFieldname) & "'" & mValueDeli

'~~~~~~~~~~~~~~~~~~~~~~~~~
End If
r.MoveNext
Loop

If len(mAllValues) = 0 then
mAllValues = nz(pNoValue,"")
end if


Proc_Exit:
'close the recordset
r.Close
'release the recordset variable
Set r = Nothing

LoopAndCombine = Trim(mAllValues)
Exit Function

'if there is an error, the following code will execute
Proc_Err:
MsgBox Err.Description, , _
"ERROR " & Err.Number _
& " LoopAndCombine"

'press F8 to step through code and debug
'remove next line after debugged
Stop: Resume
Resume Proc_Exit
End Function

'~~~~~~~~~~~~~~~~
pTablename As String --> "[tblEmail]"
(name of the table containing this information)
pIDFieldname As String --> "[PeopleID]"
(name of Primary Key field, assumed to be long integer)
pTextFieldname As String --> "[primaryEmail]"
(the name of the filed with the email address (assumed to be text)
pValueID As Long --> [PeopleID]
(value of Primary Key field you want to combine)

Optional pWhere As String --> if you want to further specify records
within a matching Supplier Code, if not --> just put comma to skip
argument if you are specifying optional arguments after this
Optional pDeli As String -- optional if you want something in
addition to space around each value -- a comma is used is nothing is
specified
Optional pNoValue as string --> what to return if there are no records

'~~~~~~~~~~~~~~~~

so, in a query, here is what you would do (we will skip the optional
arguments:

AllEmail: LoopAndCombine(
"[tblEmail]",
"[PeopleID]",
"[primaryEmail]",
[PeopleID])

~~~~~~~~~~~~~~~~~~~~`

in a report or on a form:
ControlSource -->
=LoopAndCombine("[tblEmail]","[PeopleID]","[primaryEmail]",[PeopleID])


Warm Regards,
Crystal
*
(: have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*
 
Thanks for your help! However, I am getting the error: 3265 Item not
found in this collection, and am not sure what this means.

This function should get you started. Create a new module and name it
something like basBulkEMail (just don't give it the same name as any existing
subroutine or function). You may need to add a reference (Tools > References)
to the "Microsoft DAO 3.6 Object Library", if you do not already have this
library selected. After saving the module, try compiling your project: Debug> Compile ProjectName. Correct any compile errors that you might find. If the

option becomes disabled, then this is a good sign; it means that your code
compiled without any errors.

You can test it out by opening the Immediate Window (Ctrl G) and entering:

?BulkEMail

Then try adding a textbox to the report. Set it's Can Grow property to Yes.
Set the Can Grow property for the section to Yes as well. Then set the
control source for the text box to: =BulkEMail()

Option Compare Database
Option Explicit

Function BulkEmail() As String
On Error GoTo ProcError

'Purpose: Return a string containing all the email addresses to mail to.
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim strOut As String
Dim lngLen As Long
Const conSEP = ";"

strSQL = "SELECT primaryEmail " _
& "FROM tblEmail " _
& "WHERE primaryEmail Is Not Null;"

Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL)

With rs
Do While Not .EOF
strOut = strOut & ![ContactName] & conSEP
.MoveNext
Loop
End With

lngLen = Len(strOut) - Len(conSEP)

If lngLen > 0 Then
BulkEmail = Left$(strOut, lngLen)
End If

' Debug.Print BulkEmail

ExitProc:
On Error Resume Next
rs.Close: Set rs = Nothing
db.Close: Set db = Nothing
Exit Function

ProcError:
MsgBox Err.Number & ": " & Err.Description, _
vbCritical, "Error in BulkEmail function..."
Resume ExitProc
End Function

Tom Wickerath
Microsoft Access MVPhttps://mvp.support.microsoft.com/profile/Tomhttp://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________

Hello,
I have a table, "tblEmail" that contains email addresses
"primaryEmail." I would like to create a report that lists out all
emails in a comma separated list. Can anyone help me with this?
Thanks in advance!
 
Back
Top