Pivot Records

  • Thread starter Thread starter bigbore50
  • Start date Start date
B

bigbore50

I hope I can do this

I have a query that shows

Primary# , Date, #sales.... etc

In access they all show up in record form

Primary# , Date, #sales.... etc
12345 12/12/06 50
23456 12/20/06 30


What if i wanted to show this in column form with all the same info

Primary # 12345 23456
Date 12/12/06 12/20/06
Sales 50 30

Can this be done?
Currently i am exporting the query to excel and doing
I select the values and then Transpose them


Is there a better way?

Thanks a lot
 
I want to export the report into excel
Is this possible?
I tried to make a report and then export to excel and it shows the same
as my query.

I will never have 300 records.
Only a max of 50

I need it to look that way but i can't get it to work
can i use a crosstab query?
 
Apologies for butting in...

I always just do this in Excel, but if you were
looking for a VBA function that may work for you
(I've never used this)...

http://groups.google.com/group/microsoft.public.access.forms/msg/9b0afb0fe44f5820?&hl=en

if you decide to use the Transposer function,
remember to disambiguate DIM's with "DAO," i.e.,
(and, of course, set reference to DAO in References)
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
 
I just tested and following worked for your sample data....

Copy following code into new module.
Save module as (say) "modTranspose"
In top menu, click on Debug/Compile to make
sure no problems with wordwrap, etc.

'***start new code****
Option Explicit

Public Function TableExists(strTableName As String) As Boolean
'from Joe Fallon
On Error Resume Next
TableExists = IsObject(CurrentDb.TableDefs(strTableName))
End Function

'following function adapted from code by Amy Vargas:
'2. To test the function, open the Debug window (or the Immediate window
' in Microsoft Access version 2.0). If you are in the sample database
' Northwind.mdb (or Nwind.mdb), for example, and you want to transpose
' the Suppliers table, type the following line, and then press ENTER:
'
' "?Transposer("Suppliers","SuppliersTrans")"
'
'(without the quotation marks)
'
'Regards,
'
'Amy Vargo
'Microsoft Access Engineer

Function Transposer(strSource As String, strTarget As String) As Boolean

Dim db As DAO.Database
Dim tdfNewDef As DAO.TableDef
Dim fldNewField As DAO.Field
Dim rstSource As DAO.Recordset
Dim rstTarget As DAO.Recordset
Dim i As Long, j As Long

On Error GoTo Transposer_Err

Set db = CurrentDb()
Set rstSource = db.OpenRecordset(strSource)
rstSource.MoveLast

'delete target table if it exists
If TableExists(strTarget) = True Then
db.Execute "DROP TABLE " & strTarget, dbFailOnError
Else
'did not exist
End If

' 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

rstSource.Close
rstTarget.Close
db.Close
Transposer = True
MsgBox "Successfully transposed " & strSource & " to " & strTarget

Exit_Transposer:
If Not rstSource Is Nothing Then Set rstSource = Nothing
If Not rstTarget Is Nothing Then Set rstTarget = Nothing
If Not db Is Nothing Then Set db = Nothing

Exit Function

Transposer_Err:
Transposer = False
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

Resume Exit_Transposer

End Function
'***end new code***
 
Of course your source table/query can not
have more than 255 records. You may want to
adjust code to test source recordcount and at
least return first 255 if recordcount was more
(plus message that did not transpose all data)...
 
Thank you very much

I think this will work


Gary said:
Of course your source table/query can not
have more than 255 records. You may want to
adjust code to test source recordcount and at
least return first 255 if recordcount was more
(plus message that did not transpose all data)...

"Gary Walter" wrote
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top