I am trying to rename the field names of a table in code. This table (I'll call ChangeTable)is the result of a make table query off of a crosstab, so the field names will always vary, but I do list these fieldnames in a second table (FieldTable). To rename my fields, I am looping through code that creates a temp field with the new name, dumps the data into the new field, and then deletes the old field. BUT, I can't seem to get the code to work as I am having problems referencing the original field name. Here's what I have:
Sub RenameField()
Dim sSQL As String
Dim iPosition As Integer
Dim OldField As String
X As Integer
FieldCount = FieldTable.RecordCount
For X = 1 To FieldCount
OldField = FieldTable![FieldName]
NewField = "Field" & Format(X) 'I want the new field name to be Field1, Field2, etc
iPosition = CurrentDB.TableDefs_("ChangeTable".Fields(OldField).OrdinalPosition 'SNAG HERE!
sSQL = "ALTER TABLE Table1 ADD COLUMN Temp Text"
CurrentDB.Execute sSQL
sSQL = "UPDATE DISTINCTROW ChangeTable SET Temp =_ OldField"
CurrentDB.Execute sSQL
sSQL = "ALTER TABLE ChangeTable DROP COLUMN_ OldField"
CurrentDB.Execute sSQL
CurrentDB.TableDefs("ChangeTable".Fields_("Temp".Name = NewField
CurrentDB.TableDefs("ChangeTable".Fields_(NewField).OrdinalPosition = iPosition
Next X
End Sub
As mentioned, it seems like my field references are not being recognized, though I am sure there are more bugs I haven't hit yet.
Help!
Thanks,
D
Sub RenameField()
Dim sSQL As String
Dim iPosition As Integer
Dim OldField As String
X As Integer
FieldCount = FieldTable.RecordCount
For X = 1 To FieldCount
OldField = FieldTable![FieldName]
NewField = "Field" & Format(X) 'I want the new field name to be Field1, Field2, etc
iPosition = CurrentDB.TableDefs_("ChangeTable".Fields(OldField).OrdinalPosition 'SNAG HERE!
sSQL = "ALTER TABLE Table1 ADD COLUMN Temp Text"
CurrentDB.Execute sSQL
sSQL = "UPDATE DISTINCTROW ChangeTable SET Temp =_ OldField"
CurrentDB.Execute sSQL
sSQL = "ALTER TABLE ChangeTable DROP COLUMN_ OldField"
CurrentDB.Execute sSQL
CurrentDB.TableDefs("ChangeTable".Fields_("Temp".Name = NewField
CurrentDB.TableDefs("ChangeTable".Fields_(NewField).OrdinalPosition = iPosition
Next X
End Sub
As mentioned, it seems like my field references are not being recognized, though I am sure there are more bugs I haven't hit yet.
Help!
Thanks,
D