![]() |
|||
![]()
|
![]() |
![]() |
![]() |
Day 1Your First Database Program in Visual Basic 5This chapter is for readers who have never created database applications using Visual Basic. Those who already know how to use the Visual Basic data control and the bound controls to make simple data entry programs might want to skip this chapter and move on to Day 2, "Creating Databases." Your project today is to create a completely functional data entry program using Visual Basic. The program you create will be able to access data tables within an existing database; it will also allow users to add, edit, and delete records. Sound like a lot for one day? Not really. You will be amazed at how quickly you can put together database programs. Much of the drudgery commonly associated with writing data entry programs (screen layout, cursor control, input editing, and so on) is automatically handled using just a few of Visual Basic's input controls. In addition, with Visual Basic's data controls it's easy to add the capability to read and write database tables, too. So let's get started! Starting Your New Visual Basic ProjectIf you already have Visual Basic up and running on your PC, select File | New Project to create a new project. If you haven't started Visual Basic yet, start it now. Select Standard EXE and click OK in the dialog that appears. Now you're ready to create the data entry screen. Adding the Database ControlThe first thing you need to do for the database program is open up the database
and select the data table you want to access. To do this, double-click the data control
in the Visual Basic toolbox (see Figure 1.1). This places a data control in the center
of the form. When this is done, the form is ready to open a data table. At this point,
your screen should look something like the one in Figure 1.1.
Next you need to set a few of the control's properties to indicate the database and data table you want to access. Setting the DatabaseName and RecordSource PropertiesYou must first set the following two properties when linking a data control to a database:
To set the DatabaseName of the data control, first select the data control by
single-clicking the control (the data control will already be selected if you did
not click anywhere else on the form after you double-clicked the data control in
the Toolbox). This forces the data control properties to appear in the Visual Basic
Properties dialog box. Locate the DatabaseName property (properties are listed in
either alphabetical or categorical order, depending upon the tab you select in the
Properties box), and click the property name. When you do this, three small dots
(. . .), the properties ellipsis button, appear to the right of the data entry box.
Clicking the ellipsis button brings up the Windows standard File | Open dialog box.
You should now be able to select the BOOKS5.MDB file from the list of available
database files (\\TYSDBVB5\SOURCE\DATA\BOOKS5.MDB). Your screen should look
something like the one in Figure 1.2.
Now that you know what database you will use, you must select the data table within
that database that you want to access by setting the RecordSource property of the
data control. You can do this by locating the RecordSource property in the Properties
window, single-clicking the property, and then single-clicking the small down arrow
to the right of the property input box. This brings up a list of all the tables in
the BOOKS5.MDB database, as shown in Figure 1.3. For the first database
program, you will use the Titles data table in the BOOKS5.MDB database. Setting the Caption and Name PropertiesYou need to set two other data control properties in the project. These two properties are not required, but setting them is a good programming practice because it improves the readability of the programming code. Here are the optional properties:
Setting the Name property of the data control sets the text that will be used by the Visual Basic programmer. This is never seen by the user, but you should set the Name to something similar to the Caption to make it easier to relate the two when working on your program. For your program, set the Caption property of the data control to Titles and the
Name property of the data control to datTitles. Now that you've added the Caption
property, use the mouse to stretch the data control so that you can see the complete
caption. Your form should look like the one in Figure 1.4.
Saving Your ProjectNow is a good time to save your work up to this point. To save this project, select File | Save Project from the main menu. When prompted for a filename for the form, enter DATCNTRL.FRM. You will then be prompted for a filename for the project. Enter DATCNTRL.VBP. It's always a good idea to save your work often.
Adding the Bound Input ControlsNow that you've successfully linked the form to a database with the data control and se- lected a data table to access, you are ready to add input controls to the form. Visual Basic 5 supplies you with input controls that can be directly bound (connected) to the data table you want to access. All you need to do is place several input controls on the form and assign them to an existing data control.
Let's add the first bound input control to the Titles table input form. Place an input control on the form by double-clicking the textbox control in the Visual Basic 5 toolbox. This inserts a textbox control directly in the center of the form. When the control is on the form, you can use the mouse to move and resize it in any way you choose. You could add additional input controls by double-clicking the textbox button in the toolbox as many times as you like. Set the Name property of this control to txtTitle. Add a label to describe this control by double-clicking the Label control. Set the label's Name property to lblTitle, and the Caption property to Title. Refer to Figure 1.1 if you have any problems finding a particular Visual Basic control.
Setting the DataSource and DataField PropertiesYou must set two textbox properties in order for the textbox control to interact with the data control. These are the two required properties:
Setting the DataSource property of the textbox control binds the input control
to the data control. To set the textbox DataSource property, first select the textbox
control (click it once), and then click the DataSource property in the Property window.
By clicking this property's down arrow, you can see a list of all the data controls
currently active on this form. You have only added one data control to this form,
so you see only one name in the list (see Figure 1.5). Set the DataSource value to
datTitles by clicking the word datTitles in the drop-down list box. While you're at it, add Label controls to the left of the textbox controls and
set their Caption properties to the values shown in Table 1.2. Size and align the
controls on the form, too. Also, size the form by selecting its borders and dragging
to a desired shape. Your form should look something like the one in Figure 1.7 when
you're done.
Table 1.2. The Label Control Caption properties for the Titles form.
Figure 1.7. The completed data entry form for Titles. You can now run the program and see the data control in action. Select Run | Start (or press F5) to compile and run your program. You can walk through the data table by clicking the left and right arrows on the data control at the bottom of the form. The left-most arrow (the one with the bar on it) moves you to the first record in the data table. The right-most arrow (which also has a bar) moves you to the last record in the data table. The other two arrows simply move you through the data table one record at a time. You can make any changes permanent to the data table by moving to a different record in the table. Try this by changing the data in the Title input control, moving the record pointer to the next record, and then moving the pointer back to the record you just edited. You will see that the new value was saved to the data table. Now let's include the capability to add new records to the data table and to delete existing records from the data table. Adding the New and Delete Command ButtonsUp to this point, you have not written a single line of Visual Basic code. However, in order to add the capability to insert new records and delete existing records, you have to write a grand total of two lines of Visual Basic code: one line for the add record function, and one line for the delete record function. The first step in the process is to add two command buttons labeled Add and Delete
to the form. Refer to Table 1.3 and Figure 1.8 for details on adding the command
buttons to your form.
Private Sub cmdAdd_Click() datTitles.Recordset.AddNew ` add a new record to the table End Sub
Now open the code window behind the Delete button and add this Visual Basic code: Private Sub cmdDelete_Click() datTitles.Recordset.Delete ` delete the current record End Sub Runtime and Design Time PropertiesRecordSet is a runtime only property of the data control. This property is a reference
to the underlying data table defined in the design time RecordSource property. The
RecordSet can refer to an existing table in the database or a virtual table, such
as a Visual Basic Dynaset or Snapshot. This is covered in more depth on Day 3, "Visual
Basic Database Objects." For now, think of the RecordSet property as a runtime
version of the RecordSource property you set when you designed the form. Save the project and run the program again. You can now click the Add button and see a blank set of input controls for data entry. Fill them all with some data (refer to Figure 1.9 for an example of a new record), and then move to another record in the table. The data is automatically saved to the data table. You can also use the Delete button to remove any record from the table. First, find the record you just added (it's the last record in the table), and then click the Delete button. Now move to the previous record in the table and try to move forward again to view the record you just deleted. You can't. It's not there!
SummaryIn today's lesson you learned the following:
Quiz
Exercises
|
![]() |
|
Use of this site is subject certain Terms & Conditions. Copyright (c) 1996-1999 EarthWeb, Inc.. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Please read our privacy policy for details. |