I use the code below to add a Text field to a table. I do NOT use a sub or function to store the code
It works the first time I use the code, but does not work the second time I include the code to add a second text field to the same table. The instances of the code are included in the the On Open event of a form and are run as part of an update process, depending on which version the user has. I do not get any messages. The second Text field is not added to the table. In the second instance of the code I use different names for the Db2, tdf2, fld2.
The second Text field is added if I use an Application.Quit in between the two field addition codes.
Any ideas?
John
Code:
[COLOR=red][FONT=Courier New]Dim [/FONT][/COLOR][COLOR=navy][FONT=Courier New]Db [/FONT][/COLOR][COLOR=red][FONT=Courier New]As [/FONT][/COLOR][COLOR=navy][FONT=Courier New]DAO.Database
[/FONT][/COLOR][COLOR=red][FONT=Courier New]Dim [/FONT][/COLOR][COLOR=navy][FONT=Courier New]fld [/FONT][/COLOR][COLOR=red][FONT=Courier New]As [/FONT][/COLOR][COLOR=navy][FONT=Courier New]DAO.Field
[/FONT][/COLOR][COLOR=red][FONT=Courier New]Dim [/FONT][/COLOR][COLOR=navy][FONT=Courier New]tdf [/FONT][/COLOR][COLOR=red][FONT=Courier New]As [/FONT][/COLOR][COLOR=navy][FONT=Courier New]DAO.TableDef
[/FONT][/COLOR][COLOR=red][FONT=Courier New]Set [/FONT][/COLOR][COLOR=navy][FONT=Courier New]Db = Application.CurrentDb
[/FONT][/COLOR][COLOR=red][FONT=Courier New]Set [/FONT][/COLOR][COLOR=navy][FONT=Courier New]tdf = Db.TableDefs(strTableName)
[/FONT][/COLOR][COLOR=green][FONT=Courier New]' First create a field with data type = Text
[/FONT][/COLOR][COLOR=red][FONT=Courier New]Set [/FONT][/COLOR][COLOR=navy][FONT=Courier New]fld = tdf.CreateField(strFieldName, dbText) [/FONT][/COLOR][COLOR=green][FONT=Courier New]
' Append the field
[/FONT][/COLOR][COLOR=red][FONT=Courier New]With [/FONT][/COLOR][COLOR=navy][FONT=Courier New]tdf.Fields
.Append fld
.Refresh
[/FONT][/COLOR][COLOR=red][FONT=Courier New]End With [/FONT][/COLOR][COLOR=red][FONT=Courier New]
[/FONT][/COLOR][COLOR=blue][FONT=Courier New]
ExitHere:
[/FONT][/COLOR][COLOR=red][FONT=Courier New]Set [/FONT][/COLOR][COLOR=navy][FONT=Courier New]fld = [/FONT][/COLOR][COLOR=red][FONT=Courier New]Nothing
Set [/FONT][/COLOR][COLOR=navy][FONT=Courier New]tdf = [/FONT][/COLOR][COLOR=red][FONT=Courier New]Nothing
Set [/FONT][/COLOR][COLOR=navy][FONT=Courier New]Db = [/FONT][/COLOR][COLOR=red][FONT=Courier New]Nothing[/FONT][/COLOR]
It works the first time I use the code, but does not work the second time I include the code to add a second text field to the same table. The instances of the code are included in the the On Open event of a form and are run as part of an update process, depending on which version the user has. I do not get any messages. The second Text field is not added to the table. In the second instance of the code I use different names for the Db2, tdf2, fld2.
The second Text field is added if I use an Application.Quit in between the two field addition codes.
Any ideas?
John