Concatenate the contents of the same field across (up to 3 or 4) records (1 Viewer)

rotorque

Registered User.
Local time
Today, 10:09
Joined
Dec 17, 2008
Messages
53
I should have possibly posted this thread on the VB forum, But the truth is there is so much to learn.

I have descriptions (or narratives) that span several records eg:

ID, Name, Date/PersonalID, TextDescription
1, Jon Smith, “2009-01-01/100”, “Training in”
1, Jon Smith, “2009-01-01/100”, “Finance and “
1, Jon Smith, “2009-01-01/100”, “Administration”
2, Sam Brown, “2009-01-01/200”, “Excel Training”
3, Albert Cox, “2009-01-02/500”, “Winning in Sales”
4, Alfi Burns, “2009-01-02/900”, “Finance for Senior ”
4, Alfi Burns, “2009-01-02/900”, “Managers of ”
4, Alfi Burns, “2009-01-02/900”, “Diverse Operational ”
4, Alfi Burns, “2009-01-02/900”, “Teams ”
5, Bill Shakespeare, “2009-01-02/1000”, “Creative Writing”

I need to concatenate the contents of the same field across up to three or four records (sometimes less).

So far I have created field in a table from a date and an personal id. This is necessary because I wish to keep the events on the different dates separate and those on the same date grouped together. The field is of the form “YYYY-MM-DD/PersonalID” as can be seen in the example above. There are thousands of these…

I have assumed that in a query it is not possible to take a value from one record to the next? So I believe I need to use some VB.. in a Module?

I thought a way of doing this would be to add a field that I can increments with the number of occurrences (though I don’t know how to write this code) which would be 4 in the case of Alfi Burns and 3 for Jon Smith etc. and then I would use another query that concatenates (into the last record) the values for occurrences greater than 1. But that is the limit to my understanding what I need to do. As you can see any help would be gratefully received.

Cheers

Following on from my earlier post http://www.access-programmers.co.uk/forums/showthread.php?t=173947

I have made a text based field strEmpDateID (of the form “2009-06-17/A180980”) from which I can isolate the event and the person. I copied VB code to right concatenated values to another table from here. But it doesn’t seem to work is it because I am using a text field to compare the records or is the syntax wrong?

Ideally I'd like the values to be concatenated in a new field in the same table but I am happy to work with whatever I can learn getting this to work. Any suggestions where I need to look?

Thanks




Option Compare Database

Sub Write_To_Grpd()

Dim db As Database
Dim rsGet As Recordset
Dim rsWrite As Recordset
Dim varEmpID As Variant
Dim varNextEmpID As Variant
Dim strBuild As String
Dim intNumElements As Integer

Set db = CurrentDb()
Set rsGet = db.OpenRecordset("SELECT strEmpDateID, strTrngCrs FROM tblAllYrs ORDER BY strEmpDateID, strTrngCrs”)
Set rsWrite = db.OpenRecordset("tblAllYrsGrpd")

With rsGet

Do While Not .EOF
varEmpID = ![strEmpDateID]
.MoveNext
If Not .EOF Then
varNextEmpID = ![strEmpDateID]
Else
varNextEmpID = "EOF"
End If
.MovePrevious

strBuild = strBuild & ![strTrngCrs] & ","
intNumElements = intNumElements + 1

If Not (varOrgID = varNextOrgID) Then
'add record to table
strBuild = Left(strBuild, Len(strBuild) - 1)
With rsWrite
.AddNew
!strEmpDateIDG = rsGet![strEmpDateID]
!strTrngCrsG = strBuild
!lngProcCountG = intNumElements
.Update
End With
're-initialize variables
strBuild = ""
intNumElements = 0
End If
.MoveNext
Loop

End With

rsGet.Close
rsWrite.Close

Set rsGet = Nothing
Set rsWrite = Nothing
Set db = Nothing

End Sub
 
Last edited by a moderator:

DCrake

Remembered
Local time
Today, 10:09
Joined
Jun 8, 2005
Messages
8,632
Not wishing to sound bemused but what is the object of the exercise? You have shown how it looks now, but you have not shown how it will look when its has finshed.

To me what you are attempting to do is not normalised. Can you explain


David
 

rainman89

I cant find the any key..
Local time
Today, 05:09
Joined
Feb 12, 2007
Messages
3,015
Don't think you need vb for this. I believe you can do this in a query.

What you would need to do is create a query that has a count on say the date/personalID. This will return everyones counts.

Then you do what you said with the last part and just make the query return everyone where the count is >1

Edit:

I 2nd what David is saying...

I also have a feeling that you are going to run into trouble with the date field that you have in your tables
 

Atomic Shrimp

Humanoid lifeform
Local time
Today, 10:09
Joined
Jun 16, 2000
Messages
1,954
I think the OP is wanting to end up with:

1, Jon Smith, “2009-01-01/100”, “Training in Finance and Administration"

- where that last field is a concatenation of the values from three records - I don't know of any way to do that in a query.
 

rainman89

I cant find the any key..
Local time
Today, 05:09
Joined
Feb 12, 2007
Messages
3,015
I think the OP is wanting to end up with:

1, Jon Smith, “2009-01-01/100”, “Training in Finance and Administration"

- where that last field is a concatenation of the values from three records - I don't know of any way to do that in a query.


wow!!! Why is it being stored like this then??
 

rotorque

Registered User.
Local time
Today, 10:09
Joined
Dec 17, 2008
Messages
53
Why indeed. I can only imagine it's the output of some other system that has limited capacity text per line.

Spot on! Yes I'm sorry I got rather taken up with the problem rather than explaining what I need to end up with. It is all coming from a legacy system, that is still being used but hopefully soon can be phased out.

1, Jon Smith, “2009-01-01/100”, “Training in Finance and Administration"

Is what I need to end up with. At which point I will make a selection of the full descriptions and create a new table with these records.
 

Users who are viewing this thread

Top Bottom