473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Changing Column Name in an existing table

489 256MB
I have a table that I would like to change the column names on a few columns depending on a name selected by the user.
I can delete the existing columns and then add the new ones using the Alter table command but was wondering if there is a way to just change the column name?
Also after the process is completed I want to change the column name back to the original, I know the location of the columns in the table so is there a way to use the column number to change the column name.
Thanks for any help.

CD
Jan 25 '15 #1

✓ answered by Stewart Ross

Hi Tom. Whilst it is certainly possible to use the ALTER TABLE statement to change the structure of a table, or alternatively to do so using the Append method of the Fields collection in DAO Recordset processing, I would advise against doing so.

There is no need to make physical changes to the table structure. Instead, just use a SELECT query and apply an alias to each of the field names you want to change using the AS statement, which you can do in VBA code relatively easily. The (simplified) example below shows how:

Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT Field1, [Some Field] AS [" & strNewValue & "], Field3 FROM YourTable"
The SQL can be set dynamically as the recordsource of a form or report, or appended to a QueryDef, for use in whatever application you envisage.

I am not at all clear why you need to rename columns in response to user input, but as that is what you ask I do think that aliasing the physical names is a more flexible and less error-prone solution than changing the physical structure of a table. The other advantage is that there is no need to physically change the field names back after use.

-Stewart

1 3163
Stewart Ross
2,545 Expert Mod 2GB
Hi Tom. Whilst it is certainly possible to use the ALTER TABLE statement to change the structure of a table, or alternatively to do so using the Append method of the Fields collection in DAO Recordset processing, I would advise against doing so.

There is no need to make physical changes to the table structure. Instead, just use a SELECT query and apply an alias to each of the field names you want to change using the AS statement, which you can do in VBA code relatively easily. The (simplified) example below shows how:

Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT Field1, [Some Field] AS [" & strNewValue & "], Field3 FROM YourTable"
The SQL can be set dynamically as the recordsource of a form or report, or appended to a QueryDef, for use in whatever application you envisage.

I am not at all clear why you need to rename columns in response to user input, but as that is what you ask I do think that aliasing the physical names is a more flexible and less error-prone solution than changing the physical structure of a table. The other advantage is that there is no need to physically change the field names back after use.

-Stewart
Jan 25 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: mokles | last post by:
Hi All, I am trying to change column name on an existing table. I am using SQL Server 7. As the table is quite big, it is taking quite long time to do it. By the way I could change the column...
7
by: Yannick Turgeon | last post by:
Hello all, I'm using SS2K on W2k. I'v got a table say, humm, "Orders" with two fields in the PK: OrderDate and CustomerID. I would like to add an "ID" column which would be auto-increment...
3
by: Raj | last post by:
Hi, I am trying to add some more information to the table which already has a lot a data (like 2-3000 records). The new information may be adding 2-3 new columns worth. Now my questions are:...
13
by: annecarterfredi | last post by:
I'd like to add a new column to an existing table at a specific column location... Existing table definition: MyTable(Col1, Col2, Col3) I want to add a new column in MyTable and the new...
2
by: post | last post by:
Hello, i have a table and if a record is inserted i will test a numeric value in this table. If the this value is greather as 1 million, than an status column should be changed from 'A' to 'B'....
7
by: hjohnson | last post by:
Within the access environment, I have a table that I'd like to -add a column -place the record number of each record into that column The autonumber is not working for me because I am...
9
by: mamoon | last post by:
hi all, Question: i have a table in a database of 6 columns and 630 rows.i want to add one new column to this table and also update this column starting from row 1 to row 630. System...
2
by: Rahul B | last post by:
I am new at DB2 . so please guide I am trying to change the Datatype of a column in the table. CREATE TABLE testschema.player ( NAME VARCHAR(120) NOT NULL, country ...
2
by: Rahul B | last post by:
Hi. I have created a table CREATE TABLE testschema.player ( country VARCHAR(80) NOT NULL, posn VARCHAR(2) not null ) /
1
by: rahul more | last post by:
I want to show column names of respective table into dropdownlist box. I am not getting how to write SQL query which gives column name in tabular format. For example suppose there is one table book...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.