You can use the NewCurrentDatabase method to create a new database in the Microsoft Access window. You can use the this method to create a new database from another application that is controlling Microsoft Access through OLE Automation. For example, you can use the NewCurrentDatabase method from Microsoft Excel to create a new database in the Microsoft Access window.
application.NewCurrentDatabase dbname
The NewCurrentDatabase method uses 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 filename extension. If your network supports it, you can also specify a network path in the following form. |
\\Server\Share\Directory\Filename.mdb |
The NewCurrentDatabase method enables you to create a new Microsoft Access database from another application through OLE Automation. Once you have created an instance of Microsoft Access from another application, you must also create a new database or specify a particular database to open. 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.
OpenCurrentDatabase Method.
The following example creates a new Microsoft Access database from another application through OLE Automation, 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 OLE Automation controller. For example, you might run the following code from Microsoft Excel or 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. ' From Microsoft Excel, use "Dim appAccess As Object".appAccess As Access.Application 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.7") ' Open database in Microsoft Access window. appAccess.NewCurrentDatabase(strDB) ' Get Database variable. Set dbs = appAccess.CurrentDb ' Create new table. Set tdf = dbs.CreateTableDef("Contacts") ' Create field in new table. Set fld = tdf.CreateField("Name", dbText, 20) ' Append Field and TableDef objects. tdf.Fields.Append fld dbs.TableDefs.Append tdfSub
Note From some applications, such as Microsoft Visual Basic 4.0, 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.