Creating table in code

J

John

Hi

How can I via code create a table, add a field of type txt(255) and add a
record into it?

Thanks

Regards
 
D

Dirk Goldgar

John said:
Hi

How can I via code create a table, add a field of type txt(255) and add a
record into it?


There are several ways to do it. You can do it by executing SQL statements,
or by manipulating DAO objects or (I think) ADOX objects. Here's a very
simple example using SQL:

With CurrentDb

.Execute _
"CREATE TABLE MyNewTable (Field1 TEXT(255))", _
dbFailOnError

.Execute _
"INSERT INTO MyNewTable (Field1) " & _
"VALUES('text for field1')", _
dbFailOnError

End With

There are some other options you can apply in the CREATE TABLE statement --
e.g., NOT NULL, or WITH COMPRESSION, or CONSTRAINT -- so the above is just a
simple example.

If you use DAO objects instead, you have finer control over some of the
table and field properties, but it's more complicated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top