473,435 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,435 software developers and data experts.

Trouble with concatenated string function

I'm using the fConcatChild function posted at
http://www.mvps.org/access/modules/mdl0004.htm to return a field from
the Many table of a 1:M relationship into a concatenated string. The
function puts everything in a comma separated format. In my case, it
returns a list of authors:

Borman, W., Oppler, S., White, L.,

I'd like to change the function so the last record in the string is
preceded by an ampersand instead of a comma, as in:

Borman, W., Oppler, S., & White, L.

(Note: Each last name and initial is actually a concatenated field done
in a query, using: "Author: [LName] & ", " & [Initials]"). In other
words, some of those commas are mine.)

I've tried to edit the function to use the ampersand. Within the
loop, I've added code to keep track of what the current record is so
that once I've finished the loop, I will know the last record added
to the string (i.e., the last author). I then try to remove the entry,
add an ampersand, then add the last record back.

The change I made correctly added the ampersand and the last record,
but it didn't delete the last record. In other words, the last author
is repeated twice, as in:

Borman, W., Oppler, S., White, L., & White, L.

It appears that the code to remove the last record plus the trailing
"," is
either not doing anything, or is being killed:

'Remove the last record added plus the trailing ","
fConcatChild = Left(varConcat, Len(varCurrRecord) + 1)

I know this because I made major changes to this syntax and it had no
observable effect. The next line of code works fine:

'Add the ampersand and the last record
fConcatChild = varConcat & "& " & varCurrRecord

.... but when I bebug this with:

Debug.Print "varConcat: " & varConcat, "varCurrRecord: " &
varCurrRecord

.... I get

varConcat: Borman, W., Hanson, M., Oppler, S., & White, L.,
varCurrRecord: White, L.

.... which confirms that the last record is not being deleted.

Any ideas? I've tried everything and my brain is melting. Thanks.
(Full code below.)

Kurt
'************ Code Start **********
'This code was originally written by Dev Ashish
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish
'
Function fConcatChild(strChildTable As String, _
strIDName As String, _
strFldConcat As String, _
strIDType As String, _
varIDvalue As Variant) _
As String
'Returns a field from the Many table of a 1:M relationship
'in a semi-colon separated format.
'
'Usage Examples:
' ?fConcatChild("Order Details", "OrderID", "Quantity", _
"Long", 10255)
'Where Order Details = Many side table
' OrderID = Primary Key of One side table
' Quantity = Field name to concatenate
' Long = DataType of Primary Key of One Side Table
' 10255 = Value on which return concatenated Quantity
'
Dim db As Database
Dim rs As Recordset
Dim varCurrRecord As Variant
Dim varConcat As Variant
Dim strCriteria As String, strSQL As String
On Error GoTo Err_fConcatChild

varConcat = Null
Set db = CurrentDb
strSQL = "Select [" & strFldConcat & "] From [" & strChildTable &
"]"
strSQL = strSQL & " Where "

Select Case strIDType
Case "String":
strSQL = strSQL & "[" & strIDName & "] = '" & varIDvalue &
"'"
Case "Long", "Integer", "Double": 'AutoNumber is Type Long
strSQL = strSQL & "[" & strIDName & "] = " & varIDvalue
Case Else
GoTo Err_fConcatChild
End Select

Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)

'Are we sure that 'sub' records exist
With rs
If .RecordCount <> 0 Then
'start concatenating records
Do While Not rs.EOF
varCurrRecord = rs(strFldConcat)
varConcat = varConcat & varCurrRecord & ", "
.MoveNext
Loop
End If
End With

'That's it... you should have a concatenated string now
'Remove the last record added plus the trailing ","
fConcatChild = Left(varConcat, Len(varCurrRecord) + 1)
'Add the ampersand and the last record
fConcatChild = varConcat & "& " & varCurrRecord

Exit_fConcatChild:
Set rs = Nothing: Set db = Nothing
Exit Function
Err_fConcatChild:
Resume Exit_fConcatChild
End Function

'************ Code End **********

Dec 14 '05 #1
4 2959

I use a function I wrote a few years back for doing this that I call
"TableAsString".

A quick hack of my "TableAsString" function produced this, which does
what you are asking for:

Public Function TableAsString( _
strTableName As String, _
strFieldName As String, _
strSeperator As String, _
Optional bolTextQualifiers As Boolean = True, _
Optional strOrderByField As String = "", _
Optional strWhere As String = "", _
Optional whatDB As DAO.Database = Nothing, _
Optional strLastSeperator As String = "") _
As String

Dim myDB As DAO.Database
Dim myRST As DAO.Recordset
Dim strSQL As String
Dim strReturnString As String
Dim bolISetTheDB As Boolean

If strLastSeperator = "" Then _
strLastSeperator = strSeperator

If whatDB Is Nothing Then
Set whatDB = CurrentDb
bolISetTheDB = True
End If

strSQL = "SELECT [" & strFieldName & "]" & _
"FROM [" & strTableName & "]"
If strWhere <> "" Then
strSQL = strSQL & " WHERE " & strWhere
End If
strSQL = strSQL & " GROUP BY [" & strFieldName & "]"
If strOrderByField <> "" Then
If strOrderByField <> strFieldName Then
strSQL = strSQL & ", [" & strOrderByField & "] "
End If
strSQL = strSQL & " ORDER BY [" & strOrderByField & "]"
End If
Set myRST = whatDB.OpenRecordset(strSQL, , dbOpenForwardOnly)
If Not myRST.EOF Then
With myRST
.MoveFirst
If bolTextQualifiers = True Then
strReturnString = Chr$(34) & _
myRST(strFieldName) & _
Chr$(34)
Else
strReturnString = Nz(myRST(strFieldName), _
"Null")
End If
Do
.MoveNext
If .EOF Then Exit Do
If myRST.AbsolutePosition + 1 = _
myRST.RecordCount Then
strReturnString = _
strReturnString & _
strLastSeperator
Else
strReturnString = _
strReturnString & _
strSeperator
End If
If bolTextQualifiers = True Then
strReturnString = _
strReturnString & _
Chr$(34) & _
myRST(strFieldName) & _
Chr$(34)
Else
strReturnString = _
strReturnString & _
myRST(strFieldName)
End If
Loop While Not .EOF
End With
End If
myRST.Close
Set myRST = Nothing

If bolISetTheDB = True Then
Set whatDB = Nothing
End If
TableAsString = strReturnString
End Function

On 14 Dec 2005 14:40:17 -0800, "Kurt" <kh*******@cox.net> wrote:
I'm using the fConcatChild function posted at
http://www.mvps.org/access/modules/mdl0004.htm to return a field from
the Many table of a 1:M relationship into a concatenated string. The
function puts everything in a comma separated format. In my case, it
returns a list of authors:
Borman, W., Oppler, S., White, L.,
I'd like to change the function so the last record in the string is
preceded by an ampersand instead of a comma, as in:
Borman, W., Oppler, S., & White, L.
(Note: Each last name and initial is actually a concatenated field done
in a query, using: "Author: [LName] & ", " & [Initials]"). In other
words, some of those commas are mine.)
I've tried to edit the function to use the ampersand. Within the
loop, I've added code to keep track of what the current record is so
that once I've finished the loop, I will know the last record added
to the string (i.e., the last author). I then try to remove the entry,
add an ampersand, then add the last record back.
The change I made correctly added the ampersand and the last record,
but it didn't delete the last record. In other words, the last author
is repeated twice, as in:
Borman, W., Oppler, S., White, L., & White, L.
It appears that the code to remove the last record plus the trailing
"," is
either not doing anything, or is being killed:
'Remove the last record added plus the trailing ","
fConcatChild = Left(varConcat, Len(varCurrRecord) + 1)
I know this because I made major changes to this syntax and it had no
observable effect. The next line of code works fine:
'Add the ampersand and the last record
fConcatChild = varConcat & "& " & varCurrRecord
... but when I bebug this with:
Debug.Print "varConcat: " & varConcat, "varCurrRecord: " &
varCurrRecord
... I get
varConcat: Borman, W., Hanson, M., Oppler, S., & White, L.,
varCurrRecord: White, L.
... which confirms that the last record is not being deleted.
Any ideas? I've tried everything and my brain is melting. Thanks.
(Full code below.)

--
Drive C: Error. (A)bort (R)etry (S)mack The Darned Thing

Dec 14 '05 #2
rkc
Kurt wrote:
I'm using the fConcatChild function posted at
http://www.mvps.org/access/modules/mdl0004.htm to return a field from
the Many table of a 1:M relationship into a concatenated string. The
function puts everything in a comma separated format. In my case, it
returns a list of authors:


Assuming you're using a post 97 version of Access and all your records
are unique, just do a

Replace(varConcat, "," & lastBlahBlah, "&" & lastBlahBlah)

or some such.
Dec 15 '05 #3
"Kurt" <kh*******@cox.net> wrote in
news:11**********************@g43g2000cwa.googlegr oups.com:
I'm using the fConcatChild function posted at
http://www.mvps.org/access/modules/mdl0004.htm to return a
field from the Many table of a 1:M relationship into a
concatenated string. The function puts everything in a comma
separated format. In my case, it returns a list of authors:

Borman, W., Oppler, S., White, L.,

I'd like to change the function so the last record in the
string is preceded by an ampersand instead of a comma, as in:

Borman, W., Oppler, S., & White, L.

(Note: Each last name and initial is actually a concatenated
field done in a query, using: "Author: [LName] & ", " &
[Initials]"). In other words, some of those commas are mine.)
'Are we sure that 'sub' records exist
With rs
If .RecordCount <> 0 Then
'start concatenating records
Do While Not rs.EOF
varCurrRecord = rs(strFldConcat)
varConcat = varConcat & varCurrRecord & ", "
.MoveNext
Loop
End If
End With

'That's it... you should have a concatenated string now
'Remove the last record added plus the trailing ","
fConcatChild = Left(varConcat, Len(varCurrRecord) + 1)
'Add the ampersand and the last record
fConcatChild = varConcat & "& " & varCurrRecord

Exit_fConcatChild:
Set rs = Nothing: Set db = Nothing
Exit Function
Err_fConcatChild:
Resume Exit_fConcatChild
End Function

'************ Code End **********

calculate the shortened length ot the string, and take the left
() number of characters.
x = len(varConcat)
y = len(varCurrentRecord)
z = len(", ")
(I know that it looks dumb, but if you analyse your code against
the equation you'll find your problems.)

left(varConcat,x-(y+z)) + " & " varCurrentRecord

--
Bob Quintal

PA is y I've altered my email address.
Dec 15 '05 #4
Thank you for all your suggestions. rkc's Replace suggestion got me in
the right direction the fastest, but I got a good, complete solution
from Doug Steele, MS Access MVP. The code now also handles cases with a
single author. Final relevant code for those interested:

With rs
If .RecordCount > 1 Then
'start concatenating records
Do While Not rs.EOF
varCurrRecord = rs(strFldConcat)
varConcat = varConcat & varCurrRecord & ", "
.MoveNext
Loop
'Remove the last record, plus the trailing ","
varConcat = Left(varConcat, Len(varConcat) -
Len(varCurrRecord) - 2)
'Add the ampersand and the last record
fConcatChild = varConcat & "& " & varCurrRecord
ElseIf .RecordCount = 1 Then
Do While Not rs.EOF
varCurrRecord = rs(strFldConcat)
varConcat = varConcat & varCurrRecord
.MoveNext
Loop
fConcatChild = varConcat
End If
End With

Dec 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Eric Theil | last post by:
I'm at my wit's end with this one. Within an xsl:if test, I'm not able to get 2 variables to properly evaluate if one of them is wrapped within a string function. <!-- This works --> <xsl:if...
3
by: DP | last post by:
I am trying to use the string or string$ (I don't know the difference) function to convert a number to string, but I do not know what the obligatory arguments 2nd & 3d stand for. Thanks for your...
4
by: Ralph Noble | last post by:
Does anyone know of a string function in Access that will allow me to count the number of instances one string occurs within another? Or if there is some sort of word count function? If there is,...
23
by: David Frank | last post by:
How can I write a string function that encloses the input string in quotes "string" ?? below works for the "123 operation but adding " to it clobbers the "123 main() { char...
3
by: Jay Feldman | last post by:
In vb6, the String(Number, Character) function would let you generate a string filled with a certain character. How can I do this in .NET, since there is no String function and I had trouble...
5
by: JR | last post by:
I am having trouble remembering a string function that I used a while back in VB. I don't know if it was .NET specific. The function either searched a string or replaced a substring in a string...
4
by: Jazzer | last post by:
I want to 'flatten' two tables into one by combining the '1-n' values from the 'child' records into a single concatenated string within the parent by using queries. I.E. create a single NVarChar...
2
by: rn5a | last post by:
All the String functions like 'Len', 'Left', 'Right', 'Mid' etc. that are available in ASP are supported by ASP.NET as well except for the string function 'String'. The 'String' function takes...
3
bilibytes
by: bilibytes | last post by:
Hi, I am having a problem with a concatenated string. I start my string outside of a for() and then, concatenate the string generated with the loop to it. the string out of the loop looks like...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.