View Full Version : Cancatinate Query


Squeeljunker
02-17-2010, 12:37 AM
I really need some help with this one, I'm not too bad in Access itself but when it comes to writing queries in VB I struggle, and I suspect the only way of doing what I need is with some VB code. Hopefully someone can help. What I want to do is take the attached data & concatinate the values based on where the "Stock Number" is the same so that I end up with a string of text like this

SERVICE, 210000001, WELL INTERVENTIONS, COILED TUBING AND PUMPING, PERSONNEL, PERSONNEL, OFFICE

SERVICE, 210000002, WELL INTERVENTIONS, COILED TUBING AND PUMPING, PERSONNEL, PERSONNEL, OFFICE

Obviously there are some values that are blank depending on what is entered per record & the values need to go in ordered by the Sequence field. The table name is "Attribute Values"

Many thanks in advance

Uncle Gizmo
02-17-2010, 01:27 AM
You may find this thread useful

http://www.access-programmers.co.uk/forums/showthread.php?t=71302

Squeeljunker
02-17-2010, 05:51 AM
Many thanks for the reply, I've had a look but not having much luck figuring it out. I'll have a play again tomorrow and see if I can work it out.

Squeeljunker
02-18-2010, 03:51 AM
Ok, next update to my continuing nightmare of concatination, I have managed to build a module & I think I'm very close to getting the results I'm after, but I just can't get the last bit to work. I have attached a document with everything on it to see if someone can help, I really need to get this working asap so I can get onto my next batch of data. Basically the results (as per the attached document) are very close, it's just not grouping the results to one occurance per unique number (1st Column) and it is building the description using the values from all unique numbers and not limiting it to the values that are only against the unique number (hope that makes sense)

Anyway, if someone could have a look at my code & see where I'm going wrong, I would be forever grateful

Uncle Gizmo
02-18-2010, 04:26 AM
Code Extracted from the Attached Word Doc....

Module

Public Function ConcatRelated(strField As String, _
strTable As String, _
Optional strWhere As String, _
Optional strOrderBy As String, _
Optional strSeparator = ", ") As Variant
On Error GoTo Err_Handler
'Purpose: Generate a concatenated string of related records.
'Return: String variant, or Null if no matches.
'Arguments: strField = name of field to get results from and concatenate.
' strTable = name of a table or query.
' strWhere = WHERE clause to choose the right values.
' strOrderBy = ORDER BY clause, for sorting the values.
' strSeparator = characters to use between the concatenated values.
'Notes: 1. Use square brackets around field/table names with spaces or odd characters.
' 2. strField can be a Multi-valued field (A2007 and later), but strOrderBy cannot.
' 3. Nulls are omitted, zero-length strings (ZLSs) are returned as ZLSs.
' 4. Returning more than 255 characters to a recordset triggers this Access bug:
' http://allenbrowne.com/bug-16.html
Dim rs As DAO.Recordset 'Related records
Dim rsMV As DAO.Recordset 'Multi-valued field recordset
Dim strSql As String 'SQL statement
Dim strOut As String 'Output string to concatenate to.
Dim lngLen As Long 'Length of string.
Dim bIsMultiValue As Boolean 'Flag if strField is a multi-valued field.

'Initialize to Null
ConcatRelated = Null

'Build SQL string, and get the records.
strSql = "SELECT " & strField & " FROM " & strTable
If strWhere <> vbNullString Then
strSql = strSql & " WHERE " & strWhere
End If
If strOrderBy <> vbNullString Then
strSql = strSql & " ORDER BY " & strOrderBy
End If
Set rs = DBEngine(0)(0).OpenRecordset(strSql, dbOpenDynaset)
'Determine if the requested field is multi-valued (Type is above 100.)
bIsMultiValue = (rs(0).Type > 100)

'Loop through the matching records
Do While Not rs.EOF
If bIsMultiValue Then
'For multi-valued field, loop through the values
Set rsMV = rs(0).Value
Do While Not rsMV.EOF
If Not IsNull(rsMV(0)) Then
strOut = strOut & rsMV(0) & strSeparator
End If
rsMV.MoveNext
Loop
Set rsMV = Nothing
ElseIf Not IsNull(rs(0)) Then
strOut = strOut & rs(0) & strSeparator
End If
rs.MoveNext
Loop
rs.Close

'Return the string without the trailing separator.
lngLen = Len(strOut) - Len(strSeparator)
If lngLen > 0 Then
ConcatRelated = Left(strOut, lngLen)
End If

Exit_Handler:
'Clean up
Set rsMV = Nothing
Set rs = Nothing
Exit Function

Err_Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "ConcatRelated()"
Resume Exit_Handler
End Function

namliam
02-18-2010, 04:32 AM
Basically the results (as per the attached document) are very close, it's just not grouping the results to one occurance per unique number (1st Column) and it is building the description using the values from all unique numbers and not limiting it to the values that are only against the unique number (hope that makes sense)

Suggest you look into "grouping" your query using a "group by" clause

Squeeljunker
02-18-2010, 04:36 AM
To be honest the grouping isn't the problem, it's where the description I'm trying to build is not being limited to the unique number from the table so I'm getting a string of text that is made up from every value in the table. If I enter the Group By function I get one line per Stock Number, but the text is still not limited to the values associated to that number.