Results 1 to 4 of 4
  1. #1
    snoopy2003 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    72

    Help with query

    Hello,



    I have 2 queries defined in Access:
    When I run the following query in VBA I get the desired results.
    stSQL = "SELECT * INTO tmpEmpDistPerMan FROM qryCountManagers;"
    DoCmd.RunSQL stSQL

    qryCountManagers

    TRANSFORM Count(qryCountManagersCrosstab.Manager) AS CountOfManager
    SELECT qryCountManagersCrosstab.Manager
    FROM qryCountManagersCrosstab
    GROUP BY qryCountManagersCrosstab.Manager
    PIVOT qryCountManagersCrosstab.EmployeeType;

    qryCountManagersCrosstab

    SELECT tblEmployees.M1 AS Manager, tblEmployees.EmployeeType, tblEmployees.Status
    FROM tblEmployees
    WHERE (Not IsNull(M1) AND Status = 'Active')
    UNION ALL SELECT tblEmployees.M2 AS Manager, tblEmployees.EmployeeType, tblEmployees.Status
    FROM tblEmployees
    WHERE (Not IsNull(M2) AND Status = 'Active')
    UNION ALL SELECT tblEmployees.M3 AS Manager, tblEmployees.EmployeeType, tblEmployees.Status
    FROM tblEmployees
    WHERE (Not IsNull(M3) AND Status = 'Active');


    When I try to define all the queries as 1 query in VBA and run it (as string variable) I get an error - "Error in From".

    stSQL = "SELECT * INTO tmpEmpDistPerMan FROM (" & qryCountManagers & ");"
    DoCmd.RunSQL stSQL

    qryCountManagers is equal to:
    SELECT * INTO tmpEmpDistPerMan FROM (TRANSFORM Count(qryCountManagersCrosstab.Manager) AS CountOfManager SELECT qryCountManagersCrosstab.Manager FROM (SELECT tblEmployees.M1 AS Manager, tblEmployees.EmployeeType, tblEmployees.Status FROM tblEmployees WHERE (Not IsNull(M1) AND Status = 'Active') UNION ALL SELECT tblEmployees.M2 AS Manager, tblEmployees.EmployeeType, tblEmployees.Status FROM tblEmployees WHERE (Not IsNull(M2) AND Status = 'Active') UNION ALL SELECT tblEmployees.M3 AS Manager, tblEmployees.EmployeeType, tblEmployees.Status FROM tblEmployees WHERE (Not IsNull(M3) AND Status = 'Active')) AS qryCountManagersCrosstab GROUP BY qryCountManagersCrosstab.Manager PIVOT qryCountManagersCrosstab.EmployeeType);



    What am I doing wrong ?

    Thank you for your help

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    I recently attempted to run UNION query in Access VBA and it would not work. Would not build recordset from a saved UNION query object nor would it build a recordset with the UNION SQL statement in VBA. Finally gave up and restructured procedure to not need UNION.

    Weird thing is I have VBA in Excel that does build recordset from a saved UNION query.

    The only thing I came up different for your SQL is in the PIVOT clause. Doesn't seem the query name is needed but might try. If I had data I would test.
    PIVOT qryCountManagersCrosstab.EmployeeType) As qryCountManagers;
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    snoopy2003 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    72
    Hello,

    Thank you for your answer.
    I added the query name but it did not help.

    What is the alternative to UNION in VBA ?

    Thank you for your help

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    There is no defined alternative I can provide. Solution would be unique to your data. I had to reconfigure my procedure. I no longer have the old version. If I remember right, my solution was to write data to a temp table. Temp meaning data lives in table for the duration of process and is purged when done.

    In your case, maybe it is the PIVOT that is issue. I don't use many PIVOT queries and never tried coding one. As test, try coding just the UNION part - will the recordset open?

    If you want to provide project, I will test query.
    Last edited by June7; 08-17-2011 at 06:28 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

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