Results 1 to 12 of 12
  1. #1
    dumbledown is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    46

    Unhappy Query works but VBA fails

    Hi, I have a query that works fine:



    SELECT *
    FROM [outputlinks1]
    UNION ALL
    SELECT * FROM [outputlinks2];

    I have VBA code of this query that fails (is not recognised):

    Dim strSQL As String
    strSQL = "SELECT * " & _
    "FROM [outputlinks1] " & _
    "UNION ALL " & _
    "SELECT * " & _
    "FROM [outputlinks2];"
    DoCmd.RunSQL strSQL


    I haven't a clue why. Does anyone know??

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You can only "run" an action query, not a select query. Since it's not dynamic, why not just leave it as a saved query?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    dumbledown is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    46
    Thanks pbaldy,
    OK that makes sense.
    However, VBA is required as I want to add a loop in as the number of tables to be linked varies from use to use.
    The code I have is:

    Dim strSQL As String


    strSQL = "SELECT " & Chr(42) & " " & _
    "INTO newlinks " & _
    "FROM [outputlinks1]"
    For i = 2 To logs
    strSQL = strSQL + " UNION ALL SELECT " & Chr(42) & " FROM [outputlinks" & i & "]"
    Next i
    strSQL = strSQL + ";"


    Debug.Print strSQL
    DoCmd.RunSQL strSQL


    When run through the debugger for logs = 3 the SQL is:

    SELECT * INTO newlinks FROM [outputlinks1] UNION ALL SELECT * FROM [outputlinks2] UNION ALL SELECT * FROM [outputlinks3];

    Do you know a work around for this?

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    As Paul said
    You can only "run" an action query, not a select query.
    .

    What did you plan to do with the result?

    You could use it to open a recordset.

  5. #5
    dumbledown is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    46
    Thanks Orange,
    I want to combine the tables (this number varies) to eventually average the combined data outputted into a table (ideally an existing one) so it can be linked to another program.
    Hope that makes sense??

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You could use the SQL as part of an append or make table query, if that's what you want.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    dumbledown is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    46
    Call me an idiot, but aren't I doing that with the INTO statement?

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I'm the idiot.

    My apologies, I only looked at the original SQL. That is certainly a make table query. I've never tried with a union. What error message do you get? If you run the SQL in a query, do you get a better error?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    dumbledown is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    46
    No worries...
    'An action query cannot be used as a row source'
    Thanks for persisting!

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Odd, since you're not trying to use it as a row source (if that's all the code). It occurs to me the final syntax might need to be

    SELECT * INTO newlinks FROM (SELECT * FROM [outputlinks1] UNION ALL SELECT * FROM [outputlinks2] UNION ALL SELECT * FROM [outputlinks3];

    If that doesn't work, can you post the db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    dumbledown is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    46
    Mr Baldy
    Thanks very much for that - it works perfectly, and works in VBA as well!
    Great stuff, have a top weekend

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You too! You must have picked up on the fact that I missed the closing parentheses. I'm having a bad day here!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Access fails export query to XML
    By vinz in forum Access
    Replies: 1
    Last Post: 09-16-2014, 07:31 AM
  2. query fails
    By rjjhome in forum Queries
    Replies: 4
    Last Post: 03-15-2012, 05:19 PM
  3. Replies: 3
    Last Post: 01-15-2012, 02:46 PM
  4. Code works in full, fails in Runtime
    By stephenaa5 in forum Programming
    Replies: 3
    Last Post: 09-14-2010, 12:30 PM
  5. Parameter Query Fails Occasionally
    By jp2access in forum Programming
    Replies: 0
    Last Post: 08-26-2009, 07:33 AM

Tags for this Thread

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