Saturday, January 3, 2009

Creating a Database with DAO and ADO

Before tables or other objects can be defined, the database itself must be created. The following code creates and opens a new Microsoft Jet database.

DAO

Sub DAOCreateDatabase()

Dim db As DAO.Database

Set db = DBEngine.CreateDatabase(".\New.mdb", dbLangGeneral)

End Sub

ADOX

Sub ADOCreateDatabase()

Dim cat As New ADOX.Catalog

cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=.\New.mdb;"

End Sub

As discussed earlier in this document (see the section titled "ADOX: Data Definition and Security"), the DAO Database object corresponds to the Catalog object in ADOX. So to create a new Jet database using ADOX, you use the Catalog objects Create method.

In the DAO code above, the Locale parameter is specified as dbLangGeneral. In the ADOX code, locale is not explicitly specified. The default locale for the Microsoft Jet Provider is equivalent to dbLangGeneral. Use the ADO Locale Identifier property to specify a different locale.

In DAO, CreateDatabase also can take a third Options parameter, specifying information for encrytion and database version. For example, the following line is used to create an encrypted, version 1.1 Microsoft Jet database:

   Set db = DBEngine.CreateDatabase(".\New.mdb", dbLangGeneral, _
dbEncrypt + dbVersion11)

In ADO, encryption and database version information is specified by provider-specific properties. With the Microsoft Jet Provider, use the Encrypt Database and Engine Type properties, respectively. The following line of code specifies these values in the connection string to create an encrypted, version 1.1 Microsoft Jet database:

   cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=.\New.mdb;" & _
"Jet OLEDB:Encrypt Database=True;" & _
"Jet OLEDB:Engine Type=2;"

No comments:

Post a Comment