|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
The Data Form Wizard: Creating A Data FormYou can use the Visual Basic Data Form Wizard to create a form using an ADO control or ADO code that lets you open and edit a database. To use the Data Form Wizard, select it in the Visual Basic Add-Ins menu (if its not there, add it with the Add-Ins Manager in the Add-Ins menu) and follow the steps in the Data Form Wizard, one step for each successive window that appears in the Wizard:
Lets see an example to make this clearer. Create a new project, and remove Form1. Next, follow the preceding steps to create a new data form named Form1 using our db.mdb database, and set the projects startup object (using the Visual Basic Project|Properties menu item) to Form1. When you run this example, the records of our database appear in the data form, as shown in Figure 24.17. You can move through the database, editing the records as you like. When you change a record, click the Update button to change the actual record in the database file itself. Now were editing databases with our own programs in Visual Basic. The code for this example is located in the dataentry folder on this books accompanying CD-ROM.
Using Database Control Methods: Adding, Deleting, And Modifying RecordsThe Testing Department is calling again: your SuperDuperDataBase program is terrific, but instead of restricting users to simply moving through a database, how about letting them edit the data in that text box, adding new records and so on? Hmm, you think, how would that work? Like most controls, the DAO, RDO, and ADO controls have methods, events, and properties. To make these controls consistent, Microsoft has given them the same core methods, and well take a look at those methods in this chapter. Using these methods, users can add records to a database, change those records, delete them, and move around in the database. In the next few topics, well develop the program you see in Figure 24.18, where were editing the db.mdb database we developed in the beginning of the chapter. Because all data controls have the same core methods, well use a data control, Data1, in this example to keep this example easy. We also use two text boxes, Text1 and Text2, connected to Data1 and the Name and Grade fields in our database, respectively. Now all we have to do is to make all the buttons in the program active, and well do that in the following few topics. The code for this example is located in the dbmethods folder on this books accompanying CD-ROM.
Adding Records To DatabasesYou can add a new record to a database with the AddNew method of the Recordset property of a data or ADO data control, or of the Resultset property of a remote data control. Lets see an example. When the user clicks the Add button in the dbmethods example were developing in this and the previous few topics, we can add a new record like this: Private Sub cmdAdd_Click() Data1.Recordset.AddNew End Sub This adds a new, blank record. You can enter the data you want in the records fields, and to update the database, you click the Update button. Deleting Records In DatabasesYou can delete a record in a database with the Delete method of the Recordset property of a data or an ADO data control, or of the Resultset property of a remote data control. Lets see an example. When the user clicks the Delete button in the dbmethods example were developing in this and the previous few topics, we can delete a record like this: Private Sub cmdDelete_Click() Data1.Recordset.Delete ... End Sub To avoid displaying a blank record, we also move to the next record this way: Private Sub cmdDelete_Click() Data1.Recordset.Delete Data1.Recordset.MoveNext End Sub Refreshing A Data ControlWhen working with multiple databases, you can refresh the data in the current database control with the Refresh method of the data, ADO data control, or the remote data control. Lets see an example. When the user clicks the Refresh button in the dbmethods example were developing in this and the previous few topics, we can refresh the control like this: Private Sub cmdRefresh_Click() Data1.Refresh End Sub
|
|
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. |