M
morriand
Guest
Hello,
I have a database, where I am trying to concatenate sub-record values into one cell so that when I use mail merge, I can actually have a printout of all the subvalues on one record. I am sure my problem is easy to solve but I am a physician who ended up having to teach himself computers over the past 4 years rather than actually be trained as a programmer:
My SQL code is:
SELECT tDemographics.*, fConcatChild("tChronic Problems","MRN","ChronicProblem","String",[MRN]) AS [Chronic Problem Summary]
FROM tDemographics
WHERE (((tDemographics.MRN) = [MRN]));
using the following module:
Option Compare Database
Option Explicit
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.
'
Dim db As Database
Dim rs As Recordset
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", "Memo", "Text":
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
varConcat = varConcat & rs(strFldConcat) & Chr(13)
.MoveNext
Loop
End If
End With
fConcatChild = Left(varConcat, Len(varConcat) - 1)
Exit_fConcatChild:
Set rs = Nothing: Set db = Nothing
Exit Function
Err_fConcatChild:
Resume Exit_fConcatChild
End Function
My problem is that I only seem to be able to output a concatenated string of 255 characters (?text) whereas I need it to output up to 2000 characters. What can I change in the code to fix this problem?
Andrew
I have a database, where I am trying to concatenate sub-record values into one cell so that when I use mail merge, I can actually have a printout of all the subvalues on one record. I am sure my problem is easy to solve but I am a physician who ended up having to teach himself computers over the past 4 years rather than actually be trained as a programmer:
My SQL code is:
SELECT tDemographics.*, fConcatChild("tChronic Problems","MRN","ChronicProblem","String",[MRN]) AS [Chronic Problem Summary]
FROM tDemographics
WHERE (((tDemographics.MRN) = [MRN]));
using the following module:
Option Compare Database
Option Explicit
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.
'
Dim db As Database
Dim rs As Recordset
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", "Memo", "Text":
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
varConcat = varConcat & rs(strFldConcat) & Chr(13)
.MoveNext
Loop
End If
End With
fConcatChild = Left(varConcat, Len(varConcat) - 1)
Exit_fConcatChild:
Set rs = Nothing: Set db = Nothing
Exit Function
Err_fConcatChild:
Resume Exit_fConcatChild
End Function
My problem is that I only seem to be able to output a concatenated string of 255 characters (?text) whereas I need it to output up to 2000 characters. What can I change in the code to fix this problem?
Andrew