FrosyFeet456
FrosyFeet456

Reputation: 349

Rename a field in access vba

I need to rename a field name in VBA access code to automate things so it works faster. I am using a button to move some data around and need to change the column names, as a result, to clean things up. I am working out of a form but the table I need to change isn't linked to the form at all...

I have tried using some VBA code but keep getting variables undefined errors like the tblFieldBookwslk50 is not defined

Set db = CurrentDb
Set rst = db.OpenRecordset("tblFieldBookwslk50", dbOpenDynaset)
'rename field names
rst.TableDefs(tblFieldBookwslk50).Fields("GDU_slk_50_Daily HU").Name = 
"GDU_slk50"
rst.TableDefs(tblFieldBookwslk50).Fields("tblFieldBookwshd50_Daily HU").Name = "GDU_shd50"
db.Close
rst.Close

I want the field names to say 'GDU_shd50' rather than it say 'tblFieldBookwshd50_Daily HU'.

Upvotes: 2

Views: 8217

Answers (1)

Chris Boden
Chris Boden

Reputation: 76

You have to use the TableDefs in the Database object.

Set db = CurrentDb
'rename field names
db.TableDefs("tblFieldBookwslk50").Fields("GDU_slk_50_Daily HU").Name = "GDU_slk50"
db.TableDefs("tblFieldBookwslk50").Fields("tblFieldBookwshd50_Daily HU").Name = "GDU_shd50"
db.Close

Upvotes: 6

Related Questions