Concatenate Records from Record Set (rs) SQL Query from ACCESS Database

bemp87

Board Regular
Joined
Dec 10, 2016
Messages
102
Hi Cummunity,

Needing some help to concatenate row values being retrieved from ACCESS Database. INstead of writing to multiple rows, i need to concatenate all the record set values and write it to a single cell.

Here is a view of part of my code: Currently there are two rows in the database that meet this condition. This would write the first record to TargetRange + 1 which TargetRange = K44. The second row would then be written to K45 + 1. I need to either loop through the records or concatenate them to write the records to a single cell / row. Any guidance?

Code:
' QUERY FOR (PENDING VACATION)

 rs31.Open "SELECT Request_Date FROM Vacation WHERE EID ='" & Worksheets("Sched").Range("N17") & "' And Request_Status = 0", cn, , , adCmdText


    
          ' Write the field names (PENDING VACATION)
For intColIndex = 0 To rs31.Fields.Count - 1
TargetRange31.Offset(1, intColIndex).Value = rs31.Fields(intColIndex).Name
    Next


          ' Write recordset (PENDING VACATION)
TargetRange31.Offset(1, 0).CopyFromRecordset rs31
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Replace the CopyFromRecordSet line with:
Code:
    Dim records As Variant, allRecords As String, i As Long
    records = rs31.GetRows
    allRecords = ""
    For i = 0 To UBound(records, 2)
        allRecords = allRecords & records(0, i) & ","
    Next
    TargetRange31.Offset(2, 0).Value = Left(allRecords, Len(allRecords) - 1)
 
Last edited:
Upvote 0
Hi John - is there a way to format this in a way where there is a space in between each record? i.e. "record & " " record " ""
 
Upvote 0
Thanks John! This worked like a charm. I have one more question. I've slightly altered the query that now pulls (2) columns "Holiday_Name and Holiday_Date". I'm looking to figure out how do I alter the code you provided above to concatenate now what is two column values being retrieved in one record set. I.e. I have rs31 retrieving Holiday_Name and Holiday_Date, and I want it to somehow concatenate it as Holiday_Name & " - " & Holiday_Date both being placed in cell K45. Any guidance here?
 
Upvote 0
ADODB RecordSet.GetRows returns a 2-dimensional array of records and fields - records(field index, record index) in my code - so it is just a matter of looping through the record and field indexes. Your first query returned only 1 field, so the field index of the array is always 0, i.e. records(0, i). For multiple fields we also loop through the field indexes:

Code:
    Dim records As Variant, allRecords As String
    Dim r As Long, f As Long
    
    records = rs31.GetRows
    allRecords = ""
    For r = 0 To UBound(records, 2)
        For f = 0 To UBound(records, 1)
            allRecords = allRecords & records(f, r) & " - "
        Next
        allRecords = Left(allRecords, Len(allRecords) - 3)
        allRecords = allRecords & ", "
    Next
    TargetRange31.Offset(1, 0).Value = Left(allRecords, Len(allRecords) - 2)
in the above code each record is separated by ", ".
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top