Solved

How to change field-order in Access table ?

Posted on 1998-08-07
6
495 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 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