Concatenate function not working (1 Viewer)

Kayleigh

Member
Local time
Today, 02:36
Joined
Sep 24, 2020
Messages
706
Hi,
Trying to join together several related records into one textbox - used the very useful function I found on this post and associated code.
However I keep encountering errors when running.
Can anyone suggest where I went wrong please?
Thank you
Krayna
 

Attachments

  • WorkPlaceTest.accdb
    684 KB · Views: 563

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 18:36
Joined
Oct 29, 2018
Messages
21,485
Hi. I don't use Allen Browne's function because I have my own here.


However, I took a look and tried to fix your db. Check it out.
 

Attachments

  • WorkPlaceTest.zip
    33.8 KB · Views: 532

Kayleigh

Member
Local time
Today, 02:36
Joined
Sep 24, 2020
Messages
706
That's neat! I like the way you used a query to join fields for the concat function.
And why is the function isTable required?
 

Kayleigh

Member
Local time
Today, 02:36
Joined
Sep 24, 2020
Messages
706
I have extended the form to include a fake multi-value checklist - as see on @MajP and I used here - however having trouble linking to original form.
Can you help debug this?

Really excited with how this form is turning out:)
 

Attachments

  • WorkPlaceTest2.accdb
    704 KB · Views: 572

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:36
Joined
May 7, 2009
Messages
19,247
actually should be change to:
Code:
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
    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.
    If IsTableOrQuery(strTable) Then
        strSql = "SELECT " & strField & " FROM " & strTable
    Else
        strSql = "SELECT " & strField & " FROM (" & strTable & ")"
    End If
    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


Public Function IsTableOrQuery(ByVal strTable As String) As Boolean
    Dim td As DAO.TableDef
    Dim qd As DAO.QueryDef
    Dim db As DAO.Database
    IsTableOrQuery = False
    Set db = CurrentDb
    On Error Resume Next
    Set td = db.TableDefs(strTable)
    IsTableOrQuery = (Err.Number = 0)
    If Not IsTableOrQuery Then
        Err.Clear
        Set qd = db.QueryDefs(strTable)
    Else
        Set td = Nothing
        Set db = Nothing
        Exit Function
    End If
    IsTableOrQuery = (Err.Number = 0)
    Set td = Nothing
    Set qd = Nothing
    Set db = Nothing
    
End Function
this is to test if we passed a Table/Query to the function.
the old ConcatRelated only accepts table and Query.
the new function now accepts also a Select statement.

need also to adjust the concatRelated function:
Code:
    'Build SQL string, and get the records.
    If IsTableOrQuery(strTable) Then
        strSql = "SELECT " & strField & " FROM " & strTable
    Else
        strSql = "SELECT " & strField & " FROM (" & strTable & ")"
    End If
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:36
Joined
May 7, 2009
Messages
19,247
see this changes.
with respect to mr.majp, i remove the click event on the checkbox.
 

Attachments

  • WorkPlaceTest2.accdb
    748 KB · Views: 560

Kayleigh

Member
Local time
Today, 02:36
Joined
Sep 24, 2020
Messages
706
Thanks for looking at that. It definitely works to change days for an existing record and when I added it as a subform of frmStudentRecord, it allowed me to add new records.
By the way there was some trouble implementing into my database until I realised that the field references were not completely correct...
 
Last edited:

Kayleigh

Member
Local time
Today, 02:36
Joined
Sep 24, 2020
Messages
706
Hi there,
I know this thread is a year old but I'm encountering a very similar issue and struggling to resolve it.

I would like to produce a summary of attendance on each student's form. Something like:
DateRegister code
16/08/2021/, \
17/08/2021O, \...

See attached the relevant tables. I tried concatRelated but keep getting error 3061 - I found out here that its to do with opening recordsets.
If anyone can help me with a simple query to resolve this. Would really appreciate.
 

Attachments

  • concatRegs.accdb
    2 MB · Views: 296

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 18:36
Joined
Oct 29, 2018
Messages
21,485
Hi there,
I know this thread is a year old but I'm encountering a very similar issue and struggling to resolve it.

I would like to produce a summary of attendance on each student's form. Something like:
DateRegister code
16/08/2021/, \
17/08/2021O, \...

See attached the relevant tables. I tried concatRelated but keep getting error 3061 - I found out here that its to do with opening recordsets.
If anyone can help me with a simple query to resolve this. Would really appreciate.
Hi. Sorry, but I don't understand your table relationships. I also don't see any form in the db you posted.
 

Kayleigh

Member
Local time
Today, 02:36
Joined
Sep 24, 2020
Messages
706
Each session is logged in jtblSessionLog. Students participating in that session are logged in jtblStudentAttendance. So SessionLogID is FK in jtblstudentattendance. Date of session in jtblSessionLog; registerCodeID in jtblStudentAttendance.
Form is too complicated to copy over - this is just one component of it.
I just need some help building a suitable query.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 18:36
Joined
Oct 29, 2018
Messages
21,485
Each session is logged in jtblSessionLog. Students participating in that session are logged in jtblStudentAttendance. So SessionLogID is FK in jtblstudentattendance. Date of session in jtblSessionLog; registerCodeID in jtblStudentAttendance.
Form is too complicated to copy over - this is just one component of it.
I just need some help building a suitable query.
But you said you were getting a 3061 error. That usually means you are referring to a form control. Can you show that part to us?
 

Kayleigh

Member
Local time
Today, 02:36
Joined
Sep 24, 2020
Messages
706
The SQL for the query looked up in concat is:
SQL:
SELECT jtblStudentAttendance.fldStudentID, jtblSessionLog.fldDate, tblRegisterCodes.fldRegisterCode
FROM jtblSessionLog INNER JOIN (jtblStudentAttendance INNER JOIN tblRegisterCodes ON jtblStudentAttendance.fldRegisterCodeID = tblRegisterCodes.fldRegisterCodeID) ON jtblSessionLog.fldSessionLogID = jtblStudentAttendance.fldSessionLogID
GROUP BY jtblStudentAttendance.fldStudentID, jtblSessionLog.fldDate, tblRegisterCodes.fldRegisterCode
HAVING (((jtblStudentAttendance.fldStudentID)=[forms]![frmStudents]![fldStudentID]))
ORDER BY jtblStudentAttendance.fldStudentID;
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 18:36
Joined
Oct 29, 2018
Messages
21,485
The SQL for the query looked up in concat is:
SQL:
SELECT jtblStudentAttendance.fldStudentID, jtblSessionLog.fldDate, tblRegisterCodes.fldRegisterCode
FROM jtblSessionLog INNER JOIN (jtblStudentAttendance INNER JOIN tblRegisterCodes ON jtblStudentAttendance.fldRegisterCodeID = tblRegisterCodes.fldRegisterCodeID) ON jtblSessionLog.fldSessionLogID = jtblStudentAttendance.fldSessionLogID
GROUP BY jtblStudentAttendance.fldStudentID, jtblSessionLog.fldDate, tblRegisterCodes.fldRegisterCode
HAVING (((jtblStudentAttendance.fldStudentID)=[forms]![frmStudents]![fldStudentID]))
ORDER BY jtblStudentAttendance.fldStudentID;
Okay, thanks. It was just as I had suspected. May I suggest you give my new simple function a try?

theDBguy's Access Blog: SimpleCSV() v2.0
 

Kayleigh

Member
Local time
Today, 02:36
Joined
Sep 24, 2020
Messages
706
Thanks. I understand but not sure how to apply to my situation - would like join related rows by date.
I've copied relevant part of form to DB so you see how it works.
 

Attachments

  • concatRegs1.accdb
    640 KB · Views: 340

Users who are viewing this thread

Top Bottom