When uploading one of my .txt files into Access, I have to change three of my datetime columns to text so that I don't get import errors. I have some VBA code that can go through and fix these columns to the correct data type, but it refers to the columns specifically by name and I have to repeat a section of code 3x and this is bugging me.
If I set up a pair of arrays, one for column names (col1, col2, col3), and another with the fld.OrdinalPosition (9, 12, 15), I think I could loop through the same code 3x and get this done. Is there some way that, if given the column name, I can have some lookup function return the fld.OrdinalPosition so that I can skip the second array? Something to make this more dynamic.
Here is the code that I have stolen and adapted to my needs:
Code:
'---Create New Field
dbsData.TableDefs.Refresh
Set tdf = dbsData.TableDefs("EMAILS_RESOLVED_DETAIL_TEMP")
Set fld = tdf.CreateField("LOCK_DATE_NEW", dbDate)
'Optional: set default value
fld.DefaultValue = "0"
'We set ordinal position, just after old field
fld.OrdinalPosition = 12
'And append
tdf.Fields.Append fld
'Copy values from old field to a new one
dbsData.Execute _
"Update EMAILS_RESOLVED_DETAIL_TEMP Set LOCK_DATE_NEW = LOCK_DATE", dbFailOnError
'Delete old field
tdf.Fields.Delete "LOCK_DATE"
tdf.Fields.Refresh
'Rename new field to old
tdf.Fields("LOCK_DATE_NEW").Name = "LOCK_DATE"
tdf.Fields.Refresh
'Done!
Set tdf = Nothing