![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
Using The ADO Data ControlWhile the ADO data model is relatively simple and easy to use, the programmers task is made even easier by the ADO Data control. You had a brief look at this control in Chapter 20. Placed on a form, the ADO Data control serves as the datasourcethe link between your program and the ADO interfaces. You program an ADO Data control by setting its properties and calling its methods. The control then takes care of the details of creating the required ADO interfaces. By binding other Visual Basic controls, such as Text Boxes, to the ADO Data control, the information is automatically transferred between the database and your programs visual display. The ADO Data control works very much like the Data control used in Chapter 19s Music database program, but the ADO Data control uses the newer ADO data model rather than the older DAO model. Accessing Databases Without The ADO Data ControlIn spite of the ADO Data controls convenience, it does have limitations. For one, only a subset of Visual Basic controls can be bound to the ADO Data control. For another, use of Recordset objects is not as flexible when an ADO Data control is used. Chapter 20 presents some examples of accessing a database without using an ADO Data control. But even with its fairly minor limitations, using the ADO Data control undoubtedly simplifies your programming tasks. I make use of the ADO Data control whenever possible. After all, one of the main reasons I use Visual Basic is to take advantage of the many software components that it offers. If I wanted to do everything myself, I would program in C++. Creating Databases At RuntimeSo far, you have dealt only with situations where the database already exists on disk, having been initially created using the Visual Data Manager. An existing database created by any other methodwith the Access or Paradox database programs, for exampleis handled the same way. But what if the database doesnt exist? Can you create one from scratch using Visual Basic? The answer is yes, but Why the qualification? Indeed, you can create new databases in Visual Basic code, by defining the table and record structure, and then adding new data. In that sense, you can use Visual Basic to write a program that permits users to define their own databases from scratch, rather than limiting them to databases that already exist. Here comes the catch: I generally dont recommend the approach just mentioned, because creating a database from scratch requires a lot of complicated and difficult programming. Not only do you have to accept user specifications regarding table structure, field names, data types, and the likeand convert this information into the necessary commands to create the databasebut you also have to provide the capability to design forms and reports, if the data is to be of any value to the user. While this type of program can and has been written successfully in Visual Basic, writing such a program is not for the fainthearted. Even if you find a custom control that provides much of the required functionality, the project is still daunting, with a lot of potential pitfalls. A much better approach is to use a dedicated database frontend generator, such as Visual Data Manager, Access, or Paradox, which have builtin functionality for creating new databases and designing forms. If you decide to take this approach and avoid Visual Basic altogether, your job as a programmer is to use the applications internal programming language (Access Basic for Access, PAL for Paradox) to create the customized interfaces and queries that the client requires. Alternatively, after the database has been created, you can combine a commercial program (Access or Paradox) for database design and creation with a custom Visual Basic program for decision support and transaction processing. DataAware ControlsAlthough dataaware controls were introduced earlier in the book, now is the time to look at the full complement. Dataaware controls work in conjunction with an ADO Data control. A dataaware control can be bound to a specific field in the data table for which an ADO Data control is serving as datasource. This binding is accomplished with the controls DataSource property (set to the name of the ADO Data control) and its DataField property (set to the name of the desired field in the data table). Ten dataaware controls are provided with the Professional Edition of Visual Basic. The following is a brief review of Visual Basics dataaware controls, each of which can be bound to a single field in a data table:
As useful as these controls are, they are all limited to displaying data from one field in one record at a time. This is simply too limited for certain applications. Three custom dataaware controls can provide additional capabilities: DataGrid, DataList, and DataCombo. The DataGrid ControlThe DataGrid control provides a grid, or matrix, of cells. Each column in the grid displays data from one field in the linked data table, and each row displays data from one record. A DataGrid control is shown in Figure 21.3, displaying data from the Music database developed in an earlier chapter. By default, a DataGrid control displays all the tables fields and records, with vertical and/or horizontal scrollbars if the data is too wide or too tall to display completely. The user can move from cell to cell, viewing and editing data. The bottom row on the control, marked by an asterisk in the left column, can be used to enter new data (this is not shown in Figure 21.3). Column widths can be changed, fonts can be specified, and many other aspects of the controls appearance and behavior can be manipulated via control properties.
The DataGrid control consists of a Columns collection, with one Column object for each column (field) in the control. A DataGrid control can contain a maximum of 32,767 columns; the number of rows is limited only by system resources. Each column is a partially independent object with its own properties. Thus, some properties belong to the DataGrid control itself and affect operation and appearance of the entire control, but each column also has its own set of properties. Each Column object has its own caption (header), font, and so on. The Columns collection has properties that relate to all columns, such as Count, which returns the number of columns in the control. The DataGrid controls features are covered further in the upcoming demonstration program. Refer to the Visual Basic Help system for complete details on its properties, methods, and events. The DataList And DataCombo ControlsThe DataList and DataCombo controls are similar to the regular Combo Box and List Box controls: all four controls provide a list of items from which the user can choose; the difference lies in data binding ability. The DataList and DataCombo controls display a list of items, and the DataCombo adds a TextBox that displays the currently selected item, and permits entry/editing by the user. These controls differ from the standard Combo Box and List Box controls, however, because they are dataaware and can be automatically loaded with values from a datasource. The DataList and DataCombo controls can be extremely useful, to speed the task of data entry and to reduce the chance of errors. For example, when a new Invoice record is entered, the Customer and each Item likely come from existing tables in the database. Rather than require the operator to manually enter the customer name or the items stock number, you can display a DataList control for each field in the data table, showing all the currently available values for that entry. The following demonstration project shows how these controls are used in this manner. Designing The Database ApplicationIn the remainder of this chapter and in the next two chapters, I walk you through all the steps of planning and programming a relational database application by using Visual Basics ADO model. Before writing a single line of code, however, some planning is in order. How careful and thorough you are during the planning stages has a major impact on your speed and efficiency in completing the projectnot to mention the quality of the finished product. The remainder of this chapter covers some general considerations related to database design, walking you through the first steps of designing the demonstration database program. The discussion is limited to designing a new database from scratch, whereby no existing tables or applications are considered. If your own project involves a legacy database, the design process is different. Although you probably wont have the option of changing the table structure, the other considerations still apply.
|
![]() |
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. Read EarthWeb's privacy statement. |