|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
DAO: Creating A DatabaseThe Testing Department is calling again. How about creating a DAO databasein code? Hmm, you think, is that possible? It is, with the objects in the Microsoft DAO Object Library. To add a reference to that library, select the Project|References menu item, select the Microsoft DAO Object Library, and click on OK to close the References dialog box. Now we can make use of the data objects in that library to create a new database using CreateDatabase. CreateDatabase is a method of the DAO Workspace object (there are a collection of Workspace objects in the DAO DBEngine objects Workspaces collection). Heres how you use CreateDatabase: Set database = workspace.CreateDatabase (name, locale [, options]) Here are the arguments to CreateDatabase:
Here are the possible settings for the options argument:
Lets see an example to make this clearer. When the user selects the New database item in our example DAO program, daocode (see the first topic in this chapter), we will create a new database. First, we declare that database, db, as a form-wide variable: Dim db As Database Next, we add a Common Dialog control, CommonDialog1, to the program and show it to get the name of the database file the user wants to create:
Private Sub NewDatabase_Click()
CommonDialog1.ShowSave
If CommonDialog1.FileName <> "" Then
...
Finally, we create the new database, passing the CreateDatabase method the name of the database file and indicating that we want to use the default collating order by passing the constant dbLangGeneral:
Private Sub NewDatabase_Click()
CommonDialog1.ShowSave
If CommonDialog1.FileName <> "" Then
Set db = DBEngine.Workspaces(0).CreateDatabase_
(CommonDialog1.FileName, dbLangGeneral)
End If
End Sub
And thats itweve created a new, empty database. The next step is to add a table to that database, and well take a look at that in the next topic. DAO: Creating A Table With A TableDef ObjectHow do you create a table in a DAO database? You define it with a TableDef object. After you do so, you can append fields to the table, and then you can append the new table definition to a databases TableDefs collection. Lets see an example. After the users create a new database with our DAO code example, the daocode project (see the first topic in this chapter), they can create a new table using the New Table item in the File menu. That item opens the New Table dialog box you see in Figure 25.2.
Users can enter the name of the new table to create in the text boxes in the New Table dialog box, and we can use that information to create a new TableDef object, td, which we declare as a form-wide variable: Dim td As TableDef We create a new TableDef for the Database object we created in the previous topic, db, using the name for the table the user has placed in Text1 in the New Table dialog box:
Sub CreateTable()
Set td = db.CreateTableDef(TableForm.Text1.Text)
...
This code creates a new, empty TableDef object named td. An empty table isnt much use, thoughwell see about adding fields to this object in the next topic.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |