0

Thank you to all your responses. I have a table with one id field and R1-R30 fields. I was able to concatenate R1-R30 fields in a query using

Route: Trim([R1] & IIf([R2]="","","   ") & [R2] & IIf([R3]="","","   ") & [R3] & IIf([R4]="","","   ") & [R4] & IIf([R5]="","","   ") & [R5] & IIf([R6]="","","   ") & [R6] & IIf([R7]="","","   ") & [R7] & IIf([R8]="","","   ") & [R8] & IIf([R9]="","","   ") & [R9] & IIf([R10]="","","   ") & [R10] & IIf([R11]="","","  ") & [R11] & IIf([R12]="","","   ") & [R12] & IIf([R13]="","","   ") & [R13] & IIf([R14]="","","   ") & [R14] & IIf([R15]="","","   ") & [R15] & IIf([R16]="","","   ") & [R16] & IIf([R17]="","","   ") & [R17] & IIf([R18]="","","   ") & [R18] & IIf([R19]="","","   ") & [R19] & IIf([R20]="","","   ") & [R20] & IIf([R21]="","","   ") & [R21] & IIf([R22]="","","   ") & [R22] & IIf([R23]="","","   ") & [R23] & IIf([R24]="","","   ") & [R24] & IIf([R25]="","","   ") & [R25] & IIf([R26]="","","   ") & [R26] & IIf([R27]="","","   ") & [R27] & IIf([R28]="","","   ") & [R28] & IIf([R29]="","","   ") & [R29] & IIf([R30]="","","   ") & [R30])

My question is if the Join function I found can be applied to a query where the delimeter could be a spare, comma or slash.

Join (source_array,[delimiter])

Thanks

braX
  • 11,506
  • 5
  • 20
  • 33
R Rivera
  • 1
  • 3
  • There is no such function in VBA. – Gustav Jan 26 '19 at 10:20
  • Thank you @Gust. I found this Join(source_array,[delimiter]) in Access 2016 book but not sure if I can apply it to a query or a form. – R Rivera Jan 28 '19 at 16:39
  • Join requires an array. But it is unclear what you are trying to accomplish. – Gustav Jan 28 '19 at 21:39
  • Join Requieres a unidimensional array. You can take a Recordset into a bidimensional array, but a bidimensional array cannot be joined. Of course, you can take the values of that bidimensional array into a unidimensional new array with a loop, but then maybe you could just loop every field and concatenate them – Foxfire And Burns And Burns Jan 29 '19 at 12:17
  • 1
    Possible duplicate of [Combine values from related rows into a single concatenated string value](https://stackoverflow.com/questions/13278590/combine-values-from-related-rows-into-a-single-concatenated-string-value) – Andre Jan 29 '19 at 13:51
  • What do you mean _concatenate function_, there is the & oparatot to concat strings. – Siyon DP Jan 29 '19 at 16:03

1 Answers1

0

This would be the code to take all values of 1 single recordset into a bidimensional array, and then take those values into a unidimensional array (excluding null values, because null values cannot be joined with JOIN).

I think it would be better just looping trough every field with the loop, but in case it might help, i'll post it.

To replicate your issue, I just created a database with 1 single table with 2 records:

enter image description here

I'll concatenate all fields, excluding ID field. So with an easy query, I can get a recordset of 1 single record, using ID field as parameter:

SELECT Tabla1.Field1, Tabla1.Field2, Tabla1.Field3, Tabla1.Field4
FROM Tabla1
WHERE (((Tabla1.Id)=1));

And then the VBA code to Msgbox the fields joined, using a comma as delimiter.

Sub JOIN_RST()
Dim rst As Recordset

Dim vArray As Variant
Dim SingleArray() As Variant

Dim i As Long

Dim MySQL As String
Dim STRJoined As String

MySQL = "SELECT Tabla1.Field1, Tabla1.Field2, Tabla1.Field3, Tabla1.Field4 " & _
    "FROM Tabla1 WHERE (((Tabla1.Id)=2));" 'query to get a single recordset.


Set rst = Application.CurrentDb.OpenRecordset(MySQL, 2, 4)
DoEvents

If rst.RecordCount > 0 Then
    rst.MoveLast
    rst.MoveFirst
    vArray = rst.GetRows
    ReDim SingleArray(UBound(vArray))

    For i = 0 To UBound(SingleArray)
        If IsNull(vArray(i, 0)) = True Then
            SingleArray(i) = ""
        Else
            SingleArray(i) = vArray(i, 0)
        End If
    Next i

    Debug.Print vArray(0, 0) 'Field 1
    Debug.Print vArray(1, 0) 'Field 2
    Debug.Print vArray(2, 0) 'Field 3
    Debug.Print vArray(3, 0) 'Field 4

    STRJoined = Join(SingleArray, ",")

    Debug.Print STRJoined
End If

Set rst = Nothing
Erase vArray
Erase SingleArray
DoEvents


End Sub

If I execute this code using as WHERE parameter ID=1 , in debugger Window I get:

First Record
 1 
Null
My first record. Got a null value in Field 3 (it's empty)
First Record,1,,My first record. Got a null value in Field 3 (it's empty)

With ID=2 I get:

Second Record
 2 
Not null
Second Record
Second Record,2,Not null,Second Record

So this kinda works. I hope you can adapt it to your needs. but as i said. looking at the code, I think it would be easier just looping trough fields in a single query with all records. something like this:

Sub LOOPING_TROUGHT_FIELDS()
Dim RST As Recordset
Dim Joined_Records() As Variant
Dim i As Long
Dim MySQL As String
Dim STRJoined As String

Dim FLD As Field

MySQL = "SELECT Tabla1.Field1, Tabla1.Field2, Tabla1.Field3, Tabla1.Field4 " & _
    "FROM Tabla1;" 'query to get all recordset you want to join
Set RST = Application.CurrentDb.OpenRecordset(MySQL, 2, 4)
DoEvents

If RST.RecordCount > 0 Then
    RST.MoveLast
    RST.MoveFirst

    i = 0
    ReDim Joined_Records(RST.RecordCount)

    Do Until RST.EOF = True
        For Each FLD In RST.Fields
            If IsNull(FLD.Value) = True Then
                STRJoined = STRJoined & "" & ","
            Else
                STRJoined = STRJoined & FLD.Value & ","
            End If

        Next FLD
        Joined_Records(i) = Left(STRJoined, Len(STRJoined) - 1) 'we get 1 minus because there is an extra comma at end
        i = i + 1
        STRJoined = ""
        RST.MoveNext
    Loop

End If

Set RST = Nothing
Set FLD = Nothing

For i = 0 To UBound(Joined_Records) Step 1
    Debug.Print Joined_Records(i)
Next i

Erase Joined_Records

End Sub

I don't know how many records you got. Try both and check how long does each option takes, and then choose 1.

Hope you can adapt all this to your needs. Welcome to SO.