Connecting Tech Pros Worldwide Help | Site Map

adding a field to a table

Earl
Guest
 
Posts: n/a
#1: Nov 12 '05
How do I add a new field to a table. the code below gives a type mismatch
error

---------------
Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset("mytable", dbOpenDynaset)

rs.Fields.Append "test", adChar





Allen Browne
Guest
 
Posts: n/a
#2: Nov 12 '05

re: adding a field to a table


To append a field to a table, add it to the TableDef, not to a Recordset.

Dim db As DAO.Database
Dim tdf As TableDef
Dim fld As DAO.Field

Set db = CurrentDb()
Set tdf = db.TableDefs("MyTable")
Set fld = tdf.CreateField("MyField, dbText)
tdf.Fields.Append fld

For the field type, make sure you use the DAO constants, not the ADOX
constants.
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Earl" <womenfo@mindspring.com> wrote in message
news:htROb.18312$OM2.4130245@news4.srv.hcvlny.cv.n et...[color=blue]
> How do I add a new field to a table. the code below gives a type mismatch
> error
>
> ---------------
> Dim db As DAO.Database
> Dim rs As DAO.Recordset
>
> Set db = CurrentDb
> Set rs = db.OpenRecordset("mytable", dbOpenDynaset)
>
> rs.Fields.Append "test", adChar[/color]


Closed Thread