I am currently working on a macro to do formatting on a table imported from excel by the user. (I was originally running a macro in excel)
The table needs to have a certain order to the columns, however I read that you can't change the position of an existing column. I found a macro which changes the field type by deleting and creating a new column and in the process, puts the column in a specific spot.
The only problem now is that the new column does not allow zerolength cells. I added and have tried multiple lines to allow the zero length but I continue to get the same error: Invalid Operation
Code:
Public Function ChangeFieldType(Fieldname As String, fieldpos As Integer, fieldtype As String)
Dim dbsData As Database
Dim tdf As TableDef
Dim fld As Field2
Set dbsData = CurrentDb
'---Create New Field
dbsData.TableDefs.Refresh
Set tdf = dbsData.TableDefs("TestTable")
Set fld = tdf.CreateField("MyFieldNew", fieldtype)
'Optional: set default value
fld.AllowZeroLength = True
fld.DefaultValue = "0"
'We set ordinal position, just after old field
fld.OrdinalPosition = fieldpos
'And append
tdf.Fields.Append fld
'Copy values from old field to a new one
dbsData.Execute _
"Update TestTable Set MyFieldNew=" & Fieldname, dbFailOnError
'Delete old field
tdf.Fields.Delete Fieldname
tdf.Fields.Refresh
'Rename new field to old
tdf.Fields("MyFieldNew").Name = Fieldname
tdf.Fields.Refresh
'Done!
Set tdf = Nothing
Set fld = Nothing
End Function
I have tried the following lines of code:
fld.AllowZeroLength = True
fld.AllowZeroLength.Value = True
fld.Properties("AllowZeroLength") = True
fld.Properties(AllowZeroLength) = True
fld.Properties("AllowZeroLength").Value = True
fld.Properties(AllowZeroLength).Value = True
Does anyone know the problem?
Or even does anyone know how to change the position of the column without this code?
Thanks for any help!
The original macro comes from: http://accessblog.net/2007/03/how-to...using-dao.html