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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add a field using VBA 2

Status
Not open for further replies.

simonjackson79

Technical User
Dec 23, 2003
31
0
0
GB
Hi,

I have a macro setup up that imports a text file "All CLA ever.txt" and creates a table from it. Is there anyway that I can add a text field called "Key Worker" to the table using VBA?

Many thanks in advance,

Simon
 
Take a look at the ALTER TABLE DDL instruction, like this:
DoCmd.RunSQL "ALTER TABLE myTable ADD COLUMN [Key Worker] TEXT(25)"

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Try this code example:

Code:
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As Field
Set db = CurrentDb
Set tbl = db![red]yourtablename[/red]
Set fld = tbl.CreateField("Key Worker", DB_TEXT)
fld.Size = 2
tbl.Fields.Append fld
db.close

There are other properties of the new text field that can be set like the code line above for Size. (i.e. Default, Ordinal Position, etc.) You can see these properties in ACCESS Help and add them as necessary.

Post back if you have any questions.

Bob Scriver
[blue]Want the best answers? See FAQ181-2886[/blue]


 
Hi Simon,

You cannot programmatically add a field to a table once the table has been created and added to the Tabledefs collection.

If you say your existing macro creates the table, I would suggest you insert some code within your macro using the RunCode command which first creates a tabledef object, then create field objects for the field(s) you wish to have in the table (CreateField method), then adds each of those field objects to the tabledef object, and finally adds the tabledef object to the Tabledefs collection.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top