NewCurrentDatabase Method

Applies To

Application object.

Description

You can use the NewCurrentDatabase method to create a new database in the Microsoft Access window. You can use this method to create a new database from another application that is controlling Microsoft Access through Automation, formerly called OLE Automation. For example, you can use the NewCurrentDatabase method from Microsoft Excel to create a new database in the Microsoft Access window.

Syntax

application.NewCurrentDatabase dbname

The NewCurrentDatabase method has the following arguments.

Argument

Description

application

The Application object.

dbname

A string expression that is the name of a new database file, including the path name and the file name extension. If your network supports it, you can also specify a network path in the following form:

\\Server\Share\Folder\Filename.mdb


Remarks

The NewCurrentDatabase method enables you to create a new Microsoft Access database from another application through Automation. Once you have created an instance of Microsoft Access from another application, you must also create a new database. This database opens in the Microsoft Access window.

If the database identified by dbname already exists, an error occurs.

The new database is opened under the Admin user account.

See Also

CloseCurrentDatabase method, OpenCurrentDatabase method.

Example

The following example creates a new Microsoft Access database from another application through Automation, and then creates a new table in that database.

You can enter this code in a Visual Basic module in any application that can act as an ActiveX component. For example, you might run the following code from Microsoft Excel, Microsoft Visual Basic, or even Microsoft Access.

When the variable pointing to the Application object goes out of scope, the instance of Microsoft Access that it represents closes as well. Therefore, you should declare this variable at the module level.

' Include following in Declarations section of module.
Dim appAccess As Access.Application

Sub NewAccessDatabase()
    Dim dbs As Database, tdf As TableDef, fld As Field
    Dim strDB As String

    ' Initialize string to database path.
    strDB = "C:\My Documents\Newdb.mdb"
    ' Create new instance of Microsoft Access.
    Set appAccess = _
        CreateObject("Access.Application.8")
    ' Open database in Microsoft Access window.
    appAccess.NewCurrentDatabase strDB
    ' Get Database object variable.
    Set dbs = appAccess.CurrentDb
    ' Create new table.
    Set tdf = dbs.CreateTableDef("Contacts")
    ' Create field in new table.
    Set fld = tdf. _
        CreateField("CompanyName", dbText, 40)
    ' Append Field and TableDef objects.
    tdf.Fields.Append fld
    dbs.TableDefs.Append tdf
    Set appAccess = Nothing
End Sub
Note From some applications, such as Microsoft Visual Basic, you can include the New keyword when declaring the Application object variable. This keyword automatically creates a new instance of Microsoft Access, without requiring you to use the CreateObject function. Check your application's documentation to determine whether it supports this syntax.