|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Creating A Table With The Visual Data ManagerTo add a table named students to the db.mdb database we created in the previous topic, right-click the Properties item in the Visual Data Managers Database window and select the New Table item, opening the Table Structure window you see in Figure 24.4.
Give this new table the name students by typing that into the Table Name field, as shown in Figure 24.4. Thats itit was as quick as that to create a new table. Now that weve created a new table, well add the new fields in this table (Name and Grade) in the next topic. Creating A Field With The Visual Data ManagerTo add fields to the database table named students we created in the previous topic, click the Add Field button in the Visual Data Managers Table Structure window, opening the Add Field dialog box you see in Figure 24.5.
Give this new field the name Name by typing that into the box labeled Name and clicking on the OK button. The Add Field dialog box stays open, and the new field is added to the students table. Add another field named Grade in the same way. Click on OK to add the field, then click Close to close the Add Field dialog box. Now in the Table Structure window, click the Build The Table item to build the new table with our two new fields. This creates the students table with two fields: Name and Grade, and opens that table in the Visual Data Managers Database window, as you can see in Figure 24.6.
In the next topic, well enter data into our new table. Entering Data In A Database With The Visual Data ManagerTo enter data into a table in the Visual Data Manager, right-click the tables entry in the Visual Data managers Database window and select the Open item in the menu that opens. In our case, doing so opens the Table: students dialog box you see in Figure 24.7. Well use this dialog box to enter the data in our databases records. Well use the data you saw in Figure 24.1, but you can also use it to edit that data.
Using the data in Figure 24.1, enter the name Ann and the grade C for the first student in the labeled boxes in the Table: students dialog box, as shown in Figure 24.7. Then click Update to add that new record to the database; when the Visual Data manager displays a message box asking if you want to add the new record to the database, click Yes. To add a new record, click the Add button and add the name Mark and the grade B, and click Update. Click Yes when the Visual Data Manager asks you if you want to add the new record to the database. When youre done entering your records, click the Close button in the Table: students dialog box and close the database with the Close item in the Visual Data managers File menu. Congratulationsyouve created a new database. Adding A Data Control To A ProgramThe Testing Department is calling again. Now that youve created a new database file, how about using it in a program? Okay, you say, Ill look into it. The database weve created in the previous few topics is in Microsoft Access (Jet) format, which is a format you can use with all three data access object sets: DAO, RDO, and ADO. Well see how to use the simplest method first: connecting that database to a data control in a Visual Basic program and using that data control with data-bound controls. The data control is the only intrinsic database controlits already in the toolbox. This controls tool appears as the tenth tool down on the right in the toolbox in Figure 24.8. Double-click that tool now to add a data control to your form.
Stretch the data control as you want it. When you stretch the control beyond its original size, youll see a space for text in the center of the control; set the controls Caption property to the name of the database table well be working withstudents. Thats all it takes to add a data control to your program. In the next topic, well see how to connect the data control to a database. Opening A Database With The Data ControlTo connect a data control to a database, you just set the data controls DatabaseName property to the path and name of the Access/Jet database file you want to open. Here, well use the db.mdb file weve created at the beginning of this chapter. In addition, you select the table you want to work with in that file with the data controls RecordSource property. After connecting the control to the db.mdb database, select the students table in the drop-down list box for the RecordSource property in the Properties window. Youve connected the data control to a table in a database. But how do you actually see or modify the data in the database? You do that with bound controls, and well get an introduction to that 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. |