I’m trying to find a way where I can transpose data that I have in a table. Does anyone know how I can do this?
Please see the attached Excel file for a visual explaination.
Thanks,
Drew
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Transpose (Access 2002)
You can use DAO (or ADO) code to loop through the records and fields of the source table, and create records in the target table. See the thread beginning at post 229479 for an example; the next to last post in the thread contains a link to Roger’s Access Library where you can find a demo that may help.
I have attached a demo (Access 2000 format, zipped) based on your example tables. It uses DAO, so if you use the code in your own database, you must select Tools | References… in the Visual Basic Editor and make sure that a reference to the Microsoft DAO 3.6 Object Library is set. Here is the code:
Sub Normalize()
‘ Declarations
Dim dbs As DAO.Database
Dim rstSource As DAO.Recordset
Dim rstTarget As DAO.Recordset
Dim i As Integer
‘ Always set error handling when dealing with objects
On Error GoTo ErrHandler
‘ Set object variables
Set dbs = CurrentDb
Set rstSource = dbs.OpenRecordset(“tblSource”, dbOpenForwardOnly)
Set rstTarget = dbs.OpenRecordset(“tblTarget”, dbOpenDynaset)
‘ Loop through source records
Do While Not rstSource.EOF
For i = 1 To 11
‘ Add new record to target table
rstTarget.AddNew
‘ Set the fields
rstTarget![ITEM_ID] = rstSource![ITEM_ID]
rstTarget![SHORT DESC] = rstSource![SHORT DESC]
rstTarget![LONG DESC] = rstSource.Fields(“LONG DESC ” & i)
rstTarget.Update
Next i
‘ And on to the next
rstSource.MoveNext
Loop
ExitHandler:
‘ Make sure that recordsets are closed
‘ and object memory is released
On Error Resume Next
rstTarget.Close
Set rstTarget = Nothing
rstSource.Close
Set rstSource = Nothing
Set dbs = Nothing
Exit Sub
ErrHandler:
‘ Inform user, then clean up
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub
In your own database, you must replace “tblSource” and “tblTarget” by the names of your tables. You must already have designed the target table. If the field names in your spreadsheet were just examples, you must adapt the code to use the correct names.
Oh yeah, to run the code in the demo database, activate the Visual Basic Editor (Alt+F11), open the module basNormalize if necessary, click somewhere in the procedure and press F5. Execution in the demo is instantaneous, but for a table with thousands of records, it may take some time.
It is possible to write similar code to denormalize, i.e. to reconstruct the original table. There is a sample database from Roger’s Access Library that shows both how to normalize and how to denormalize a table.
It is possible to write similar code to denormalize, i.e. to reconstruct the original table. There is a sample database from Roger’s Access Library that shows both how to normalize and how to denormalize a table.
Hans,
I have a similar table that I’m trying to transpose(See Spreadsheet)
The difference is that every 6th row I have to start over again and populate the columns.
If I step through my code I can get it to work for the first model in the example and up to the first column of the second model. My problem is getting back to the 6th, 12th… row when I loop to edit the row.
Function CreateExcelTable() Dim db As Database Dim rst As Recordset Dim rst1 As Recordset Dim V, W, X, Y, Z As Integer Dim strSQL As String Dim strNAME, strNAME1, strWHERE As String Dim qdf As QueryDef Dim strFieldCount As Integer Set db = CurrentDb Set rst = db.OpenRecordset("qryPerformance", dbOpenDynaset) strFieldCount = rst.Fields.Count DoCmd.SetWarnings False DoCmd.RunSQL "Delete tblExcel.* From tblExcel;" DoCmd.SetWarnings True Set rst1 = db.OpenRecordset("tblExcel") W = 1 X = 1 Y = 1 Z = 1 Do While Not rst.EOF Do Until X = 6 Select Case W Case 1 rst1.AddNew Case 2 rst1.Edit End Select rst1!Model = rst!Model rst1!EndDate = rst!End_Date rst1!Item = rst.Fields(Y).Name Select Case Z Case 1 rst1![175] = rst.Fields(Y).Value End Select Y = Y + 1 rst1.Update X = X + 1 If Z > 1 Then rst1.MoveNext V = V + 1 Loop 'x = 6 rst.MoveNext Select Case V Case Is < 25 rst1.MoveFirst W = 2 Z = Z + 1 Case Else Z = 1 V = 1 W = 1 End Select X = 1 Y = 1 Loop End Function
I’m getting loopy trying to figure this out.
Scott
Hans happens to be on vacation at the moment, but one potential problem with your code is that you are deleting everything in tblExcel and then opening a recordset on that table and saying “Do While Not rst.EOF”, which means the code should never execute, so I don’t know how you’re getting it to run the first time.
A quick debug of the code and I see the following :
In the Select Case V, Case Else you set W = 3. In the Select Case W there is no Case 3. So you don’t have an append or an edit.
That’s why you get an error in line: rst1!Model = rst!Model ”””errors on this line
If I change W=1, the code run but you have another problem:
In the Select Case V, Case is <25, you do a rst1.MoveFirst. This is wrong, you should do a rst.FindFirst for the value of ModelY
Hope this help.
Francois,
Thank you, Thank you. I don’t do enough with recordsets to be fully proficient in them.
I can rest my brain for awhile.
A couple of questions. What do I gain with the dbOpenDynaset ? I thought that was used for large recordsets.
Also I removed the rst1.AddNew from the Case Else of the Select Case V statement since it is taken care of at the beginning of the loop.
Thanks again for your work, I owe you one…or two
Francois,
Thank you, Thank you. I don’t do enough with recordsets to be fully proficient in them.
I can rest my brain for awhile.
A couple of questions. What do I gain with the dbOpenDynaset ? I thought that was used for large recordsets.
Also I removed the rst1.AddNew from the Case Else of the Select Case V statement since it is taken care of at the beginning of the loop.
Thanks again for your work, I owe you one…or two
A quick debug of the code and I see the following :
In the Select Case V, Case Else you set W = 3. In the Select Case W there is no Case 3. So you don’t have an append or an edit.
That’s why you get an error in line: rst1!Model = rst!Model ”””errors on this line
If I change W=1, the code run but you have another problem:
In the Select Case V, Case is <25, you do a rst1.MoveFirst. This is wrong, you should do a rst.FindFirst for the value of ModelY
Hope this help.
Hans happens to be on vacation at the moment, but one potential problem with your code is that you are deleting everything in tblExcel and then opening a recordset on that table and saying “Do While Not rst.EOF”, which means the code should never execute, so I don’t know how you’re getting it to run the first time.
Hans,
I have a similar table that I’m trying to transpose(See Spreadsheet)
The difference is that every 6th row I have to start over again and populate the columns.
If I step through my code I can get it to work for the first model in the example and up to the first column of the second model. My problem is getting back to the 6th, 12th… row when I loop to edit the row.
Function CreateExcelTable() Dim db As Database Dim rst As Recordset Dim rst1 As Recordset Dim V, W, X, Y, Z As Integer Dim strSQL As String Dim strNAME, strNAME1, strWHERE As String Dim qdf As QueryDef Dim strFieldCount As Integer Set db = CurrentDb Set rst = db.OpenRecordset("qryPerformance", dbOpenDynaset) strFieldCount = rst.Fields.Count DoCmd.SetWarnings False DoCmd.RunSQL "Delete tblExcel.* From tblExcel;" DoCmd.SetWarnings True Set rst1 = db.OpenRecordset("tblExcel") W = 1 X = 1 Y = 1 Z = 1 Do While Not rst.EOF Do Until X = 6 Select Case W Case 1 rst1.AddNew Case 2 rst1.Edit End Select rst1!Model = rst!Model rst1!EndDate = rst!End_Date rst1!Item = rst.Fields(Y).Name Select Case Z Case 1 rst1![175] = rst.Fields(Y).Value End Select Y = Y + 1 rst1.Update X = X + 1 If Z > 1 Then rst1.MoveNext V = V + 1 Loop 'x = 6 rst.MoveNext Select Case V Case Is < 25 rst1.MoveFirst W = 2 Z = Z + 1 Case Else Z = 1 V = 1 W = 1 End Select X = 1 Y = 1 Loop End Function
I’m getting loopy trying to figure this out.
Scott
I have attached a demo (Access 2000 format, zipped) based on your example tables. It uses DAO, so if you use the code in your own database, you must select Tools | References… in the Visual Basic Editor and make sure that a reference to the Microsoft DAO 3.6 Object Library is set. Here is the code:
Sub Normalize()
‘ Declarations
Dim dbs As DAO.Database
Dim rstSource As DAO.Recordset
Dim rstTarget As DAO.Recordset
Dim i As Integer
‘ Always set error handling when dealing with objects
On Error GoTo ErrHandler
‘ Set object variables
Set dbs = CurrentDb
Set rstSource = dbs.OpenRecordset(“tblSource”, dbOpenForwardOnly)
Set rstTarget = dbs.OpenRecordset(“tblTarget”, dbOpenDynaset)
‘ Loop through source records
Do While Not rstSource.EOF
For i = 1 To 11
‘ Add new record to target table
rstTarget.AddNew
‘ Set the fields
rstTarget![ITEM_ID] = rstSource![ITEM_ID]
rstTarget![SHORT DESC] = rstSource![SHORT DESC]
rstTarget![LONG DESC] = rstSource.Fields(“LONG DESC ” & i)
rstTarget.Update
Next i
‘ And on to the next
rstSource.MoveNext
Loop
ExitHandler:
‘ Make sure that recordsets are closed
‘ and object memory is released
On Error Resume Next
rstTarget.Close
Set rstTarget = Nothing
rstSource.Close
Set rstSource = Nothing
Set dbs = Nothing
Exit Sub
ErrHandler:
‘ Inform user, then clean up
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub
In your own database, you must replace “tblSource” and “tblTarget” by the names of your tables. You must already have designed the target table. If the field names in your spreadsheet were just examples, you must adapt the code to use the correct names.
Oh yeah, to run the code in the demo database, activate the Visual Basic Editor (Alt+F11), open the module basNormalize if necessary, click somewhere in the procedure and press F5. Execution in the demo is instantaneous, but for a table with thousands of records, it may take some time.
You can use DAO (or ADO) code to loop through the records and fields of the source table, and create records in the target table. See the thread beginning at post 229479 for an example; the next to last post in the thread contains a link to Roger’s Access Library where you can find a demo that may help.
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications