Creating a new database.

sts023

Registered User.
Local time
Today, 21:00
Joined
Dec 1, 2010
Messages
40
Hi guys!
Once more, sorry for the Newbie type query.
I'm trying to write some Access 2003 code in an Access Module to create a new (i.e it doesn't already exist) database.
The code is shown below.
As usual, I've fallen at the first hurdle :(

On execution of the "Set db = OpenDatabase....." statement, Access complains that it can't find an existing "NewCopy.mdb".

Could some kind person tell me what syntax I should be using, and have a quick look at the subsequent code for the inevitable second hurdle?

Thanks again in anticipation
(Oh, and how do you put "pictures" of code in a question? I usually indent code for clarity, but when I preview the post, the code is all left justified!:confused:)

Option Compare Database
Option Explicit
Private Sub NewDb()
'*
'** variables
'*
Dim rs1 As Recordset
Dim db As Database
Dim td As TableDef
Dim fld As Field
Dim iFields As Integer
'*
'** Open new database.
'*
Set db = OpenDatabase("C:\Users\testi\Acaccess\NewCopy.mdb")
'*
'** Create a pointless sample Table.
'*
Set td = db.TableDefs("SillyTable")
'*
'** Now that the database Table is created, add fields to the Table.
'*
For iFields = 1 To 3 'Just a few....
Set fld = td.CreateField("Field " & CStr(iFields), dbInteger)
td.Fields.Append fld
Next iFields
For iFields = 4 To 6 'Just a few....
Set fld = td.CreateField("String " & CStr(iFields), dbText, 25)
td.Fields.Append fld
Next iFields
End Sub
 
Code:
Set db = OpenDatabase("C:\Users\testi\Acaccess\NewCopy.mdb" )

You can't open a database which dosen't exist yet, you'll have to Create it first.

Here is a simple way:

Code:
Function MakeDB()
Dim db As Database
Dim wrkDefault As Workspace
 
Set wrkDefault = DBEngine.Workspaces(0)
Set db = wrkDefault.CreateDatabase("d:\Test.mdb", dbLangGeneral)
 
wrkDefault.Close 
db.Close
 
Set wrkDefault = Nothing
Set db = Nothing
End Function

JR
 

Users who are viewing this thread

Back
Top Bottom