ms-accessvbams-access-2013

Convert Access SQL To VBA


I am in need of converting this Access SQL Query to a VBA Query ->

SELECT informationTable.userID, 
ConcatRelated('itemSold','[informationTable]',"userID='" & [userID] & "'") AS NameOfItemSold
FROM informationTable
GROUP BY informationTable.userID;

I tried ths VBA

DoCmd.RunSQL ("SELECT informationTable.userID,
 ConcatRelated('itemsold','[informationTable]','userID= ' & Chr(34)  & [userID] & Chr(34) & ') AS NameOfItemSold 
Into CRInformationTable
FROM informationTable 
GROUP BY informationTable.userID;")

but I get an error of

A RunSQL action requires an argument consisiting of an SQL statement


Solution

  • I did some testing. Assuming userID is number type field, see if this works for you:

    DoCmd.RunSQL ("SELECT DISTINCT informationTable.userID, " & _
        "ConcatRelated('itemsold','[informationTable]','userID=' & [userID]) AS NameOfItemSold " & _
        "INTO CRInformationTable FROM informationTable;")
    

    If userID is text type:

    "ConcatRelated('itemsold','[informationTable]','userID=" & Chr(34) & "' & [userID] & '" & Chr(34) & "') AS NameOfItemSold " & _
    

    Instead of Chr(34):

    "ConcatRelated('itemsold','[informationTable]','userID=""' & [userID] & '""') AS NameOfItemSold " & _