R
Ray S.
I'm using a slight variation of Dev's function, but apostrophes in names are
giving me fits. Here's the essence of his function, which I always credit to
him even in my variation:
Option Compare Database
Function fConcatChild(strChildTable As String, _
strIDName As String, _
strFldConcat As String, _
strIDType As String, _
varIDvalue As Variant) _
As String
'This function was modified from one written by Dev Ashish
'It returns a field from the Many table of a 1:M relationship
'in a semi-colon separated format.
'
'Usage Example with my database:
' ?fConcatChild("Timesheets","Name","Timesheet
' ID","String",[Names].[Name])
' returns all the Timesheet IDs that are assigned
' to an employee separated by colons
'
' Timesheets = Many side table name
' Name = Primary Key of the One side table
' Timesheet ID = Field name to concatenate
' String = DataType of Primary Key of One Side Table
' value = Value on which return concatenated item
'
' I replaced the value with [Names].[Name] and changed the strIDType
from
' Long to String because I'm returning values on names
' to give me all values
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":
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) - 2)
Exit_fConcatChild:
Set rs = Nothing: Set db = Nothing
Exit Function
Err_fConcatChild:
Resume Exit_fConcatChild
End Function
OK, essentially that's the function...the problem I'm getting is that when
the name has an apostrophe, like O'Neill or O'Meara, the function returns no
data (when I can see that there is data that should be returning). It is only
happening with those types of names. I created fake "Irish type" names to
check this and that's what's happening, but I don't know why, or how to fix
it.
giving me fits. Here's the essence of his function, which I always credit to
him even in my variation:
Option Compare Database
Function fConcatChild(strChildTable As String, _
strIDName As String, _
strFldConcat As String, _
strIDType As String, _
varIDvalue As Variant) _
As String
'This function was modified from one written by Dev Ashish
'It returns a field from the Many table of a 1:M relationship
'in a semi-colon separated format.
'
'Usage Example with my database:
' ?fConcatChild("Timesheets","Name","Timesheet
' ID","String",[Names].[Name])
' returns all the Timesheet IDs that are assigned
' to an employee separated by colons
'
' Timesheets = Many side table name
' Name = Primary Key of the One side table
' Timesheet ID = Field name to concatenate
' String = DataType of Primary Key of One Side Table
' value = Value on which return concatenated item
'
' I replaced the value with [Names].[Name] and changed the strIDType
from
' Long to String because I'm returning values on names
' to give me all values
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":
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) - 2)
Exit_fConcatChild:
Set rs = Nothing: Set db = Nothing
Exit Function
Err_fConcatChild:
Resume Exit_fConcatChild
End Function
OK, essentially that's the function...the problem I'm getting is that when
the name has an apostrophe, like O'Neill or O'Meara, the function returns no
data (when I can see that there is data that should be returning). It is only
happening with those types of names. I created fake "Irish type" names to
check this and that's what's happening, but I don't know why, or how to fix
it.