Changing Field Name in VBA

  • Thread starter Thread starter PGZ
  • Start date Start date
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"
 
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
 
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?
 
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.
 
Back
Top