C
Claire
I am trying to use fConcatChild and am unsuccessful.
I have a query that uses a value from a form along the way that has a list
of scheduled tests during a certain month (qryScheduling5). These all have a
CustNo along with site information and a scheduled date.
There is also a table (Customers) that has a CustNo primary key and various
contact information that I am going to use in a mail merge.
There may be 0, 1, or many scheduled dates for each CustNo. I would like to
concatenate the dates into one line so that I can use it one one page of a
mail merge.
Right now I am getting no values. If I comment out the first On Error
(after the Dim statements) the code gets stuck at Set rs =
db.OpenRecordset(strSQL, dbOpenSnapshot), with "Too few parameters. Expected
2." This sure looks like two parameters to me, and if I hover over both
strSQL and dbOpenSnapshot they each have values.
Thank you for any suggestions you have- hitting my head on the desk has
proven to be unfruitful.
~Claire
My SQL statement reads:
SELECT Customers.CustName, Customers.CustomerName2, Customers.CustAddress,
Customers.CustCity, Customers.CustState, Customers.CustZip,
Customers.CustomerContactTitle, Customers.CustContact,
fConcatChild("qryScheduling5","CustNo","SiteName","String",[CustNo]) AS Days
FROM Customers;
The code reads:
'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
'
' Set a reference to DAO
Dim db As DAO.Database
Dim rs As DAO.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":
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) & ";"
.MoveNext
Loop
End If
End With
'That's it... you should have a concatenated string now
'Just Trim the trailing ;
fConcatChild = Left(varConcat, Len(varConcat) - 1)
Exit_fConcatChild:
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
Set db = Nothing
Exit Function
Err_fConcatChild:
Resume Exit_fConcatChild
End Function
I have a query that uses a value from a form along the way that has a list
of scheduled tests during a certain month (qryScheduling5). These all have a
CustNo along with site information and a scheduled date.
There is also a table (Customers) that has a CustNo primary key and various
contact information that I am going to use in a mail merge.
There may be 0, 1, or many scheduled dates for each CustNo. I would like to
concatenate the dates into one line so that I can use it one one page of a
mail merge.
Right now I am getting no values. If I comment out the first On Error
(after the Dim statements) the code gets stuck at Set rs =
db.OpenRecordset(strSQL, dbOpenSnapshot), with "Too few parameters. Expected
2." This sure looks like two parameters to me, and if I hover over both
strSQL and dbOpenSnapshot they each have values.
Thank you for any suggestions you have- hitting my head on the desk has
proven to be unfruitful.
~Claire
My SQL statement reads:
SELECT Customers.CustName, Customers.CustomerName2, Customers.CustAddress,
Customers.CustCity, Customers.CustState, Customers.CustZip,
Customers.CustomerContactTitle, Customers.CustContact,
fConcatChild("qryScheduling5","CustNo","SiteName","String",[CustNo]) AS Days
FROM Customers;
The code reads:
'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
'
' Set a reference to DAO
Dim db As DAO.Database
Dim rs As DAO.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":
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) & ";"
.MoveNext
Loop
End If
End With
'That's it... you should have a concatenated string now
'Just Trim the trailing ;
fConcatChild = Left(varConcat, Len(varConcat) - 1)
Exit_fConcatChild:
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
Set db = Nothing
Exit Function
Err_fConcatChild:
Resume Exit_fConcatChild
End Function