473,378 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Show rows into columns directly

3
Hi all, Please help me.
I want to convert all rows from my table into columns (to show on report). I tried pivot, but it is not suitable, there are no any aggregate column, i just need to directly rotate the table as columns become rows and rows become columns.
My table looks like this:

Name | ID | Age | Interest
Donna | 1 | 20 | Music
Paul | 2 | 23 | Sport
Mike | 3 | 18 | Sport
Ann | 4 | 19 | Dancing

my report must show the data like this:
Name: | Donna | Paul | Mike | Ann
ID: | 1 | 2 | 3 | 4
Age: | 20 | 23 | 18 | 19
Interest | Music | Sport | Sport | Dancing

Thank you for help!
Feb 13 '11 #1

✓ answered by ADezii

As long as the Data remains simple as to the Number of Records and Fields, so can the solution.
  1. Create a Function named fTranspose_2(). This Function will consist of a single Parameter, the Name of the Table whose Data you wish to Transpose and display. The Function will actually Transpose the Data (flip Rows and Columns), and build a String consisting of all the Data.
    Expand|Select|Wrap|Line Numbers
    1. Function fTranspose_2(strSource As String)
    2. On Error GoTo fTranspose_2_Err
    3. Dim MyDB As DAO.Database
    4. Dim rstSource As DAO.Recordset
    5. Dim inFldCtr As Integer
    6. Dim strBuild As String
    7.  
    8.  
    9. Set MyDB = CurrentDb()
    10. Set rstSource = MyDB.OpenRecordset(strSource)
    11. rstSource.MoveLast: rstSource.MoveFirst
    12.  
    13.  
    14. 'The Transposing is done within this Nested Structure
    15. With rstSource
    16.   For inFldCtr = 0 To .Fields.Count - 1
    17.    strBuild = strBuild & .Fields(inFldCtr).Name & Space$(10)
    18.     Do While Not .EOF
    19.         strBuild = strBuild & .Fields(inFldCtr).Value & Space$(10)
    20.       .MoveNext
    21.     Loop
    22.       strBuild = strBuild & vbCrLf
    23.       .MoveFirst
    24.   Next inFldCtr
    25. End With
    26.  
    27. rstSource.Close
    28. Set rstSource = Nothing
    29.  
    30. fTranspose_2 = strBuild
    31.  
    32. Exit_fTranspose_2:
    33.   Exit Function
    34.  
    35. fTranspose_2_Err:
    36.   MsgBox Err.Description, vbExclamation, "Error in fTranspose()"
    37.     Resume Exit_fTranspose_2:
    38. End Function
  2. On your Report, create a Text Box in the Detail, or other Section, the width of the Report itself. The Report should probably be in Landscape Orientation.
  3. Set the Can Grow Property of the Text Box to Yes.
  4. Set the Control Source of the Text Box equal to the Function Call, as in:
    Expand|Select|Wrap|Line Numbers
    1. =fTranspose_2("tblTest")
  5. Download the Attachment to get a much clearer picture of the solution.
  6. You can also Format the Data in the Text Box for a better look.

7 5400
ADezii
8,834 Expert 8TB
As long as the Data remains simple as to the Number of Records and Fields, so can the solution.
  1. Create a Function named fTranspose_2(). This Function will consist of a single Parameter, the Name of the Table whose Data you wish to Transpose and display. The Function will actually Transpose the Data (flip Rows and Columns), and build a String consisting of all the Data.
    Expand|Select|Wrap|Line Numbers
    1. Function fTranspose_2(strSource As String)
    2. On Error GoTo fTranspose_2_Err
    3. Dim MyDB As DAO.Database
    4. Dim rstSource As DAO.Recordset
    5. Dim inFldCtr As Integer
    6. Dim strBuild As String
    7.  
    8.  
    9. Set MyDB = CurrentDb()
    10. Set rstSource = MyDB.OpenRecordset(strSource)
    11. rstSource.MoveLast: rstSource.MoveFirst
    12.  
    13.  
    14. 'The Transposing is done within this Nested Structure
    15. With rstSource
    16.   For inFldCtr = 0 To .Fields.Count - 1
    17.    strBuild = strBuild & .Fields(inFldCtr).Name & Space$(10)
    18.     Do While Not .EOF
    19.         strBuild = strBuild & .Fields(inFldCtr).Value & Space$(10)
    20.       .MoveNext
    21.     Loop
    22.       strBuild = strBuild & vbCrLf
    23.       .MoveFirst
    24.   Next inFldCtr
    25. End With
    26.  
    27. rstSource.Close
    28. Set rstSource = Nothing
    29.  
    30. fTranspose_2 = strBuild
    31.  
    32. Exit_fTranspose_2:
    33.   Exit Function
    34.  
    35. fTranspose_2_Err:
    36.   MsgBox Err.Description, vbExclamation, "Error in fTranspose()"
    37.     Resume Exit_fTranspose_2:
    38. End Function
  2. On your Report, create a Text Box in the Detail, or other Section, the width of the Report itself. The Report should probably be in Landscape Orientation.
  3. Set the Can Grow Property of the Text Box to Yes.
  4. Set the Control Source of the Text Box equal to the Function Call, as in:
    Expand|Select|Wrap|Line Numbers
    1. =fTranspose_2("tblTest")
  5. Download the Attachment to get a much clearer picture of the solution.
  6. You can also Format the Data in the Text Box for a better look.
Attached Files
File Type: zip Transpose.zip (28.2 KB, 226 views)
Feb 13 '11 #2
Stewart Ross
2,545 Expert Mod 2GB
There is no built-in way to do this in Access nor in SQL itself. This is not what relational databases were designed to do. In relational theory each row shown in a table or query should be unique, and represent either atomic (indivisible) data about the unique entry (such as a name or interest as you have shown), or aggregate data about the unique row entry (for example, a summary row in a purchase system showing a customer's basic details alongside the number of items the customer has purchased this year and their total value).

Transposing data in the way you suggest breaks relational rules because the row data is no longer atomic or aggregate data for a single unique entity; instead it is attempting to make the row consist of a theoretically unlimited number of columns with no commonality of unique entity at all.

As this is not what relational database are designed to do there is no built-in method of doing so, by SQL or any other means. You can adopt a programmed solution, as ADezii recommends, but be aware that you need to have very limited numbers of rows for such approaches to work. In database tables consisting of thousands of rows there is no way this can be advised.

In Excel it is possible to use the Transpose facility which can turn round all the row-oriented data into column-oriented format, and it is Excel I'd suggest you look at and not Access to do what you ask.

-Stewart

PS ADezii shows you a code solution which you really should look at - but I would still say Excel is an easier option in general...
Feb 13 '11 #3
Rabbit
12,516 Expert Mod 8TB
You're not aggregating any data but you can pivot and "aggregate" on the max or first.
Feb 13 '11 #4
Buyaa
3
I have tried such aggregate functions, but it is not working and i thought pivot is not suitable for this purpose. If possible please show me the solution, thank you
Feb 14 '11 #5
Buyaa
3
Thank you ADezii. Your solution seems to work. But my db is MS Access and language I'm using .Net C# (report wizard). I don't know how to call function in the report. Anyway I'll go trough carefully on your solution and maybe i could found the solution. Thank you
Feb 14 '11 #6
ADezii
8,834 Expert 8TB
Try Reposting this question in the appropriate Forum, then see what happens.
Feb 14 '11 #7
Rabbit
12,516 Expert Mod 8TB
You would have to do a 3 pivots and union them together if you want to do it all in SQL.
Feb 14 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: WindAndWaves | last post by:
Hi Gurus I was wondering if you can send me in the right direction: I have a table with about 300 rows. I want to make all of them invisible and when a user enters a code in a form then make...
0
by: Benny Raymond | last post by:
I just wanted to figure out a way to have a list view work like the microsoft explorer list view, so I could show/hide columns with a right click - anyone know the best way of going about this? ...
2
by: J055 | last post by:
Hi I've been looking into different ways of formatting columns/rows in the GridView control. I realize now, I think, that once the datasource is bound to the control the original column type...
11
by: jimstruckster | last post by:
I have a table with 10 rows, I want all rows except for the first to be hidden when the page first opens up. If the user puts a value in a text box in the first row then I want the second row to...
4
by: jeanlee | last post by:
I have a crosstab query of events by month, and many months do not have any events. How can I make the crosstab show the months (crosstab rows) that don't have any events? Is there a shortcut? ...
3
by: Rambaldi | last post by:
Although i'm working on ASP.net, i think the problem i'm facing is related to my javascript function so i hope i create the thread in the right section. My goal is to create a table dynamically,...
0
by: yoav.sagi | last post by:
hi, i am Inheriting from TableLayoutPane with a designer that inherits from ParentControlDesigner ..i would like to allow the user to resize the columns and rows ..All rows and columns SizeType =...
0
by: cadab | last post by:
I have a data grid view, i have specified several columns through the designer that i would like to show on the grid, i have mapped them to the datasource, this works fine, the problem is, my data...
6
by: viki1967 | last post by:
Hi all. I need your help. I realize this script and I do not know where to begin: 1) A simple grid rows / columns. 2) The first column contain an input type = "checkbox" 3) When select...
3
by: ronakinuk | last post by:
how can i unhide rows/columns in excel 2007
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.