To support the creation of a database, the Access.Application object is equipped with a method named NewCurrentDatabase. Its syntax is: Access.Application.NewCurrentDatabase(filepath, _ FileFormat, _ Template, _ SiteAddress, _ ListID) Only the first argument is required. You can pass it as the name of the new database. Here is an example: Private Sub cmdCreateDatabase_Click() Dim ROSH As Access.Application Set ROSH = CreateObject("Access.Application") ROSH.NewCurrentDatabase "Red Oak High School" End Sub If you do not specify an extension, the database engine would find out the latest version of Microsoft Access that is installed in your computer. If it is Microsoft Access 2007, a new database with extension .accdb would be created. Still, if you want, you can add an extension. If you have Microsoft Access 2007 but want to specify the version of database you want to create, pass the second argument. The second argument is a member of the AcNewDatabaseFormat enumeration. The available values are:
Here is an example: Private Sub cmdCreateDatabase_Click() Dim ROSH As Access.Application Set ROSH = CreateObject("Access.Application") ROSH.NewCurrentDatabase "Red Oak High School", acNewDatabaseFormatAccess2007 End Sub The other arguments are optional.
Once a database has been created, in order to use it, you must open it. To open a database in the Microsoft Office 12.0 Access Database Engine Library, you can call the OpenCurrentDatabase() method of the Access.Application class. Its syntax is: Access.Application.OpenCurrentDatabase(filepath, Exclusive, bstrPassword) The first argument can be the name of the database. If you provide (only) the name of the database, the engine would look for it in the same directory as the application that called it. Otherwise, you should provide the complete path to the database.
After using a database, you must close it to free the resources it was using. To close a database using the Microsoft Office 12.0 Access Database Engine Library, call the CloseCurrentDatabase() method. Its syntax is: Access.Application.CloseCurrentDatabase() As you can see, this method takes no argument. After calling this method, set its variable to Nothing. Here is an example: Private Sub cmdCloseDatabase_Click() Dim ROSH As Access.Application Set ROSH = CreateObject("Access.Application") ROSH.CloseCurrentDatabase Set ROSH = Nothing End Sub When calling this method, the database must have been previously opened. If the database was not opened or it was already closed, you would receive a 2467 error.
In the previous lesson, saw that DAO is one of the most ancient and reliable libraries of Microsoft Access and used by applications published by companies other than Microsoft. Based on this, DAO supports all types of operations that can be performed on a database. One of these basic operations consist of creating a database. To support the creation of a database, the DBEngne class of the DAO library is equipped with a method named CreateDatabase(). Its syntax is: DBEngine.CreateDatabase(ByVal Name As String, _ ByVal locale As String, _ ByVal options As Variant) As Database Because the DBEngine object is already recognized in the current database, you can omit it when calling the CreateDatabase() method. The arguments of this method are the same as those of the CreateDatabase() method of the Workspace class we reviewed for the Microsoft Access library. When the CreateDatabase() method has finished, it returns a reference to the database that was created and you must obtain that reference. The database is recognized as the Database object of DAO. To get it, first declare a variable of type Database. Because the DAO object is implied, you can omit it in your declaration and simply use the Database object as type. To get a reference to the new database, use the SET operator and assign the returned value of the method to your Database variable. Here is an example that creates a new database named Exercise.accdb in the current folder: Private Sub cmdCreate_Click() Dim db As DAO.Database Set db = CreateDatabase("Exercise.accdb", dbLangGeneral) End Sub
To use a database, of course you must first open it. To support this operation, the DBEngine class of the DAO library provides the OpenDatabase() method whose syntax is: Public Function DBEngine.OpenDatabase(ByVal Name As String, _ ByVal Options As Boolean, _ ByVal ReadOnly As Boolean, _ ByVal Connect As String) As Database As you can see, the arguments of this method are exactly the same as those of the Workspace class of the Microsoft Access Object library. This means that the arguments follow the same description we saw already. Here is an example of calling the method: Private Sub cmdOpenDatabase_Click() OpenDatabase("Example.accdb") End Sub When the DBEngine.OpenDatabase() method has been called, it returns a Database object. If you want to continue doing anything on the open database, you must get this Database value. Here is an example of getting it: Private Sub cmdOpenDatabase_Click() Dim db As DAO.Database Set db = OpenDatabase("Example.accdb") . . . Now you can use the Database object End Sub Remember that the other arguments of the DBEngine.OpenDatabase() method follow the same description we saw for the Workspace class.
After using a database, you should close it to release the resources it was consuming. To do this, you can call the Close() method of the Database object. We saw that, to get a reference to the database that is being opened, you can get the return value of calling the OpenDatabase() method of the DBEngine class. This is done exactly as we saw for the Database class of the Microsoft Access Object library. Here is an example: Private Sub cmdUseDatabase_Click() Dim db As DAO.Database Set db = OpenDatabase("Example.accdb") . . . Now you can use the Database object db.Close End Sub
Before using ADO, you should first create a database using an application such as Microsoft Access or by other related means. Among the limitations of ADO in the beginning, there were security and even the ability to create a database without necessarily using a product from Microsoft. To address these issues, Microsoft created an additional library called Microsoft ActiveX Data Objects Extensions for Data Definition Language and Security, abbreviated ADOX. Before using ADOX, you must reference it in Microsoft Visual Basic. To do this, you can open the References dialog box from the Tools menu and select your latest version of Microsoft ADOX: Like Microsoft Access' own library and like DAO, ADO relies on objects for its functionality. The most fundamental object used in ADO databases is ADOX. To use this object, you must create a reference to it. To do this, first declare a variable of type ADOX.
To support the creation of a database, the ADOX library provides the Catalog object. To access this object, the ADOX object is equipped with a class named Catalog. This class defines most of the objects you will need to use in your databases. To use the Catalog class, declare a variable for it and qualify it by using ADOX.Catalog. Here is an example: Private Sub cmdAction_Click() Dim objCatalog As ADOX.Catalog End Sub Before using the Catalog object, you must specify that you need a reference to it, this can be done by initializing your variable with ADOX.Catalog using the New operator. Here is an example: Private Sub cmdAction_Click() Dim objCatalog As ADOX.Catalog Set objCatalog = New ADOX.Catalog End Sub You can also refer to this property when declaring the variable. Here is an example: Private Sub cmdAction_Click() Dim objCatalog As New ADOX.Catalog End Sub Whenever you create an ADO or ADOX object and while it is being used, it consumes resources. Therefore, after using the object, you should/must remove it from memory (if you don't, you may start seeing abnormal behavior on your computer after a while, as resources become scarce while other objects or applications are trying to use the same resources). To remove a resource from memory, after using it, assign it the Nothing constant.
ADO is probably the most popular library of Microsoft Access used outside of Microsoft. Most non-Microsoft programming environments that want to access a Microsoft Access database use this library. But as we saw in the previous lesson, you cannot create a database in ADO. Instead, you use its sister library the ADOX.
To create a database in ADOX, the ADOX library is equipped with a class named Catalog. To support database creation, the Catalog class is equipped with a method named Create. Its syntax is: Catalog.Create(ByVal ConnectString As String) This would be started as follows: Private Sub cmdAction_Click() Dim objCatalog As ADOX.Catalog Set objCatalog = New ADOX.Catalog objCatalog.Create Set objCatalog = Nothing End Sub The Create() method of the Catalog class method takes as argument a string, also called a connection string, that defines the assignment to perform. The connection string follows the same description we reviewed for the Connection object. If you are dealing with a Microsoft SQL Server database, specify the provider as SQLOLEDB. Here is an example: Private Sub cmdAction_Click() Dim objCatalog As ADOX.Catalog Set objCatalog = New ADOX.Catalog objCatalog.Create "provider=SQLOEDB" Set objCatalog = Nothing End Sub If you are dealing with a Microsoft Access database, specify the provider as Microsoft.JET.OLEDB.4.0 (case insensitive). Here is an example: Private Sub cmdAction_Click() Dim objCatalog As ADOX.Catalog Set objCatalog = New ADOX.Catalog objCatalog.Create "provider=Microsoft.Jet.OLEDB.4.0" Set objCatalog = Nothing End Sub Remember that you can include the name of the provider in single-quotes: Private Sub cmdAction_Click() Dim objCatalog As ADOX.Catalog Set objCatalog = New ADOX.Catalog objCatalog.Create "provider='Microsoft.Jet.OLEDB.4.0'" Set objCatalog = Nothing End Sub If you are creating a SQL Server type of database, there are various other details you must provide to the connection string. For the rest of our lessons, we will use Microsoft JET. To specify the second part of the connection string, which is the data source, use the same approach we saw for the Connection object. Here is an example: Private Sub cmdCreateDatabase_Click() Dim objCatalog As ADOX.Catalog Set objCatalog = New ADOX.Catalog objCatalog.Create "provider=Microsoft.Jet.OLEDB.4.0;Data Source=Exercise.accdb" Set objCatalog = Nothing End Sub If you are creating a database and you provide (only) the name of the database, it would be created in the same folder as the application that called it. In the above example, the database would be created in the My Documents folder. If you want to create the database in a folder other than that of the application that called this method, provide a complete path of the database. Here is an example: Private Sub cmdCreateDatabase_Click() Dim objCatalog As ADOX.Catalog Set objCatalog = New ADOX.Catalog objCatalog.Create "provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\Exercises\Exercise.accdb" Set objCatalog = Nothing End Sub If you are creating a Microsoft Access Office 2007 database, specify the extension as .accdb. If you want to create a database compatible with previous versions of Microsoft Access, specify the extension as .mdb. Remember that you can create the connection string and store it in a String variable. Then pass that variable to the Create() method. Here is an example: Private Sub cmdAction_Click() Dim objCatalog As ADOX.Catalog Dim strCreator As String Set objCatalog = New ADOX.Catalog strCreator = "provider=Microsoft.Jet.OLEDB.4.0;'" strCreator = strCreator & "Data Source=C:\Exercises\Exercise.mdb'" objCatalog.Create strCreator Set objCatalog = Nothing End Sub |
|
||||||||||||||||||||||||||||||||||||||||||||||
|