473,320 Members | 1,922 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,320 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 3156
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.