PC Review


Reply
Thread Tools Rate Thread

concatenate several of record in a field to one record

 
 
thread
Guest
Posts: n/a
 
      22nd May 2007
Hi all,
what is the best way to concatenate several of records from a field to
1 record?

 
Reply With Quote
 
 
 
 
Ken Snell \(MVP\)
Guest
Posts: n/a
 
      23rd May 2007
Duane Hookom (MVP) has previously (and many times) posted a generic
concatenate function that may be what you seek:

--

Ken Snell
<MS ACCESS MVP>



Function Concatenate(pstrSQL As String, _
Optional pstrDelim As String = ", ") _
As String
'example
'tblFamily with FamID as numeric primary key
'tblFamMem with FamID, FirstName, DOB,...
'return a comma separated list of FirstNames
'for a FamID
' John, Mary, Susan
'in a Query
'SELECT FamID,
'Concatenate("SELECT FirstName FROM tblFamMem
' WHERE FamID =" & [FamID]) as FirstNames
'FROM tblFamily
'

'======For DAO uncomment next 4 lines=======
'====== comment out ADO below =======
'Dim db As DAO.Database
'Dim rs As DAO.Recordset
'Set db = CurrentDb
'Set rs = db.OpenRecordset(pstrSQL)

'======For ADO uncomment next two lines=====
'====== comment out DAO above ======
Dim rs As New ADODB.Recordset
rs.Open pstrSQL, CurrentProject.Connection, _
adOpenKeyset, adLockOptimistic
Dim strConcat As String 'build return string
With rs
If Not .EOF Then
.MoveFirst
Do While Not .EOF
strConcat = strConcat & _
.Fields(0) & pstrDelim
.MoveNext
Loop
End If
.Close
End With
Set rs = Nothing
'====== uncomment next line for DAO ========
'Set db = Nothing
If Len(strConcat) > 0 Then
strConcat = Left(strConcat, _
Len(strConcat) - Len(pstrDelim))
End If
Concatenate = strConcat
End Function



"thread" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi all,
> what is the best way to concatenate several of records from a field to
> 1 record?
>



 
Reply With Quote
 
thread
Guest
Posts: n/a
 
      23rd May 2007
thanks
Ken Snell (MVP) :
> Duane Hookom (MVP) has previously (and many times) posted a generic
> concatenate function that may be what you seek:
>
> --
>
> Ken Snell
> <MS ACCESS MVP>
>
>
>
> Function Concatenate(pstrSQL As String, _
> Optional pstrDelim As String = ", ") _
> As String
> 'example
> 'tblFamily with FamID as numeric primary key
> 'tblFamMem with FamID, FirstName, DOB,...
> 'return a comma separated list of FirstNames
> 'for a FamID
> ' John, Mary, Susan
> 'in a Query
> 'SELECT FamID,
> 'Concatenate("SELECT FirstName FROM tblFamMem
> ' WHERE FamID =" & [FamID]) as FirstNames
> 'FROM tblFamily
> '
>
> '======For DAO uncomment next 4 lines=======
> '====== comment out ADO below =======
> 'Dim db As DAO.Database
> 'Dim rs As DAO.Recordset
> 'Set db = CurrentDb
> 'Set rs = db.OpenRecordset(pstrSQL)
>
> '======For ADO uncomment next two lines=====
> '====== comment out DAO above ======
> Dim rs As New ADODB.Recordset
> rs.Open pstrSQL, CurrentProject.Connection, _
> adOpenKeyset, adLockOptimistic
> Dim strConcat As String 'build return string
> With rs
> If Not .EOF Then
> .MoveFirst
> Do While Not .EOF
> strConcat = strConcat & _
> .Fields(0) & pstrDelim
> .MoveNext
> Loop
> End If
> .Close
> End With
> Set rs = Nothing
> '====== uncomment next line for DAO ========
> 'Set db = Nothing
> If Len(strConcat) > 0 Then
> strConcat = Left(strConcat, _
> Len(strConcat) - Len(pstrDelim))
> End If
> Concatenate = strConcat
> End Function
>
>
>
> "thread" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hi all,
> > what is the best way to concatenate several of records from a field to
> > 1 record?
> >


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
concatenate several of records in a field to 1 record thread Microsoft Access 2 23rd May 2007 08:30 AM
concatenate several of records in a field to 1 record thread Microsoft Access 0 22nd May 2007 07:09 PM
showing record totals from one field based on current record criteria chanchito Microsoft Access 7 25th Oct 2006 04:05 AM
Concatenate content of text field to a master record =?Utf-8?B?RWR3YXJkIEMgRCdTb3V6YQ==?= Microsoft Access 1 3rd Jun 2005 06:53 AM
Making Record By record and Field By Field Calculations Faster Preying Mentis Microsoft Access 1 7th Apr 2004 03:03 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:25 AM.