I picked up the code to change table names from TonyJollans: and have been trying to work it into a module as a sub for changing field names in any table passed to it... with problems.
I keep getting error '3265 - Item not found in this collection'... Seems to be a problem with how I refer to a field with a variable...
Help please! Code follows:
The sub is called with:
where OldFieldName, NewFieldName are strings.
I keep getting error '3265 - Item not found in this collection'... Seems to be a problem with how I refer to a field with a variable...
Help please! Code follows:
Code:
Public Sub AlterFieldName(TableName, OldName, NewName)
'Thanks to TonyJollans on [URL unfurl="true"]www.tek-tips.com[/URL] forum for the original code (dated 20030629)
Dim tjTab As DAO.TableDef
Dim tjFld As DAO.Field
Dim tjProp As DAO.Property
Set tjDb = CurrentDb
Set tjTab = tjDb.TableDefs!(TableName)
Set tjFld = tjTab.Fields!OldName ' THIS IS THE PROBLEM LINE
'if I replace OldName with an actual field name it works...
Set tjProp = tjFld.Properties!Name
tjProp = NewName
tjDb.Close
Set tjProp = Nothing
Set tjFld = Nothing
Set tjTab = Nothing
Set tjDb = Nothing
End Sub
The sub is called with:
Code:
Call AlterFieldName("TConvertedCountsheet", OldFieldName, NewFieldName)