Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programatically add fields to tables?

Status
Not open for further replies.

asenft

Programmer
Apr 24, 2001
52
US
How can i programatically check to see if a certain table has a specific field, and if it doesnt, add the field to the table? The field isnt going to have any relationships or indexes, so i'm hoping it will be simple. Thanks for any help.

Aaron
 
Hello,

Are you trying to add a new field or append data to a field. If you want to create a new field you can use .FieldName to test for the fields existance. If you want to append data, you can create a recordset to see if the records already exists, and if not, append.

lbigk
 
Are you using Access 97 or 2000, and DAO or ADO? It can be done in either DAO or ADO, which do you prefer?
 
Well, i got it working pretty well. Now i just need to test if the field is there or not. I noticed that lbigk said i could use .fieldname to test if it is there or not, so i will look into that. Thanks for your help so far!

Aaron
 
somting like this should work using DAO
Dim dbs As Database, tdf As TableDef
Dim fld As Field, fldexist As Boolean
Set dbs = CurrentDb
Set tdf = dbs.TableDefs!table1
For Each fld In tdf.Fields
If fld.Name = "test1" Then fldexist = True
Next fld

If fldexist = False Then
Set fld = tdf.CreateField("test1", dbText, 11)
tdf.Fields.Append fld
End If
Set dbs = Nothing
 
Thanks braindead2. That helped a lot.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top