12. Data Projects
12.1 Using the Data control
The Data control is part of the standard toolbox that Visual Basic installs. Other controls that are capable of being Bound to a
data source can be linked to the Data control.
The Data control stores a Recordset of data from the original source (e.g. an Access database). A Recordset is a sub-set of the
whole data store, which meets particular criteria. If, for example, you specified that you wanted to see all entries in a database
where the "name" field was "John", the result would be a Recordset of several entries, but all of them would be called "John". In
this way, we can search through large amounts of data for a particular entry.
Navigation through a given Recordset is then done with the transport buttons within the Data control itself.
The following exercise guides you through the construction of a simple data form. Initially, the Recordset contains all of the data
in the original source (an Access database).
Exercise xx:
You should be supplied with an Access database before beginning this exercise.
- Create a new "Standard EXE" project in Visual Basic. Add a Frame, a CommandButton and a Data control to the Form. Insert
Label controls into the Frame to match the following form:

Note:
Note that all of the Label controls within the Frame are standard Label controls that have had their "ForeColor" and/or
"BackColor" properties changed.
The CommandButton has had it's "Caption" property changed to "E&xit" and the Data control has had it's "Caption" property
changed to "Employees".
- Select the Data control and modify it's "DatabaseName" property to the path to the Access database you have been supplied
with (you can 'browse' to the file by clicking the little button (".") that appears hen you select this property).
- The "RecordSource" property of the Data control must then be set to control the source of the data within the database. We will
use an SQL query statement to determine the records that we want to use. Type the following SQL statement into the "RecordSource"
property:
SELECT * FROM Employees
- Each of the field Labels (the ones shown beginning with a '#' symbol) on the Frame must then be set up to use the Data
control as their source. The other Labels are static descriptions of the fields being viewed.
- For each of the Labels, change the "DataSource" property to the name of your Data control ("Data1") and the "DataField"
property to the field associated with the Label (see the description label beside each one). In both cases, Visual Basic will
suggest possible options in a drop-down style list when you select the property.
- Enter some code into the CommandButton's Click Event method to unload the form when the user clicks "Exit".
Run the project and verify that you can navigate through the "Employees" field using the Data Control's buttons.
How could you modify the project so that the user could search for a specific employee by name? Test your theory.
12.2 Data Environment and Data Reports
It is possible to construct print-outs from the Data control by writing loops in Visual Basic code and building up the image of each
sheet before sending it to the printer. An easier way of doing this is by using Data Environment and Data Reports.
The Data Environment acts as the Data source, like the Data control in the previous exercise. A Data Report can then be attached to
this Data Environment and used to preview and print the records required.
Exercise xxi:
- Re-Open the project in the previous exercise and add both a "Data Environment" and a "Data Report" from the "Project" menu.
- On the Data Environment we are going to create a connection to our database. Expand the "Connections" folder in the Data
Environment. Right-click on the default Connection called "Connection1" and select the "Properties." option on the pop-up menu.
- On the dialogue box, select "Microsoft Jet 3.51 OLE DB Provider" under the "Provider" tab, then click "Next".
- On the "Connection" tab, browse to the database or type the path into the box. Click the "Test Connection" button. If the
connection fails, repeat the last two steps to connect to the database.
- Click "OK" on the dialog box.
- Right-click on the "Commands" folder in the data Environment and select "Add Command" from the pop-up menu. This should create a
new Command called "Command1" inside the "Commands" folder.
- Right-click on "Command1" and select "Properties." from the pop-up menu. A dialog box should appear. On the "General" tab of the
dialog, change the value of the "Connection" to "Connection1".
- Change the value of "Database Object" to "Table" and "Object Name" to "Employees". Click "OK" to accept the command.
Note:
Notice that the Command now has sub-members, each one representing a field in the "Employees" table.
- Open the Data Report and change the "DataSource" property to "DataEnvironment1" and the "DataMember" property to "Command1",
to match those created in the Data Environment.
- Drag-and-drop the sub-members (i.e. the fields) of Command1 onto the "Detail" section of the Data Report where you want them to
appear. Give the Data Report a title caption using the RptLabel control from the toolbox.
- Save and close the Data Report and Data Environment.
- Open the form into the Object View window. Make space for and insert a new CommandButton. Label the CommandButton "&Report" and
edit the CommandButton's Event method to display the Data Report as follows:
DataReport1.Show
Test your application.
Note:
Note that the Data Report automatically caters for printing to the Windows System printer, making report generation much simpler.
<<Page 11 :
Top :
Page 13>>
|