Transpose Query to a Table (1 Viewer)

jdlewin1

Registered User.
Local time
Today, 00:01
Joined
Apr 4, 2017
Messages
92
Hi,

I have a query which holds a set of information for a particular asset. One record for each year say showing only 6 records. I want to transpose this data so that the rows are columns and the columns are rows so i can use it in a report.

Does anyone have any code to transpose data in Access?

Cheers,
Jdlewin
 

jdlewin1

Registered User.
Local time
Today, 00:01
Joined
Apr 4, 2017
Messages
92
As far as I can tell a crosstab query wont do the job.

I literally want to take the query results and turn them on their side. So say the query results are as below (i have 6 records but included 3 for example purposes):

Header : Sample ID........Sample Date.....Sample Temp.....Result1.....Result2...
Record1: A2006/01/01.....24/05/16...........13.2.................34............549
Record2: A0869/04/01.....04/02/16...........8.1...................4.............516
Record3: A0869/03/03.....16/01/15...........13.6.................33............1726

I want it to end up like the below:

Header............Record1............Record2...........Record3...
Sample ID........A2006/01/01.....A0869/04/01.....A0869/03/03
Sample Date.....24/05/16..........04/02/16..........16/01/15
Sample Temp....13.2................8.1..................13.6
Result1............34...................4.....................33
Result2............549.................516..................1726

Hope that explains things....
 

Minty

AWF VIP
Local time
Today, 00:01
Joined
Jul 26, 2013
Messages
10,371
I'm not aware of any easy way to do that in Access since they removed the pivot option. I'd probably export to Excel and move it around there.

It's quite hard to make reports with variable numbers of columns.

The only other route I can think of would be to code a function to create a temp table with the desired layout and base your report on that.
 

static

Registered User.
Local time
Today, 00:01
Joined
Nov 2, 2015
Messages
823
Create a new form.
Add a button and a web browser control.
Paste the code into the forms module.

You can copy/paste the result into Excel, Word or whatever.

Code:
Private Sub Command0_Click()
    Const tbl As String = "table1" '<== CHANGE THIS

    Dim f As DAO.Field
    
    With CurrentDb.OpenRecordset(tbl)
    
        s = "<table border='1' cellspacing='0px' width='100%'>"
        For Each f In .Fields
            .MoveFirst
            s = s & "<tr>"
            s = s & "<td style='color:blue;font-weight:bold;'>" & f.Name & "</td>"
            Do Until .EOF
                s = s & "<td>" & .Fields(f.Name) & "</td>"
                .MoveNext
            Loop
            s = s & "</tr>"
            s = s & vbNewLine
        Next
        s = s & "</table>"
        .Close
    End With
    WebBrowser1.Document.body.innerhtml = s

End Sub

Private Sub Form_Open(Cancel As Integer)
    WebBrowser1.Navigate "about:blank"
End Sub
 

jdlewin1

Registered User.
Local time
Today, 00:01
Joined
Apr 4, 2017
Messages
92
Please can anyone help!?

I want to be able to click a command button on a form to remove any existing data in my table so new can be added and then to transpose what ever is in my query holding the info into the table.

My data is in a query called "TOA_Report_History" and i want it to go into a table called "TOA_Report_History_Transpose".

I have attached a cut down version database to make things easier (hopefully).

I found the following code:

Anyone know if this will work and also how to get it to work?

Code:
Function Transposer(strSource As String, strTarget As String) 
     
    Dim db As DAO.Database 
    Dim tdfNewDef As DAO.TableDef 
    Dim fldNewField As DAO.Field 
    Dim rstSource As DAO.Recordset, rstTarget As DAO.Recordset 
    Dim i As Integer, j As Integer 
     
    On Error Goto Transposer_Err 
     
    Set db = CurrentDb() 
    Set rstSource = db.OpenRecordset(strSource) 
    rstSource.MoveLast 
     
     ' Create a new table to hold the transposed data.
     ' Create a field for each record in the original table.
    Set tdfNewDef = db.CreateTableDef(strTarget) 
    For i = 0 To rstSource.RecordCount 
        Set fldNewField = tdfNewDef.CreateField(CStr(i + 1), dbText) 
        tdfNewDef.Fields.Append fldNewField 
    Next i 
    db.TableDefs.Append tdfNewDef 
     
     ' Open the new table and fill the first field with
     ' field names from the original table.
    Set rstTarget = db.OpenRecordset(strTarget) 
    For i = 0 To rstSource.Fields.Count - 1 
        With rstTarget 
            .AddNew 
            .Fields(0) = rstSource.Fields(i).Name 
            .Update 
        End With 
    Next i 
     
    rstSource.MoveFirst 
    rstTarget.MoveFirst 
     ' Fill each column of the new table
     ' with a record from the original table.
    For j = 0 To rstSource.Fields.Count - 1 
         ' Begin with the second field, because the first field
         ' already contains the field names.
        For i = 1 To rstTarget.Fields.Count - 1 
            With rstTarget 
                .Edit 
                .Fields(i) = rstSource.Fields(j) 
                rstSource.MoveNext 
                .Update 
            End With 
             
        Next i 
        rstSource.MoveFirst 
        rstTarget.MoveNext 
    Next j 
     
    db.Close 
     
    Exit Function 
     
Transposer_Err: 
     
    Select Case Err 
    Case 3010 
        MsgBox "The table " & strTarget & " already exists." 
    Case 3078 
        MsgBox "The table " & strSource & " doesn't exist." 
    Case Else 
        MsgBox CStr(Err) & " " & Err.Description 
    End Select 
     
    Exit Function 
     
End Function

Many Thanks!!!!!!
 

Attachments

  • Transposer.accdb
    516 KB · Views: 278

jdlewin1

Registered User.
Local time
Today, 00:01
Joined
Apr 4, 2017
Messages
92
Hi Minty / Static,

Sorry missed your posts! thanks for the response.

Can the code in my post above work?
 

jdlewin1

Registered User.
Local time
Today, 00:01
Joined
Apr 4, 2017
Messages
92
Hi Galax,

I dont think that will do it....i dont want to concatenate anything just turn the query on its side to display the results in a column view instead of a row view for my report.
 

Minty

AWF VIP
Local time
Today, 00:01
Joined
Jul 26, 2013
Messages
10,371
It's not concatenating - That's just the thread title, that query should give you what you need I think. (Damn clever as well)
 

jdlewin1

Registered User.
Local time
Today, 00:01
Joined
Apr 4, 2017
Messages
92
Lol i have no idea how to implement that on my data....newbie to all this!
 

Minty

AWF VIP
Local time
Today, 00:01
Joined
Jul 26, 2013
Messages
10,371
What have you tried? - That query should be easy to adapt to your table.
 

jdlewin1

Registered User.
Local time
Today, 00:01
Joined
Apr 4, 2017
Messages
92
I tried the query you referenced but that groups them by the value / item in column 1.

I need my new column 1 to be the field names in the query and then column 2 is the record 1 results followed by column 2 as the record 2 results etc (max of 6 records), so like below:
Query results (max of 6 records):

Header : Sample ID........Sample Date.....Sample Temp.....Result1.....Result2...
Record1: A2006/01/01.....24/05/16...........13.2.................34............54 9
Record2: A0869/04/01.....04/02/16...........8.1...................4.............5 16
Record3: A0869/03/03.....16/01/15...........13.6.................33............17 26

The header row becomes the 1st column and the 1st record (row) becomes the 2nd column, the 2nd record (row) becomes the 3rd column etc up to 6 records:

Header............Record1............Record2...... .....Record3...
Sample ID........A2006/01/01.....A0869/04/01.....A0869/03/03
Sample Date.....24/05/16..........04/02/16..........16/01/15
Sample Temp....13.2................8.1..................1 3.6
Result1............34...................4......... ............33
Result2............549.................516........ ..........1726

Any ideas?
 

isladogs

MVP / VIP
Local time
Today, 00:01
Joined
Jan 14, 2017
Messages
18,247
As Minty has already suggested, I would export to Excel & rearrange the data there. You can then reimport back to Access if you need to do so
 

jdlewin1

Registered User.
Local time
Today, 00:01
Joined
Apr 4, 2017
Messages
92
Hi Ridders,

As always thanks for your help!

I was hoping to be able to automate the process so others could generate the data with just clicking on a button or two.

I have created a work around where the data is exposed to excel. The user then still has to do a copy and transpose paste in excel and then copy it back into access.

So not really ideal for the users!
 

static

Registered User.
Local time
Today, 00:01
Joined
Nov 2, 2015
Messages
823
I just followed the instructions somebody gave in post #5 and it seemed to do the trick. Did you try it?
 

Users who are viewing this thread

Top Bottom