Solved

Access Table Transpose Column Name as first column

Posted on 2011-09-15
9
435 Views
Last Modified: 2012-05-12
Hi,

I found some transposer VBA that allows me to transpose my table (http://support.microsoft.com/kb/182822).  The transposer puts the row number into the header of each column.  I would like the first column into headers.  Anything thoughts?

Here is the current transposed table
From  (Headers Bolded)

Column1    Column2
Name         Bob
Phone        111-111-1111

To (Headers Bolded)
1               2            3
Column1   Name     Phone
Column2   Bob        111-111-1111


I would like it to look like this (Headers Bolded):

Column1   Name     Phone
Column2   Bob        111-111-1111
0
Comment
Question by:atljarman
  • 5
  • 4
9 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
0
 

Author Comment

by:atljarman
Comment Utility
capricorn1,

Their main field is Month and my table is Field.  To reflect this, I've changed part of the code to:


sFld = "[Field] Text"
Do Until rstSource.EOF
    sFld = sFld & "," & "[" & rstSource!field & "] Number"
rstSource.MoveNext
Loop

I get an error "3421 Data type conversion error."   I've deleted rows, columns based on data type, and haven't found where the issue is coming from.

Do you know where there error is coming from.


--------------------------------------------------

table structure:

Field   Current
ID        1
Date    11/1/2010
Name   Bob


---------------------------------------------------

Function Transposer2(strSource As String, strTarget As String)

        If TableExists(strTarget) Then
            CurrentDb.TableDefs.Delete (strTarget)
        End If
       
       
   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
   
Dim sFld
   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("[" & rstSource(0) & "]"), dbText)
        '      tdfNewDef.Fields.Append fldNewField
        '   Next i
        '   db.TableDefs.Append tdfNewDef

sFld = "[Field] Text"
Do Until rstSource.EOF
    sFld = sFld & "," & "[" & rstSource!field & "] Number"
rstSource.MoveNext
Loop
If DCount("*", "msysobjects", "name='" & strTarget & "'") > 0 Then
    CurrentDb.Execute "drop table " & strTarget
End If

'create the target table
CurrentDb.Execute "create table " & strTarget & "(" & sFld & ")"

   ' 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
   For i = 1 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
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
first of all,  do not use "Field" as a name of an object, this is an Access reserved word.

upload a  .MDB version of your db
0
 

Author Comment

by:atljarman
Comment Utility
Here is a sample file with the required code.   I keep getting the same error when stripping out the other database objects.  I renamed Field to Field1 with the same error.
db1.mdb
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:atljarman
Comment Utility
FYI - The form is hidden with the button that calls the conversion.
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
post an excel file, showing exactly how you want the table transposed, use the values from the table.
0
 

Author Comment

by:atljarman
Comment Utility
The code as is works, ha ha, execept it does not include the data.
file.xls
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
Comment Utility
test this


db1-rev.mdb
0
 

Author Closing Comment

by:atljarman
Comment Utility
Excellent.  Capricorn1's suggestion worked like a charm.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
A short article about problems I had with the new location API and permissions in Marshmallow
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now