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

Table Creation

Status
Not open for further replies.

praveensg

Programmer
May 15, 2002
26
0
0
GB
Hi,
I would like to create a table with 6 fields within an access database(.mdb) thru my application...how do i go about doing this...this is a little urgent..plz help me...
thanx
praveen
 
I can give you an example using DAO. Most folks seem to use ADO and since DAO's days are, to an extent, numbered, they're probably right to do so. That said:
Dim dbMyDB as Database
Dim tdfTable As TableDef
Dim fldField As Field

Set dbMyDB = OpenDatabase("YourDatabasePath")
Set tdfTable = dbMyDB.CreateTableDef("TableName")
Set fldField = tdfTable.CreateField("FieldName", dbText)
tdfTable.Fields.Append fldField

'Repeat from "Set fldField..." for each field
dbMyDB.TableDefs.Append tdfTable

Set fldField = Nothing
Set tdfTable = Nothing
Set dbMyDB = Nothing


When creating your field, check the DataTypeEnum constants in the Object Browser to find appropriate data types (I've set the example to a Text data type).
 
You will need to add a reference to the Microsoft ADO Ext. 2.x for DLL And Security . This code will create a DB and a table with a couple of columns using ADO. It should get you started anyway.

Private Sub cmdCreateDB_Click()
Dim cat As New ADOX.Catalog
Dim tbl As New Table
Dim sConnection As String

sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=I:\newDBName.mdb"
cat.Create sConnection
cat.ActiveConnection = sConnection
tbl.Name = "newTableName"
tbl.Columns.Append "newColumn1Name", adInteger
tbl.Columns.Append "newColumn2Name", adVarWChar, 25
cat.Tables.Append tbl

Set tbl = Nothing
Set cat = Nothing
End Sub

Hope this helps. [spin] If you choose to battle wits with the witless be prepared to lose.
[machinegun][ducky]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top