Results 1 to 15 of 15
  1. #1
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672

    Convert Query SQL To VBA

    Greetings @pbaldy helped me out with syntax in a diff thread on here, but that was just for the SQL - I am now having issues converting it to VBA. This is my Query SQL
    Code:
    SELECT Table1.empID, ConcatRelated('saleItem','[Table1]',"empID='" & [empID] & "'") AS Item
    FROM Table1
    GROUP BY Table1.empID;
    And this is the VBA syntax I have tried...what is the proper way to utilize this in VBA?

    Code:
    DoCmd.RunSQL ("SELECT Table1.empID, ConcatRelated('saleItem','[Table1]','empID= ' & Chr(34)  & [empID] & Chr(34) & ') AS Item Into Hopscotch FROM Table1 GROUP BY Table1.empID;")


  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    ID are usually numeric, not string. If this is so, you dont need quotes.

    SELECT Table1.empID, ConcatRelated('saleItem','[Table1]','empID= ' & [empID] & ') AS Item Into Hopscotch FROM Table1 GROUP BY Table1.empID;"

    Tho you can always just concat fields instead of CONCATRELATED.

  3. #3
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    how would I use concat and not concat related? The field is short text because it contains alpha numeric characters.

    And attempting the syntax you posted gives me the below error
    Syntax error in query expression 'ConcatRelated('saleItem', '[Table1]',empID=' & [empID] & ') As Item
    INTO Hopscotch From Table1 Group By Table1.empID;

    EDIT --
    Attached is a sample of my database, this should help me better understand if concat will work
    Attached Files Attached Files

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Ranman's use of the word "concat" is really a short form of the word concatenate. He is saying that you can just concatenate the string (like you've done with empid), instead of using the function called ConcatRelated.

    There is an apostrophe missing from the front of [Table1].

  5. #5
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by aytee111 View Post
    Ranman's use of the word "concat" is really a short form of the word concatenate. He is saying that you can just concatenate the string (like you've done with empid), instead of using the function called ConcatRelated.

    There is an apostrophe missing from the front of [Table1].
    The missing apostrophe was just a typo on my part. It would not let me copy/paste from the error message box that displayed. I have used Concatenate before in Excel - but what I am after is creating a comma seperated list of each saleItem for the selected empID. Using the database I attached, taking the 3 rows for abc123 and creating ONE row that reads like the below
    empID saleItem
    abc123 gr1, zz1, fla323

    I have had success with the ConcatRelated() function when the field is a numeric field, but I just can not get this to work since it is a text field. Can concatenate() function in vba do what I am after?

  6. #6
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936

  7. #7
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by aytee111 View Post
    That's the function I was using, and I have it working in the SQL view of a query - I just for the life of me can not get the syntax down to utilize it in VBA

  8. #8
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Read "The Arguments" step 3. Your quotes are all messed up.

    Do you know how to debug VBA? Make a breakpoint and step thru the code, then you can see what each and every field contains at each step.

  9. #9
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by aytee111 View Post
    Read "The Arguments" step 3. Your quotes are all messed up.

    Do you know how to debug VBA? Make a breakpoint and step thru the code, then you can see what each and every field contains at each step.

    If I change the syntax to this - it runs error free, but it will insert ALL empID not the selected one from the sub form

    Code:
    DoCmd.RunSQL ("SELECT Table1.empID, ConcatRelated('saleItem','[Table1]','empID= ''' & [empID] & '''') AS Item Into Hopscotch FROM Table1 GROUP BY Table1.empID;")
    And yes - I stepped through my code to ensure the variable [empID] holds the appropriate value

  10. #10
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    The reason it is doing it for all EmpIDs is because that is what your SQL is saying, there is no where clause.

    It was the strWhere that didn't contain the appropriate value, not the EmpID.

  11. #11
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by aytee111 View Post
    The reason it is doing it for all EmpIDs is because that is what your SQL is saying, there is no where clause.

    It was the strWhere that didn't contain the appropriate value, not the EmpID.
    I must be having an issue with the quoting again - i tried this syntax with an added WHERE clause - but it tells me 0 rows to insert

    DoCmd.RunSQL ("SELECT Table1.empID, ConcatRelated('saleItem','[Table1]','empID= ''' & [empID] & '''') AS Item Into Hopscotch FROM Table1
    WHERE empID= ' & [empID] & ' GROUP BY Table1.empID;")

  12. #12
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Same as the other, empid needs quotes because it is a text field.

  13. #13
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    You are also going to need it to be a totals query with Group By, or else SELECT DISTINCT
    Edit: oh, it is!

  14. #14
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by aytee111 View Post
    Same as the other, empid needs quotes because it is a text field.
    I altered the syntax to
    Code:
    DoCmd.RunSQL ("SELECT Table1.empID, ConcatRelated('saleItem','[Table1]','empID= ''' & [empID] & '''') AS Item Into Hopscotch FROM Table1 
    WHERE empID= '' & [empID] & ''")
    And access just spins and spins.... Still incorrectly quoted?

  15. #15
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Sigh!
    DoCmd.RunSQL ("SELECT [Table1].[empID], ConcatRelated('saleItem','[Table1]','empID = """ & [empID] & """') AS Item INTO [ELS] FROM [Table1] WHERE empID = """ & [empID] & """")

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Convert SQL Server Query To Access Query
    By jo15765 in forum Queries
    Replies: 3
    Last Post: 03-16-2017, 06:13 AM
  2. Query to convert row to header ?
    By Cassim in forum Queries
    Replies: 1
    Last Post: 11-04-2013, 11:44 AM
  3. Convert SQL Server Query to Access Query
    By Juan4412 in forum Queries
    Replies: 4
    Last Post: 04-16-2013, 12:41 PM
  4. Query to Convert table
    By dhborchardt in forum Queries
    Replies: 3
    Last Post: 10-11-2012, 10:43 AM
  5. Convert Access query to SQL Server query
    By mkallover in forum Queries
    Replies: 2
    Last Post: 05-13-2011, 06:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums