Changing Field Name in VBA

M

Marshall Barton

PGZ said:
Is there anyway to the change field names of a table using
VBA?

Check Help for details, but it could be something like:

CurrentDb.TableDefs("tablename").Fields("oldanme").Name =
"newname"
 
D

dchendrickson

I don't have experience working in the older DAO methods,
but the newer ADO methods might use an approach to
changing field names in a table like this:

Public Sub TableFieldNameModify(strOld as String, strNew
as String, strTbl as String)

Dim cat As New ADOX.Catalog
Dim cnn As New ADODB.Connection
Dim tbl As New ADOX.Table

Set cnn = CurrentProject.Connection
Let cat.ActiveConnection = cnn
Set tbl = cat.Tables(strTbl)

Let tbl.Columns(strOld).Name = strNew

End Sub

WHERE:
strOld is the current field name to be changed
strNew is the desired field name
strTbl is the name of the table containing the field

Hope this is what you had in mind.

-dc
 
P

PGZ

When I tried the following, I received a "Item not found
in this collection." error message.

CurrentDb.TableDefs("Table_Struct_Chains_TEMP").Fields
("Top Tier ID").Name = "Tier 02 ID"

I also tried this:

Table_Struct_Chains_TEMP.Field("Top Tier ID").Name = "Tier
02 ID"

but I received "Object doesn't support this property or
method" error message.

Any suggestions?
 
M

Marshall Barton

PGZ said:
When I tried the following, I received a "Item not found
in this collection." error message.

CurrentDb.TableDefs("Table_Struct_Chains_TEMP").Fields
("Top Tier ID").Name = "Tier 02 ID"

Assuming you're using DAO, double check to make sure you're
using the exact spelling of the table and field names.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top