CreateDatabase Method

Applies To   DBEngine object, Workspace object.

Description

Creates a new Database object, saves the database to disk, and returns an opened Database object (Microsoft Jet workspaces only).

Syntax

Set database = workspace.CreateDatabase(name, locale, options)

The CreateDatabase method syntax has these parts.

Part

Description

database

An object variable that represents the Database object you want to create.

workspace

An object variable that represents the existing Workspace object that will contain the database. If you omit workspace, CreateDatabase uses the default Workspace.


(continued)

Part

Description

name

A String up to 255 characters long that is the name of the database file that you're creating. It can be the full path and file name, such as "C:\db1.mdb". If you don't supply a file name extension, .mdb is appended. If your network supports it, you can also specify a network path, such as "\\server1\share1\dir1\db1". You can only create .mdb database files with this method.

locale

A string expression that specifies a collating order for creating the database, as specified in Settings. You must supply this argument or an error occurs.

You can also create a password for the new Database object by concatenating the password string (starting with ";pwd=") with a constant in the locale argument, like this:

dbLangSpanish & ";pwd=NewPassword"

If you want to use the default locale, but specify a password, simply enter a password string for the locale argument:

";pwd=NewPassword"

options

Optional. A constant or combination of constants 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

dbLangChineseSimplified

Simplified Chinese

dbLangChineseTraditional

Traditional Chinese

dbLangCyrillic

Russian

dbLangCzech

Czech

dbLangDutch

Dutch

dbLangGreek

Greek

dbLangHebrew

Hebrew

dbLangHungarian

Hungarian

dbLangIcelandic

Icelandic

dbLangJapanese

Japanese

dbLangKorean

Korean

dbLangNordic

Nordic languages (Microsoft Jet database engine version 1.0 only)


(continued)

dbLangNorwDan

Norwegian and Danish

dbLangPolish

Polish

dbLangSlovenian

Slovenian

dbLangSpanish

Traditional Spanish

dbLangSwedFin

Swedish and Finnish

dbLangThai

Thai

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 Microsoft Jet database engine version 1.0 file format.

dbVersion11

Creates a database that uses the Microsoft Jet database engine version 1.1 file format.

dbVersion20

Creates a database that uses the Microsoft Jet database engine version 2.0 file format.

dbVersion30

(Default) Creates a database that uses the Microsoft Jet database engine version 3.0 file format (compatible with version 3.5).


If you omit the encryption constant, CreateDatabase creates an un-encrypted database. You can specify only one version constant. If you omit a version constant, CreateDatabase creates a database that uses the Microsoft Jet database engine version 3.0 file format.

Remarks

Use the CreateDatabase method to create and open a new, empty database, and return the Database object. You must complete its structure and content by using additional DAO 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, OpenDatabase method.

Specifics (Microsoft Access)

The CreateDatabase method creates a new database from Visual Basic, opens the new database, appends it to the Databases collection, and saved to disk.

Use of the CreateDatabase command doesn't affect the database that's currently open in the Microsoft Access Database window. A database that you've opened in the Database window remains open. This database is the first database in the Databases collection. Also, the CurrentDb function returns a reference to the database that's open in the 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.

Sub CreateDatabaseX()

    Dim wrkDefault As Workspace
    Dim dbsNew As DATABASE
    Dim prpLoop As Property

    ' Get default Workspace.
    Set wrkDefault = DBEngine.Workspaces(0)

    ' Make sure there isn't already a file with the name of
    ' the new database.
    If Dir("NewDB.mdb") <> "" Then Kill "NewDB.mdb"

    ' Create a new encrypted database with the specified
    ' collating order.
    Set dbsNew = wrkDefault.CreateDatabase("NewDB.mdb", _
        dbLangGeneral, dbEncrypt)

    With dbsNew
        Debug.Print "Properties of " & .Name
        ' Enumerate the Properties collection of the new
        ' Database object.
        For Each prpLoop In .Properties
            If prpLoop <> "" Then Debug.Print "    " & _
                prpLoop.Name & " = " & prpLoop
        Next prpLoop
    End With

    dbsNew.Close

End Sub
Example (Microsoft Access)

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

Sub NewDatabase()
    Dim wspDefault As Workspace, dbs As Database
    Dim tdf As TableDef, fld1 As Field, fld2 As Field
    Dim idx As Index, fldIndex As Field

    Set wspDefault = DBEngine.Workspaces(0)
    ' Create new, encrypted database.
    Set dbs = wspDefault.CreateDatabase("Newdb.mdb", _
        dbLangGeneral, dbEncrypt)
    ' Create new table with two fields.
    Set tdf = dbs.CreateTableDef("Contacts")
    Set fld1 = tdf.CreateField("ContactID", dbLong)
    fld1.Attributes = fld1.Attributes + dbAutoIncrField
    Set fld2 = tdf.CreateField("ContactName", dbText, 50)
    ' Append fields.
    tdf.Fields.Append fld1
    tdf.Fields.Append fld2
    ' Create primary key index.
    Set idx = tdf.CreateIndex("PrimaryKey")
    Set fldIndex = idx.CreateField("ContactID", dbLong)
    ' Append index fields.
    idx.Fields.Append fldIndex
    ' Set Primary property.
    idx.Primary = True
    ' Append index.
    tdf.Indexes.Append idx
    ' Append TableDef object.
    dbs.TableDefs.Append tdf
    dbs.TableDefs.Refresh
    Set dbs = Nothing
End Sub
Example (Microsoft Excel)

See the Append method example (Microsoft Excel).