Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Introduction to Database Processing

A database system is a program that organizes, manipulates, retrieves, and reports data.

If you use a database for your data, you can take advantage of Visual Basic's Data control to access the database from within your Visual Basic applications. The Data control makes it easy for you to retrieve data and display values from a database file without using any of Visual Basic's specific file-related commands that you learned about in the first part of this lesson.

The Data Control

If you use any of the following database applications, you'll be able to write a Visual Basic application that accesses the data within your database without resorting to the file-related commands described earlier in this lesson: Microsoft Access, dBASE, Excel, FoxPro, Lotus, Paradox, and text-based data files.

The Data control makes database access simple.

A field is a column of data inside a file.

A database application manages your data in a record and field format. The database, however, doesn't necessarily store your data in records and fields in a table-like format, but the database makes the data appear to your program in that format. Visual Basic takes advantage of this format and retrieves data in the record and field format no matter how the database physically stores the data.

One challenge when using database access is that you must often describe parts of the database to Visual Basic. Visual Basic cannot magically understand your database structure. When you place the Data control on your form, you'll have to tell the control the structure of your data and tell the Data control which parts of the data to access so that the control can properly retrieve data. For example, by setting appropriate property values, you must tell the Data control the name of your database, the table, and the fields to access.

A table is a logical collection of data in a database. A database might contain several tables.

Some databases, such as Microsoft Access, store all the related database files in a single global file called the database file. Inside the database, the individual groups of records and fields are called tables. Other database systems, such as dBASE, keep track of a database's data in multiple files. When you use a database such as Microsoft Access, as this lesson does, you must describe both the overall database and the individual table name within the database that the Data control is to use.

This lesson doesn't provide you with a table of Data control property descriptions because too many of the descriptions are database related and too advanced for this discussion. You might not need to know more properties than described in the small data application at the end of this lesson in most cases. You'll probably be surprised at the amount of power the Data control gives you.

Figure 15.1 illustrates a Data control in use. Notice that the Data control works much like a VCR's series of buttons. You can step through the database one record at a time. The control itself doesn't display data. Instead, the control simply lets you regulate the access of data. You'll have to supply other controls, such as labels or text boxes, to display and collect data for the database. In other words, if you want the user to be able to move to a particular record, you'll supply a Data control that the user can push to get the record in the database. Then your application can display the data on the form using label controls.

A bound control is a control you can link to a database, via the Data control, that displays and updates database records if the user modifies the data in the bound control.

The Data control is a two-way street; not only does it display database data, but your user can modify the data that the Data control displays, and the Data control makes sure that the changes are made to the underlying database through bound controls. If you don't want the user to be able to change data displayed from a Data control, you can use a label and not a text box to display the database data. You can bind several other controls to the Data controls and make the control read-only so that the user cannot change the underlying database.

A Simple but Powerful Application

Figure 15.1's text boxes are bound to the Data control on the form. Therefore, if the user changes the data in any text box that displays a value from the database, the underlying record's field value changes as well. The application is required to do nothing.

Figure 15.1 shows an application that you are about to build. Do you want to be really shocked? The application will contain no code whatsoever. The entire database access and update can be done just with the controls on the form!

Figure 15.1's book publisher application uses a database that comes with Visual Basic named Biblio.mdb. The database is a Microsoft Access database and contains computer book titles and publishers.

Perform these steps to build Figure 15.1's application:

  1. Create a new project and name the form frmData. Add a caption that says Database Sample. Resize the form to a Height property of 5775 and a Width property of 7170.
  2. Add a label with these properties:
      Name:                   lblAnnounce
      
      Alignment:              2-Center
      
      BorderStyle:            1-Fixed Single
      
      Caption:                Computer Book Publishers
      
      Font:                   Bold 14 points
      
      Height:                 855
      
      Left:                   2160
      
      Top:                    360
      
      Width:                  2895
    
  3. Add five additional field labels as follows:
    • Field 1
              Name:                      lblPub
              
              Alignment:                 1-Right Justify
              
              Caption:                   Publisher:
              
              Font:                      12 points
              
              Left:                      720
              
              Top:                       1800
              
              Width:                     1215
      
    • Field 2
              Name:                      lblAddress
              
              Alignment:                 1-Right Justify
              
              Caption:                   Address:
              
              Font:                      12 points
              
              Left:                      720
              
              Top:                       2400
              
              Width:                     1215
      
    • Field 3
              Name:                      lblCity
              
              Alignment:                 1-Right Justify
              
              Caption:                   City:
              
              Font:                      12 points
              
              Left:                      840
              
              Top:                       3000
              
              Width:                     495
      
    • Field 4
              Name:                      lblState
              
              Alignment:                 1-Right Justify
              
              Caption:                   State:
              
              Font:                      12 points
              
              Left:                      3360
              
              Top:                       3000
              
              Width:                     735
      
    • Field 5
              Name:                      lblZip
              
              Alignment:                 1-Right Justify
              
              Caption:                   Zip:
              
              Font:                      12 points
              
              Left:                      4800
              
              Top:                       3000
              
              Width:                     495
      
  4. Before adding the text boxes, add the Data control. To begin, double-click the Data control to send the control to the center of the form. Change these property values:
       Name:                   dtaBiblio
       
       Caption:                Click to control data display
       
       Left:                   1920
       
       Top:                    4200
       
       Width:                  3255
    
    The text that appears in the center of the Data control is the caption—never data. The Data control doesn't display data. Instead, it regulates the display of data, and you use other fields to hold the displayed data. The text boxes that you place in the next two steps will display the data. You'll bind the text boxes to the Data control, and the Data control will be connected to the database. You must now make that connection; click the DatabaseName property, and then click the ellipsis that appears. Select the Biblio.mdb database from VB's folder (you might have to locate the folder from the file list that appears). Now that the Data control is connected to the database, you can add the text boxes. The database named Biblio.mdb, now connected to the dtaBiblio Data control, contains several tables. Therefore, not only must you tell the Data control which database to use, you must also specify the table source for the data. Select Publishers from the RecordSource property's drop-down list box. The Data control will now produce records only from Biblio.mdb's Publishers table.
  5. Add a text box to the form with these properties:
      Name:                  txtPublisher
      
      BackColor:             (select the ToolTip color)
      
      BorderStyle:           1-Fixed Single
      
      Left:                  2040
      
      Top:                   1800
      
      Width:                 4215
    
    Now you must inform the text box that its data source is the Data control named dtaBiblio. Set the DataSource property to dtaBiblio by selecting dtaBiblio from the DataSource property's pull-down list box. (dtaBiblio is the only item that appears, but if the form contained additional Data controls, you would have to select the proper one for the text box's data source.) The Publishers table, controlled by the Data control, contains several fields. Therefore, not only must you tell the text box which Data control to connect to, but you must also specify the field. Change the DataField property to Company Name. Company Name is the database's field name that holds the publisher name. Set Font to 12.
  6. Add the following four text box controls:
    • Control 1
              Name:                      txtAddress
              
              BackColor:                 ToolTip yellow
              
              BorderStyle:               1-Fixed Single
              
              DataField:                 Address
              
              DataSource:                dtaBiblio
              
              Font Size:                 12
              
              Left:                      2040
              
              Top:                       2400
              
              Width:                     4215
      
    • Control 2
              Name:                      txtCity
              
              BackColor:                 ToolTip yellow
              
              BorderStyle:               1-Fixed Single
              
              DataField:                 City
              
              DataSource:                dtaBiblio
              
              Font Size:                 12
              
              Left:                      1440
              
              Top:                       3000
              
              Width:                     1815
      
    • Control 3
              Name:                      txtState
              
              BackColor:                 ToolTip yellow
              
              BorderStyle:               1-Fixed Single
              
              DataField:                 State
              
              DataSource:                dtaBiblio
              
              Font Size:                 12
              
              Left:                      4200
              
              Top:                       3000
              
              Width:                     555
      
    • Control 4
              Name:                      txtZip
              
              BackColor:                 ToolTip yellow
              
              BorderStyle:               1-Fixed Single
              
              DataField:                 Zip
              
              DataSource:                dtaBiblio
              
              Font Size:                 12
              
              Left:                      5400
              
              Top:                       3000
              
              Width:                     1055
      
  7. Add a separating line with the Line control that has these properties:
      X1:                    0
      
      X2:                    7080
      
      Y1:                    3720
      
      Y2:                    3720
    
    Run the application, and you'll be looking at the first record in the database. Click the Data control's buttons to move through the database records. If you change a value, you'll be changing the actual database itself because of the bound text box controls. Despite the fact that the application requires a lot of controls, no code is required because of the Data control's powerful database retrieval and update capabilities.

Share ThisShare This

Informit Network