841 845




Visual Basic 6 Black Book:Databases: Using DAO, RDO, And ADO
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive To access the contents, click the chapter and section titles. Visual Basic 6 Black Book (Publisher: The Coriolis Group) Author(s): Steven Holzner ISBN: 1576102831 Publication Date: 08/01/98 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next





The Data Form Wizard: Creating A Data Form
You 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 it’s 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:


1.  Introduction window. This window asks if you want to load a profile to create the data form; click Next.
2.  Database Type. This window lets you select the database format, like Microsoft Access or ODBC. For the example database we’ve developed in this chapter, db.mdb, select the Access type. Click Next.
3.  Database. This window lets you select the database to work with. Use the Name box and the Browse button to select your database or data source. The Data Form Wizard will create the correct connection string for the ADO data control. Click Next.
4.  Form. This window lets you specify the name for the form you’re creating, as well as the form layout (how the data will be displayed): single record, grid, and so on, as shown in Figure 24.15. You can also specify how to bind the database to the form: with an ADO control or ADO code, or with a class (we’ll see more about classes when we discuss code components in Chapter 27). For our example database, we will use an ADO control for the binding. Click Next to go to the next window.

Figure 24.15  Setting up a data entry form.
5.  Record Source. In this window, select the table name and the fields you want displayed, as shown in Figure 24.16. Click Next to go to the next window.

Figure 24.16  Selecting the record source.
6.  Control Selection. Specify the buttons you want in the data entry form: Add, Delete, Refresh, and so on. For the example we’ll create later in this topic, leave all options selected and click Next.
7.  Finished!. Click Finish in this window to create the new data form.

Let’s 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 project’s 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 we’re editing databases with our own programs in Visual Basic. The code for this example is located in the dataentry folder on this book’s accompanying CD-ROM.

Figure 24.17  The ADO data entry form.
Using Database Control Methods: Adding, Deleting, And Modifying Records
The 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 we’ll 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, we’ll develop the program you see in Figure 24.18, where we’re editing the db.mdb database we developed in the beginning of the chapter. Because all data controls have the same core methods, we’ll 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 we’ll do that in the following few topics. The code for this example is located in the dbmethods folder on this book’s accompanying CD-ROM.

Figure 24.18  Using data control methods to add, edit, and delete records.
Adding Records To Databases
You 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. Let’s see an example. When the user clicks the Add button in the dbmethods example we’re 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 record’s fields, and to update the database, you click the Update button.

Deleting Records In Databases
You 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. Let’s see an example. When the user clicks the Delete button in the dbmethods example we’re 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 Control
When 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. Let’s see an example. When the user clicks the Refresh button in the dbmethods example we’re developing in this and the previous few topics, we can refresh the control like this:


Private Sub cmdRefresh_Click()
Data1.Refresh
End Sub






Previous
Table of Contents
Next






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.



Wyszukiwarka

Podobne podstrony:
838 841
838 841
810 845
META 845
Golec uOrkiestra Kochom ciebie dziewczyno (845)
LCD TV 84 Aper TV 845 1
845 847
mbdch20 845
841 843
mbdch20 841
LCD TV 84 Aper TV 845

więcej podobnych podstron