0

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

7
  • 2
    Where is the rest of your code? The only thing you are showing us is the SQL statement. You combine the SQL statement with VBA code, as VBA is not a type of query. Commented Sep 27, 2017 at 19:00
  • You have to turn the SQL statement into VBA string type variable and there is specific syntax for that. Commented Sep 27, 2017 at 19:15
  • 3
    Also, can't 'run' a SELECT sql. SELECT sql is used to set a RecordSource object or form/report RecordSource property. Only action sql can be run - UPDATE, INSERT, DELETE, SELECT INTO. You can Open a SELECT query object. But why open query object? Why not create report using the query as RecordSource? Commented Sep 27, 2017 at 19:22
  • @ScottHoltzman - see my edit. I failed to realize I omitted my VBA syntax Commented Sep 27, 2017 at 20:03
  • @BellHopByDayAmetuerCoderByNigh - See comment by June7. Also, I am wary of ConcatRelated. I bet Access doesn't like that syntax. Commented Sep 27, 2017 at 21:42

2 Answers 2

1

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 " & _
Sign up to request clarification or add additional context in comments.

2 Comments

[userID] is a short text type field.
Ooops! Did you find my extra ) typo? Edited answer again.
1

I've used this nifty tool many times with great success.

Creating the form

The form just needs two text boxes, and a command button. SQL statements can be quite long, so you put the text boxes on different pages of a tab control.

Create a new form (in design view.)
Add a tab control.
In the first page of the tab control, add a unbound text box. Set its Name property to txtSql.
Increase its Height and Width so you can see many long lines at once.
In the second page of the tab control, add another unbound text box.
Name it txtVBA, and increase its height and width.
Above the tab control, add a command button. Name it cmdSql2Vba.
Set its On Click property to [Event Procedure].
Click the Build button (...) beside this property.
When Access opens the code window, set up the code like this:

    Private Sub cmdSql2Vba_Click()
        Dim strSql As String
        'Purpose:   Convert a SQL statement into a string to paste into VBA code.
        Const strcLineEnd = " "" & vbCrLf & _" & vbCrLf & """"

        If IsNull(Me.txtSQL) Then
            Beep
        Else
            strSql = Me.txtSQL
            strSql = Replace(strSql, """", """""")  'Double up any quotes.
            strSql = Replace(strSql, vbCrLf, strcLineEnd)
            strSql = "strSql = """ & strSql & """"
            Me.txtVBA = strSql
            Me.txtVBA.SetFocus
            RunCommand acCmdCopy
        End If
    End Sub

Using the form

Open your query in SQL View, and copy the SQL statement to clipboard (Ctrl+C.)
Paste into the first text box (Ctrl+V.)
Click the button.
Paste into a new line in your VBA procedure (Ctrl+V.)

enter image description here

http://allenbrowne.com/ser-71.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.