Solved

How to change field-order in Access table ?

Posted on 1998-08-07
6
505 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:jannea
6 Comments
 
LVL 9

Expert Comment

by:cymbolic
ID: 1468272
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.
0
 
LVL 9

Expert Comment

by:cymbolic
ID: 1468273
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.
0
 
LVL 2

Expert Comment

by:percosolator
ID: 1468274
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
dapperry earned 50 total points
ID: 1468275
Sure you can do it with VB, and you don't need to make a new table as 'cymbolic' stated.  Just
make sure you have the DAO Library referenced in your application, and add the following:

Dim db As Database        
Dim tdf1 As TableDef
Dim tdf2 As TableDef
Dim fld1 As Field
Dim fld2 As Field

Set db = Opendatabase("c:\mytest.mdb")
Set tdf1 = db.TableDefs("Table1")
Set tdf2 = db.TableDefs("Table2")

For Each fld1 In tdf1.Fields
    Set fld2 = tdf2.Fields(fld1.Name)
    fld2.OrdinalPosition = fld1.OrdinalPosition
Next fld1
tdf2.Fields.Refresh
MsgBox "All done"

Let me know if you have any questions.

:) D Perry
0
 
LVL 2

Expert Comment

by:percosolator
ID: 1468276
dapperry:

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

Author Comment

by:jannea
ID: 1468277
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

733 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