Hi
I have a bit of a strange one.. Have to query a column in a table that is imported from an excel spreadsheet in to access. problem is a new column is added each week so the column name changes.. (I know really bad, but its what I have to work with from a 3rd party)
The one thing that never changes is a column called "total" that always follows after the newly added column each week. So far I have found the vba below which lets me look for a set column name and rename it.
What I was wondering is could I look for column name "total" and then effectively do -1 so it then looks at the column before and renames that column to a set name so I can query it.
Sub RenameField(strTableName As String, _
strFieldFrom As String, _
strFieldTo As String)
Dim dbs As DAO.Database
Dim tDef As DAO.TableDef
Dim fDef As DAO.Field
Set dbs = CurrentDb()
Set tDef = dbs.TableDefs(strTableName)
Set fDef = tDef.Fields(strFieldFrom)
fDef.Name = strFieldTo
Set fDef = Nothing
Set tDef = Nothing
Set dbs = Nothing
End Sub
By the way the previous table it deleted before the new data is imported so the previous rename would not be in the table still. So wouldn't cause an issue when running queries.
Thanks guys!
I have a bit of a strange one.. Have to query a column in a table that is imported from an excel spreadsheet in to access. problem is a new column is added each week so the column name changes.. (I know really bad, but its what I have to work with from a 3rd party)
The one thing that never changes is a column called "total" that always follows after the newly added column each week. So far I have found the vba below which lets me look for a set column name and rename it.
What I was wondering is could I look for column name "total" and then effectively do -1 so it then looks at the column before and renames that column to a set name so I can query it.
Sub RenameField(strTableName As String, _
strFieldFrom As String, _
strFieldTo As String)
Dim dbs As DAO.Database
Dim tDef As DAO.TableDef
Dim fDef As DAO.Field
Set dbs = CurrentDb()
Set tDef = dbs.TableDefs(strTableName)
Set fDef = tDef.Fields(strFieldFrom)
fDef.Name = strFieldTo
Set fDef = Nothing
Set tDef = Nothing
Set dbs = Nothing
End Sub
By the way the previous table it deleted before the new data is imported so the previous rename would not be in the table still. So wouldn't cause an issue when running queries.
Thanks guys!
