PDA

Click to See Complete Forum and Search --> : creating a new database at runtime


Jan 19th, 2000, 11:51 AM
Is there a way i can design a new access database during runtime if there is not one already existing?

Clunietp
Jan 19th, 2000, 12:31 PM
DAO or ADO?

Jan 19th, 2000, 02:03 PM
im not sure, does that matter?

bullwhip
Jan 19th, 2000, 08:08 PM
I don't know if this is the best way but it works for me:

Public Function CreateDB()

'create variables for number of fields needed
Dim F1 As Field, F2 As Field, F3 As Field

'Create Database Object variable
Dim myWs as Workspace
Dim myDB as Database

'Create TableDef object
Dim myTbl as TableDef

'Create Database
Set myWs = DBEngine.Workspaces(0)
Set myDb=myWs.CreateDatabase("C:\EMP.MDB", dbLangGeneral)

'Create Table
Set myTbl = myDb.CreateTableDef("EMPLOYEE")

'Set Field Properties
Set F1=myTbl.CreateField("LNAME", dbText, 30)
' set flag if needed such as:
F1.AllowZeroLength = False
Set F2=myTbl.CreateField("FNAME", dbText,15)
F2.AllowZeroLength = False
Set F3=myTbl.CreateField("SSN", dbText, 11)
F3.AllowZeroLength = False

'Append Fields to Table definition
myTbl.Fields.Append F1
myTbl.Fields.Append F2
myTbl.Fields.Append F3

'Append Table to Database
myDb.TableDefs.Append myTbl

myDb.Close

End Function


You'll probable use variables where I hardcoded the database path and name.
You also must remember to delete the database before it is created again.

Good Luck

[This message has been edited by bullwhip (edited 01-20-2000).]

[This message has been edited by bullwhip (edited 01-20-2000).]

Clunietp
Jan 19th, 2000, 10:44 PM
There ya go! Thanks bullwhip.

dmartin -- It does matter which data access method you are using so we can provide the appropriate example. In this case, I hope you like DAO because you got lots of it!

Jan 20th, 2000, 11:30 AM
truthfully i dont think i know the difference between ado and dao, i might but i cant recall of the top of my head. either way this looks great thanks alot.