Transpose Please please help

K

Kay Ree

Please help me. I need to transpose my data in rows to
columns. Below is an example.

Field1 field2 field3
Name john
Address xxx1
ZipCode 12345
City atlanta
State ga
Name jane
Address xxx2
ZipCode 12345
City nashville
State tn
Name alex
Address xxx3
ZipCode 12345
City sacramento
State ca

I would like the columns to be Name Address ZipCode City
State and the data that follows to go below the columns.
Any ideas?
 
C

chriske911

what's wrong with what I answered ?

so import this page into excel, you can do this working from excel or copy
the page into excel if it's a table formatted layout
copy the entire column and choose special paste\transpose
or under the data menu there is an option text to columns
finally import the transposed data into access

bon chance
 
D

dbc

Kay,
I recommend using Excel to do the transposition. If
you've got data like this entered in Access, there is no
way to fix it. Try to export this stuff into Excel. In
Excel, select a block of text representing a single
record, and click Copy. Then choose Edit..Paste Special,
choosing the Transpose checkbox. Then you'll have to
delete the all the rows of field headers except the first
one before you import it all back into Access.

dbc
 
K

Kay Ree

The problem is I have a large number of records about
600,000 records and doing that in Excel would take me my
entire life to do. I checked out the Microsoft articles
you put on there before I submitted my message in this
newsgroup. Anybody else have any ideas? Thanks Chriske911.
But it will not work.
 
C

chriske911

are you sure there always the same amount of fields for every record ?
I did this once myself by converting the table into a text file and with the
edit/replace function:
1. remove all blank lines
2. remove all line feeds
3. remove all extra spaces
4. insert tabs between all "fields" by replacing the "fieldname" with
^t"fieldname" (without the quotes offcourse)
5. insert a line feed between all first fields of a record by adding ^p^p
before that firs field (yes 2 linefeeds)
6. remove all fieldnames since you know what field is in what column
7. import that text file by using delimited by tabs option

this will take a lot of time, use an unused workstation for this, disable
screensaver and so on during this operation
by now you will have a lot less then 600K records if you're counting the
records as they are currently formatted

note: there are a lot of editors faster and more lenient than MS Notepad

bon chance
 
K

Ken Snell

This could be done using VBA code. Can you give more info about the data
file's format and type? Are you wanting to create a table of these data?
 
K

Kay Ree

The file is in MS Access table format. Yes, it would be
nice to create a table from the data. Either that or a
query would work too. Thanks Ken!
 
K

Kay Ree

There aren't any related records. The data was obtained
from a website and is the way it is. It is in exactly the
same format as my table below.
 
K

Ken Snell

Here's the concern on my part. There is *no* inherent order in a table.
None. Zip. If you put records into a table and there is no primary key that
is based on which record was entered first, which was entered second, etc.,
then there is no way to guarantee that we can *see" the table's records *in
the order in which they were entered*.

Any code that I post for you in this case would have to be based on the
*assumption* that the records will be pulled up in the correct order. If
they aren't, the code will fail. There is no way to guarantee that the
records will come up in the order you show them and in the appropriate
repetitive pattern for a single "name": Name, Address, ZipCode, City, State.

Is there any additional info that you can provide that will assist in
helping the code correctly sort the data?
 
K

Kay Ree

Ken,

I think that is a good assumption to make. I am pretty
sure the pattern is very repetitive. What other
information do you need regarding the data? I will be more
than happy to give you the information that you need if it
would help make life easy. Thanks in advance for the help!
 
K

Ken Snell

I'm not convinced that it will work, but I'll put together some sample code
for you later tonite and post it.
 
K

Ken Snell

OK - here is some sample code for trying to transpose your data.

I will assume that the table holding the original data is named tblOld.

Create the new table that will hold the "transposed" data. I will assume
that the name of the table is tblNew and that the names of the fields will
be these:
fldName
fldAddress
fldCity
fldState
fldZipCode

(Note: Do not use Name as the name of a field. Name is a word with special
meaning to ACCESS, and using it as a field name may/will confuse ACCESS at
some point.)

Create a regular module (name it basTranspose) and put the following code in
it (warning: this code has not been fully tested; also, if the records in
the original table are not in the order anticipated by this code [a group of
name, address, zipcode, city, and state records; then this group repeats],
the code will fail):


Public Sub TransposeTableData()
Dim dbs As DAO.Database
Dim rstOrig As DAO.Recordset, rstNew As DAO.Recordset
Dim intCount As Integer

Set dbs = CurrentDb()
Set rstNew = dbs.OpenRecordset("tblNew", dbOpenDynaset, dbAppendOnly)
Set rstOrig = dbs.OpenRecordset("tblOld", dbOpenDynaset, dbReadOnly)

rstOrig.MoveFirst

Do While rstOrig.EOF = False
rstNew.AddNew
For intCount = 1 To 5
rstNew.Fields("fld" & rst.Fields(0)) = rst.Fields(1)
rstOld.MoveNext
Next intCount
rstNew.Update
rstOld.MoveNext
Loop

rstNew.Close
rstOld.Close
Set rstNew = Nothing
Set rstOld = Nothing
dbs.Close
Set dbs = Nothing

End Sub


Then run this code by clicking anywhere within the code while the Visual
Basic Editor is open, and clicking on the Run icon (right-pointing triangle
on toolbar). If an error occurs, VBE will display a "debug" window telling
you of the error.
 
K

Kay Ree

Ken,

I get a user defined area. I don't think it knows what the
subroutine transposetabledata () is. And being that I dont
know any VB, I am not sure where to define it. Any ideas?
Thanks in advance!
 
K

Ken Snell

When the error occurs, on which line do you see yellow highlighting? Give me
the exact error number and description.

Be sure that you replaced my table and field names with the actual names
that you used.
 

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

Similar Threads


Top