Results 1 to 10 of 10

Thread: Trying to output the data created by TRANSFORM into a local Access table using VBA?

  1. #1

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Trying to output the data created by TRANSFORM into a local Access table using VBA?

    I have this query:

    Code:
    TRANSFORM First (MyData2025.K1) AS FirstOfK1
    SELECT
        MyData2025.Id1,
        MyData2025.Id2,
        MyData2025.Id3,
        MyData2025.TableId
    FROM
        MyData2025
    WHERE
        (((MyData2025.TableId) = "0"))
    GROUP BY
        MyData2025.Id1,
        MyData2025.Id2,
        MyData2025.Id3,
        MyData2025.TableId PIVOT MyData2025.RowNum;
    Its pivoting the data perfectly, except I want to output the data coming from it into a local table.
    But there is a catch, I want the code to be able to run on a button click which means, this needs to be programmed into VBA code.
    I have it as a VBA function but it doesn't work.

    Code:
    Public Function PivotirajSSOM(ByVal MyData2025 As String) As String
        Dim sql As String
        
        sql = "select * into PivotedDataTable from pdtc where (pdtc.Id1, pdtc.Id2, pdtc.Id3, pdtc.TableId) in (TRANSFORM First (" &  MyData2025 & ".K1) AS FirstOfK1 "
        
        sql = sql & "SELECT "
        
        sql = sql & "" &  MyData2025 & ".Id1, "
        sql = sql & "" &  MyData2025 & ".Id2, "
        sql = sql & "" &  MyData2025 & ".Id3, "
        sql = sql & "" &  MyData2025 & ".TableId "
        
        sql = sql & "FROM " &  MyData2025 & " "
        
        sql = sql & "WHERE " &  MyData2025 & ".TableId = ""0"" "
        
        sql = sql & "Group BY "
        
        sql = sql & "" &  MyData2025 & ".Id1, "
        sql = sql & "" &  MyData2025 & ".Id2, "
        sql = sql & "" &  MyData2025 & ".Id3, "
        sql = sql & "" &  MyData2025 & ".TableId PIVOT " &  MyData2025 & ".RowNum) as pdtc "
        
        PivotirajSSOM = sql
    End Function
    
    ' Then it would be called like so
    DoCmd.RunSQL PivotirajSSOM(MyData2025 )
    I am not sure of the syntax for this as I tried few things and it didn't work.

    How can I output date from a Crosstab query into a local table using VBA code?
    Last edited by kutlesh; Feb 9th, 2026 at 07:59 AM.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,261

    Re: Trying to output the data created by TRANSFORM into a local Access table using VB

    This looks convoluted.
    Why are you changing the logic?
    It doesn't make sense to alias your actual Select statement, just to check in your SELECT INTO if it exists in your actual SELECT

    Code:
    select * into PivotedDataTable from (
    TRANSFORM First (MyData2025.K1) AS FirstOfK1
    SELECT
        MyData2025.Id1,
        MyData2025.Id2,
        MyData2025.Id3,
        MyData2025.TableId
    FROM
        MyData2025
    WHERE
        MyData2025.TableId = "0"
    GROUP BY
        MyData2025.Id1,
        MyData2025.Id2,
        MyData2025.Id3,
        MyData2025.TableId PIVOT MyData2025.RowNum)
    ;
    EDIT: And TableID is really a String?
    Last edited by Zvoni; Feb 9th, 2026 at 05:48 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Re: Trying to output the data created by TRANSFORM into a local Access table using VB

    Well i tried "select into from" syntax in the internal select statement. It didn't work!

    Code:
    TRANSFORM First (MyData2025.K1) AS FirstOfK1
    SELECT
        MyData2025.Id1,
        MyData2025.Id2,
        MyData2025.Id3,
        MyData2025.TableId
    into PivotedDataTable 
    FROM
        MyData2025
    WHERE
        MyData2025.TableId = "0"
    GROUP BY
        MyData2025.Id1,
        MyData2025.Id2,
        MyData2025.Id3,
        MyData2025.TableId PIVOT MyData2025.RowNum

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,261

    Re: Trying to output the data created by TRANSFORM into a local Access table using VB

    This is making even less sense.
    Have you tried the SELECT INTO directly in Access?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Re: Trying to output the data created by TRANSFORM into a local Access table using VB

    It works when using the Crosstab query inside another query but with drag and drop.
    Not sure what changes when you recode the query using text and VBA...

  6. #6
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,261

    Re: Trying to output the data created by TRANSFORM into a local Access table using VB

    Just saw something
    sql = sql & "" & MyData2025 & ".Id1, "
    sql = sql & "" & MyData2025 & ".Id2, "
    sql = sql & "" & MyData2025 & ".Id3, "
    sql = sql & "" & MyData2025 & ".TAB PIVOT " & MyData2025 & ".RowNum) as pdtc "

    PivotirajSSOM = sql
    Is this a Typo??
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  7. #7

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Re: Trying to output the data created by TRANSFORM into a local Access table using VB

    Sorry, yes, it was a typo, but it is wrong here in the page, not in my code.
    Should be TableId.
    Last edited by kutlesh; Feb 9th, 2026 at 08:00 AM.

  8. #8
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,261

    Re: Trying to output the data created by TRANSFORM into a local Access table using VB

    OK, something else:
    Code:
    ' Then it would be called like so
    DoCmd.RunSQL PivotirajSSOM(MyData2025 )
    Are you sure??
    Code:
    Public Function PivotirajSSOM(ByVal MyData2025 As String) As String
    expects a String as a Parameter.
    Your example "how it would be called" implies that MyData2025 is a (String-Variable) outside.
    What's the Value of MyData2025 before you call your Sub?

    and is this really correct, that that Parameter contains the TABLE-Name from which to do the Pivot?

    IN a nutshell: Check the Returnvalue of
    "PivotirajSSOM(MyData2025 )" BEFORE Executing it
    Code:
    Dim s  As String
    ' Then it would be called like so
    s = PivotirajSSOM(MyData2025)
    Debug.Print s
    DoCmd.RunSQL s
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  9. #9

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Re: Trying to output the data created by TRANSFORM into a local Access table using VB

    Yea forgot to note that. The function parameter is the name of the local table containing the data.

  10. #10
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,261

    Re: Trying to output the data created by TRANSFORM into a local Access table using VB

    Well, what's the Debug-Output of the query?
    I still think you have a syntax error somewhere
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

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
  •  



Click Here to Expand Forum to Full Width