Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to rename the field's name in the table from VBA? 1

Status
Not open for further replies.

sabavno

Programmer
Jul 25, 2002
381
CA
Hi,

I was wondering if anyone could show me how to rename the field's name in the Access table from VBA?

Thanks.
 
dim MyDb as database
set mydb = currentdb
dim MyTdf as tabledef
set mytdf = mydb.tabledefs("TableName")
mytdf.fields("FieldName").Name = "NewName"
set mytdf = nothing
set mydb = nothing
 
Thanks a lot.

What if I don't know the name of the field, but know that it is a first field in the table. Is there such as way to loop through all the fields, find out how many of them and rename them accordingly.

THanks.
 
Sure!

Dim MyField as field
for each myfield in mytdf.fields
next myfield

-- or --

dim counter as integer
for counter = 0 to mytdf.fields.count - 1
set myfield = mytdf.fields(counter)
next counter
 
Thanks a lot

Sorry, but I have one more question

How can we add a new field to the existing table from VBA?

 
That's a bit more involved. At this point, you are best served by reading the Access Help on the Fields Collection.

The examples are quite good, and the help will give you the details you need to create the field exactly as you need it (there are numerous field properties that you must set correctly).

 
Adding a field is possible with SQL's ALTER TABLE statement. E.g.
Code:
ALTER TABLE table_name ADD column_name TEXT(20)
(Execute as a query.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top