Despite the cynical comments you have so far received, I too have to do this regularly. All my legacy databases are currently DAO so if you have not got the library in your references, you must activate it before you run this
Function changefieldnames(oldname As String, newname As String)
'----- oldname and newname are passed to this function from
'----- wherever you are in your process. I would do it via a
'----- form, but if the field names you change are the same every
'----- time the process is run, why not store them in a table. The
'----- code you will need to pass the field names to be corrected
'----- from the table is given in Sub readinfieldnames() below.
Dim db As Database
Dim tdf As TableDef
Dim n As Object
Set db = CurrentDb
Set tdf = db.TableDefs("tablename")
For Each n In tdf.Fields
If n.Name = oldname Then n.Name = newname
Next n
Set tdf = Nothing
Set db = Nothing
End Function
Sub readinfieldnames()
Dim rst_data As Recordset
'------ Assumes you have a table with 2 columns. Column 1 contains
'------- downloaded field names, column 2 has the names you want
Dim oldfieldname As String, newfieldname As String
Set rst_data = CurrentDb.OpenRecordset("tbl_of_field_names")
With rst_data
.MoveFirst
Do Until .EOF
oldfieldname = .Fields(0).Value
newfieldname = .Fields(1).Value
changefieldnames oldfieldname, newfieldname
.MoveNext
Loop
End With
Set rst_data = Nothing
End Sub
I did this from memory so there may be one or two syntax errors. Sorry in advance. Hope this helps
Alan T
[email protected]