Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2019
    Posts
    1,038

    Need crosstab/transform query which also slightly "re-arranges" table output

    Experts:

    Good morning -- I need some assistance with a, e.g., Crosstab/Transform query which transposed AND slightly re-arranges a table automatically.

    Attached DB contains 2 tables:
    1. Table [01_tbl_SampleRecords] -- this is my source table that needs to be transposed/re-arranged.
    2. Table [02_tbl_SampleRecords_Transposed] -- this is merely a depiction as to how the query's output should look like upon execution.

    Process:
    - To better illustrated the transpose/re-arrange process, I have included a spreadsheet which summarizes the "3-step" process.

    Note:
    - As indicated in the XLSX, please keep in mind that field [ID] is stored as data type = "text" (vs. number).

    Does anyone have recommendations for creating a, e.g., crosstab query which automatically outputs what is depicted in step 3 (aka see JPG & 2nd table in the DB)?



    Thank you,
    Tom

    P.S. In my actual table, I do have approximately fifty (50) fields that will have to be turned from fields into records. Same process applies though (instead of turning {CITY, STATE, ZIP} into rows, it will be those 3 + another ~47 fields.
    P.S.S. If a crosstab won't be able to accomplish it, I'd be ok using VBA (if necessary).
    Attached Thumbnails Attached Thumbnails Crosstab.JPG  
    Attached Files Attached Files
    Last edited by skydivetom; 11-19-2022 at 06:17 AM.

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Tom,

    Not exactly sure if this is what you're looking for.

    Created a table T2j

    Click image for larger version. 

Name:	T2jDesign.PNG 
Views:	23 
Size:	5.6 KB 
ID:	49159


    Click image for larger version. 

Name:	T2j.PNG 
Views:	22 
Size:	13.6 KB 
ID:	49158


    And a module.

    I have returned your zip file with the new table and module.
    Attached Files Attached Files

  3. #3
    Join Date
    Feb 2019
    Posts
    1,038
    Jack:

    PERFECT!!!! That's exactly what I was looking for. Kinda "ironic" that you chose VBA. I was about to close down the "query question" and post a new thread under the "Programming" forum. I didn't have to... and hopefully it would not have been in violation. Anyhow, I love your VBA function.

    Now, I changed the sample data table w/ a sample table containing *actual* data and the 50+ fields. Given that some fields have NULL values though, I'm running into an error.

    How can the VBA be modified so that everything works the same (just like in the City/State/Zip example) but I can process the 50 fields where some contain NULL values?

    Thank you... this is HUGE already!!!

    Cheers,
    Tom
    Attached Files Attached Files

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    If a field is null, what value do you want to output?

    These are empty text fields (ZLS); I changed to variant with CVar and then used NZ to put in a replacement value.

    If you change line 70, put whatever you want where I have "-----N/A" in BLUE

    sLegVal = Nz(CVar(r1.Fields(i).Value), "-----N/A")

    Sample output
    Click image for larger version. 

Name:	Tom_Nulls.PNG 
Views:	24 
Size:	30.5 KB 
ID:	49161

  5. #5
    Join Date
    Feb 2019
    Posts
    1,038
    Jack:

    Sorry for the delayed response... yes, using the "-----N/A" works great!!

    Using it on the full data set (i.e., hundreds of records * 57 fields) w/o any issues. BRILLIANT!!!

    Many thanks... I love this solution!

    Cheers,
    Tom

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,892
    A UNION query can also produce this output. Example with your original dummy data:

    SELECT ID, 1 AS Seq, "ID" AS FieldName, ID AS Legacy_Value FROM 01_tbl_SampleRecords
    UNION SELECT ID, 2, "City", City FROM 01_tbl_SampleRecords
    UNION SELECT ID, 3, "State", State FROM 01_tbl_SampleRecords
    UNION SELECT ID, 4, "ZIP", ZIP FROM 01_tbl_SampleRecords;

    If you're happy with VBA solution, probably want to stick with it. Building a 50-line UNION can be tedious. Also, limit is 50 SELECT lines.

    Why do you need the ID rows?

    If you need to repeat this process and don't want to append to existing records but always start from empty table, add a DELETE action to beginning of code.

    CurrentDb.Execute "DELETE FROM TJ2"
    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.

  8. #8
    Join Date
    Feb 2019
    Posts
    1,038
    Hi June7:

    thank you for the additional recommendation. At some point, I actually did look into the UNION SELECT. Once I realized that I only can concatenate up to 50 lines, it was no longer an option (given I need to transform 50+ fields).

    As you noticed, the VBA routine is very dynamic and does NOT have that limitation.

    Jack's VBA works 100% as envisioned... couldn't ask for a better/more flexible solution. Thanks for chiming in though. 'Much appreciated.

  9. #9
    Join Date
    Apr 2017
    Posts
    1,679
    Quote Originally Posted by skydivetom View Post
    At some point, I actually did look into the UNION SELECT. Once I realized that I only can concatenate up to 50 lines, it was no longer an option (given I need to transform 50+ fields).
    Another reason to use SQL Server as DB part (back-end)! A couple of weeks ago I had to run a query with (26 000 +) UNION SELECT's Had to use Excel to compose query text rows, and then pasted the result into SQL Server query editor.

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

Similar Threads

  1. Replies: 1
    Last Post: 01-30-2022, 11:05 PM
  2. Replies: 5
    Last Post: 04-08-2021, 09:15 AM
  3. Simple table relationships ("faces" to "spaces" to "chairs")
    By skydivetom in forum Database Design
    Replies: 36
    Last Post: 07-20-2019, 01:49 PM
  4. sort order of the date is "slightly" off...
    By markjkubicki in forum Queries
    Replies: 14
    Last Post: 06-16-2015, 12:36 PM
  5. Replies: 4
    Last Post: 10-16-2013, 01:39 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