Link to home
Start Free TrialLog in
Avatar of jannea
jannea

asked on

How to change field-order in Access table ?

Hello, How to change field-order in Access table (Row-order in design).
I like to make the fieldorder in Table_2 same as in Table_1:

I try to explain what i like to do:

Table_1 looks like this:

Table_1.Field(0).Name = Field0
Table_1.Field(1).Name = Field1
Table_1.Field(2).Name = Field2

Table_2 looks like this:

Table_2.Field(0).Name = Field2
Table_2.Field(1).Name = Field1
Table_2.Field(2).Name = Field0

*****************************************************************
* I like to have 'Your code' here to make the changes. *
*****************************************************************

Table_2 should looks like this after 'Your code':

Table_2.Field(0).Name = Field0
Table_2.Field(1).Name = Field1
Table_2.Field(2).Name = Field2

/Janne, SWEDEN
Avatar of cymbolic
cymbolic

You can only do this by creating a temporary table with the right field order, performing a "Select Into" SQL sequence, dropping the old table, and renaming the temporary table.  This becomes much more difficult and slower if you have a number of relationships defined between your target table and other tables in the form of foreign keys.  I'm dropping this in as a comment.  The actual code can be done in SQL DDL statements particular to your target Database and Access method.  You can dig these out of your relevant help systems.

Of course, who really needs to order the fields anyway?  You shouldn't be using integer offsets to pick up your columns in code, since that makes your app insensitive (not strongly typed) to your table structure.  If you get a table in with one column missing, your app is too dumb to know it, and your programmer has to figure it out.  SInce you can order your resultsets out an any order, and you should be identifying your columns by name, not position, why do you feel you need to do this?  Even if you want to make generic routines you can use the rs(colname) syntax.
You can only do this by creating a temporary table with the right field order, performing a "Select Into" SQL sequence, dropping the old table, and renaming the temporary table.  This becomes much more difficult and slower if you have a number of relationships defined between your target table and other tables in the form of foreign keys.  I'm dropping this in as a comment.  The actual code can be done in SQL DDL statements particular to your target Database and Access method.  You can dig these out of your relevant help systems.

Of course, who really needs to order the fields anyway?  You shouldn't be using integer offsets to pick up your columns in code, since that makes your app insensitive (not strongly typed) to your table structure.  If you get a table in with one column missing, your app is too dumb to know it, and your programmer has to figure it out.  SiInce you can order your resultsets out in any column order, and you should be identifying your columns by name, not position, why do you feel you need to do this?  Even if you want to make generic routines you can use the rs(colname) syntax, or you can have an external list off field offsets to use with each table, which could externally control the order you process the fields.
If all you want to do is to visually re-order the tables, in table view click once on the column header and hold, drag it to where you would like to place it and release the mouse button.

If you want to physically move the columns in a table, then you _are_ going to have to create an intermediary table as cymbolic detailed above.

I also strongly agree with his last statement.  The actual physical layout of a table is unimportant, if, you use field names to specify columns (I.E.  rs!LastName instead of rs.field(2), etc.)

I've had to go into code that was written that way, it's an absolute nightmare to debug; and if I ever get my hands on the guy who wrote it.....

It's an absolute nightmare to debug.

Use field names.
ASKER CERTIFIED SOLUTION
Avatar of dapperry
dapperry

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
dapperry:

now that was cool! learn something new every day.  i'm almost of a mind to give you points for showing me that.
Avatar of jannea

ASKER

Thanks it did work fine !

I will use the code in a function who compare and make too databases identical (Tables, Qurrys, Relations...), instead of doing this manually.
Nice to have this when working with cliens beta versions, to update their DB whithoute deleting data.

Thanks again to all comments and the excellent answer.

/Janne