PDA

Click to See Complete Forum and Search --> : creating tables in a access database using code


Oct 10th, 2000, 05:00 AM
Hi all

I know how can a user can create a database.My problem is how can i define(create) fields in that specific database (the tables has to be predefined) and how can I specify the location to my data controls as the database can be any where on the harddrive.
Please help

elpower
Oct 11th, 2000, 09:18 PM
Sure thing.
try this, just make sure you reference your project to MS DAO 3.51 Object library for access 97 file or MS DAO 3.6 Object library for Access 2000 file. and change the fields to what you want.

Dim TblFile As TableDef
Dim IdxFile As Index
Dim FLD As Field
Dim DbsNew As Database
Public DBCallData As Database
Public CallSeconds As String

Public Sub DataFileFind()
DataFile = App.Path & "\database.mdb"
If Dir(DataFile) <> "" Then ' if file exist. do nothing. else. create database
Else
'create database file
Set DbsNew = DBEngine.CreateDatabase(DataFile, dbLangGeneral) 'create data file
Set TblFile = DbsNew.CreateTableDef("SetupCall") ' create recordsets
With TblFile
.Fields.Append .CreateField("SendingMuxAddress", dbText, 50) 'add field names
.Fields.Append .CreateField("OriginMuxAddress", dbText, 50)
.Fields.Append .CreateField("Originator", dbText, 50)
.Fields.Append .CreateField("CallSerialNum", dbText, 50)
.Fields.Append .CreateField("VoicePort", dbText, 10)
.Fields.Append .CreateField("TransDestNum", dbText, 50)
.Fields.Append .CreateField("SourcePort", dbText, 50)
.Fields.Append .CreateField("DestNum", dbText, 50)
DbsNew.TableDefs.Append TblFile
End With

'IF INDEX NEEDED. USE CODING
'Set IdxFile = TblFile.CreateIndex("IdxFile") 'add index
'Set FLD = IdxFile.CreateField("Name") 'connect index to field
'With IdxFile
' .Primary = True
' .Unique = True
' .Required = True
'End With
'IdxFile.Fields.Append FLD
'TblFile.Indexes.Append IdxFile
DbsNew.Close
End If
End Sub