>

CreateDatabase Method

Applies To

Workspace Object.

Description

Creates a new Database object, saves the database on disk and returns an opened Database object.

Syntax

Set database = workspace.CreateDatabase (databasename, locale [, options])

The CreateDatabase method syntax has these parts.

Part

Description

database

A variable of an object data type that references the Database object you're creating.

workspace

A variable of an object data type that represents the existing Workspace object that will contain the database. If you omit the workspace part, the default Workspace is used.

databasename

A string expression that is the name of the database file that you're creating. It can be the full path and filename, such as "C:\MYDB.mdb". If you don't supply a filename extension, .mdb is appended. If your network supports it, you can also specify a network path, such as "\\MYSERVER\MYSHARE\MYDIR\MYDB". Only .mdb database files can be created using this method.

locale

A string expression used to specify collating order for creating the database. You must supply this argument or an error will occur. See the table that lists locale constants later in this topic.

options

An integer that indicates one or more options, as specified in Settings. You can combine options by summing the corresponding constants.


Settings

You can use one of the following constants for the locale argument to specify the CollatingOrder property of text for string comparisons.

Constant

Collating order

dbLangGeneral

English, German, French, Portuguese, Italian, and Modern Spanish

dbLangArabic

Arabic

dbLangCyrillic

Russian

dbLangCzech

Czech


Constant

Collating order

dbLangDutch

Dutch

dbLangGreek

Greek

dbLangHebrew

Hebrew

dbLangHungarian

Hungarian

dbLangIcelandic

Icelandic

dbLangNordic

Nordic languages (Microsoft Jet database engine version 1.0 only)

dbLangNorwdan

Norwegian and Danish

dbLangPolish

Polish

dbLangSpanish

Traditional Spanish

dbLangSwedfin

Swedish and Finnish

dbLangTurkish

Turkish


You can use one or more of the following constants in the options argument to specify which version the data format should have and whether or not to encrypt the database.

Constant

Description

dbEncrypt

Creates an encrypted database.

dbVersion10

Creates a database that uses the Jet database engine version 1.0.

dbVersion11

Creates a database that uses the Jet database engine version 1.1.

dbVersion20

Creates a database that uses the Jet database engine version 2.0.

dbVersion30

Creates a database that uses the Jet database engine version 3.0.


If you omit the encryption constant, an unencrypted database is created. You can specify only one version constant. If you omit a version constant, a database that uses the Jet database engine version 3.0 is created.

Remarks

The CreateDatabase method creates and opens a new, empty database and returns the Database object. You must complete its structure and content using additional data access objects. If you want to make a partial or complete copy of an existing database, you can use the CompactDatabase method to make a copy that you can customize.

See Also

CollatingOrder Property, CompactDatabase Method, Database Object, DBEngine Object, OpenDatabase Method.

Specifics (Microsoft Access)

You can use the CreateDatabase method to create a new database from Visual Basic. The new database is immediately opened, appended to the Databases collection, and saved to disk. However, only the current database is open in the Microsoft Access Database window. To open a database that you have created with CreateDatabase in the Database window, you must click Open Database on the File menu.

Example

This example uses CreateDatabase to create a new, encrypted Database object. The example customizes some properties of the new database, completes its structure, and then closes it.


Dim wspDefault As Workspace, dbsDefault As Database, dbsNew As Database
Set wspDefault = DBEngine.Workspaces(0)
Set dbsDefault = wspDefault.OpenDatabase("Northwind.mdb")
If Dir$("NewDB.mdb") <> "" Then 
    Kill "NewDB.mdb"
Set dbsNew = wspDefault.CreateDatabase("NewDB.mdb", dbLangGeneral, _
    dbEncrypt)
dbsNew.QueryTimeout = dbsDefault.QueryTimeout
...
' Complete structure of dbsNew, possibly using structure of dbsDefault.
...
dbsNew.Close
dbsDefault.Close
Example (Microsoft Access)

The following example uses CreateDatabase to create a new, encrypted database named Newdb.mdb.


Sub NewDatabase()
    Dim wspDefault As Workspace, dbsNew As Database

    Set wspDefault = DBEngine.Workspaces(0)
    ' Create new, encrypted database.
    Set dbsNew = wspDefault.CreateDatabase ("Newdb.mdb", _
        dbLangGeneral, dbEncrypt)
    ' Create and append tables to new database.
    .
    .
    .
    dbsNew.Close
End Sub

Example (Microsoft Excel)

This example creates a new database (NWINDEX.MDB). The example attaches two tables from the C:\Program Files\Common Files\Microsoft Shared\MSquery folder to the database. (On Windows NT, the two tables are in the \WINDOWS\MSAPPS\MSQUERY folder.)


Const sourceDir = "C:\Program Files\Common Files\Microsoft Shared\"

Sub createNWindEx()
    Dim nWindEx As Database, customerTable As TableDef, _
supplierTable As TableDef Dim dataSource As String dataSource = "dbase IV;DATABASE=" & sourceDir & "MSquery" appPath = Application.Path Set nWindEx = Workspaces(0).CreateDatabase(Application.Path _ & "\NWINDEX.MDB", dbLangGeneral) Set customerTable = nWindEx.CreateTableDef("Customer") customerTable.Connect = dataSource customerTable.SourceTableName = "Customer" nWindEx.TableDefs.Append customerTable Set supplierTable = nWindEx.CreateTableDef("Supplier") supplierTable.Connect = dataSource supplierTable.SourceTableName = "Supplier" nWindEx.TableDefs.Append supplierTable MsgBox "The database " & nWindEx.Name & " has been created." nWindEx.Close End Sub