Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the DDL to rename column in MSAccess?

Tags:

ddl

ms-access

What is the DDL to rename a column in MS Access? Something along the lines of:

alter table myTable rename col1 to col2

which does not work for MSAccess 2000 format databases. I'm using OLEDB or ADO.NET with a MSAccess 2000 format db but would be grateful of any hint at the syntax or a suggestion as to how to achieve this using ADO.NET in some other way.

like image 872
mikemay Avatar asked Jan 12 '10 13:01

mikemay


People also ask

How do you rename a column in access?

To rename a field in a table in Access, open the table containing the field to rename in design view. Then click into the “Field Name” column of the field that to rename and type a new name. Then click the “Save” button in the Quick Access toolbar to save your structural modifications.

What is rename in DDL?

Rename: RENAME command is used to change the name of the table or a database object. RENAME old_table_name To new_table_name; Example: Rename testable to test_table; Below is the screenshot for renaming the table from testtable to test_table.

How do I rename a column in a database?

You select the table with ALTER TABLE table_name and then write which column to rename and what to rename it to with RENAME COLUMN old_name TO new_name .

How do I rename columns in a table?

Rename a column using table designer. In Object Explorer, right-click the table to which you want to rename columns and choose Design. Under Column Name, select the name you want to change and type a new one. On the File menu, select Save table name.


3 Answers

I do not believe you can do this, other than by appending a new column, updating from the existing column and then deleting the 'old' column.

It is, however, quite simple in VBA:

Set db = CurrentDb
Set fld = db.TableDefs("Table1").Fields("Field1")
fld.Name = "NewName"
like image 157
Fionnuala Avatar answered Sep 24 '22 06:09

Fionnuala


I am at home and can't test this at the moment, but I think this should work. This site has information about it.

ALTER TABLE thetable ALTER COLUMN fieldname fieldtype

Edit I tested this a bit and oddly enough, you can't rename a column that I can find. The ALTER COLUMN syntax only allows for changing type. Using SQL, it seems to be necessary to drop the column and then add it back in. I suppose the data could be saved in a temporary table.

alter table test drop column i;
alter table test add column j integer;
like image 42
Mark Wilkins Avatar answered Sep 22 '22 06:09

Mark Wilkins


My solution, simple but effective:

Dim tbl as tabledef
set tbl = currentdb.TableDefs("myTable")
tbl.fields("OldName").name = "Newname" 
like image 34
Kees Dapperens Avatar answered Sep 22 '22 06:09

Kees Dapperens