Visual Basic Concepts
Creating a Form that Lets You View and Update Data from a Data-Aware Class
Once you've created a class that can act as a data source, you can easily create applications that let you view and update records from the data source. You can use ActiveX Data Objects (ADO) and the BindingsCollection object to bind the data source to controls on the form, and add command buttons to navigate through records.
This topic shows how to create a form that lets you view and edit customer address records from the data-aware class you created in the previous topic.
To create a form that lets you view and update data from a data-aware class
Add text box and label controls to a form.
Add code to bind the text box controls to the data source.
Add command button controls to navigate through records.
****Note ****This topic is part of a series that walks you through creating a simple database application that interacts with data in a tab-delimited text file. It begins with the topic Interacting with Data in an ASCII Text File.
Add Text Box and Label Controls to a Form
The first step in viewing and updating data from a data-aware class is to create an interface for interacting with records from the data source. The easiest way to create an interface is to open a new Standard EXE project, then add TextBox and Label controls to a form.
For example, you can create an interface for viewing customer address information from the Customers.txt file. First, add a "Customer ID:" label to the form, then add a text box control next to the label and set its Name property to txtCustomerID. Repeat the same process for CompanyName, Address, City, Region, PostalCode, and Country controls.
Add Code to Bind the Text Box Controls to the Data Source
Using a data-aware class, ActiveX Data Objects (ADO), and the BindingCollection object, you can bind controls to a data source when the page loads. You can then edit the data and add code to navigate through the records. For example, you can use an instance of your data-aware class and the BindingCollection object to bind the text box controls on your form to fields from the Customers.txt file.
First, add a reference to the BindingCollection object's type library to your project. To add the reference, select References on the Project menu, then select Microsoft Data Binding Collection in the References dialog box.
Then declare variables for the data-aware class and a BindingCollection object in your form's Declarations section:
Private objDataSource As CustomerDataSource
Private colBind As BindingCollection
To bind the text box controls to fields from the Customers.txt file when the form loads, add code to the form's Load event procedure. An instance of the CustomerDataSource class reads records from the text file into an ADO recordset, and the BindingCollection object binds the text box controls to fields in the recordset:
Private Sub Form_Load()
Set objDataSource = New CustomerDataSource
Set colBind = New BindingCollection
Set colBind.DataSource = objDataSource
colBind.Add txtCustomerID, "Text", "CustomerID"
colBind.Add txtCompanyName, "Text", "CompanyName"
colBind.Add txtAddress, "Text", "Address"
colBind.Add txtCity, "Text", "City"
colBind.Add txtRegion, "Text", "Region"
colBind.Add txtPostalCode, "Text", "PostalCode"
colBind.Add txtCountry, "Text", "Country"
End Sub
Add Command Button Controls to Navigate through Records
By binding controls on your form to a public recordset in your data-aware class, you can easily create Next and Previous buttons that let you navigate through records. Each command button requires a single line of code.
For example, to create a Next button for the form that displays customer records, add a command button to the form and change its Caption and Name properties to Next. Then add the following line to the command button's Next_Click event procedure:
objDataSource.rsCustomers.MoveNext
The code uses the MoveNext method of the rsCustomers recordset that serves as the data source for the form's controls. It refers to the recordset as a property of the object variable that represents an instance of the CustomerDataSource class.
Similarly, you can create Previous, First, and Last buttons by adding command buttons to the form and changing their Caption and Name properties to Previous, First, and Last, respectively. Then add code to the Click event procedure for each command button that invokes the MovePrevious, MoveFirst, and MoveLast methods.
When you run the form, Visual Basic lets you view and update records from the Customers.txt file and lets you navigate through the recordset.
Step by Step
This topic is part of a series that walks you through using a data-aware class and ADO to create a simple database application that interacts with data in a tab-delimited text file.
To | See |
Go to the next step | Modifying the Form to Let You Add New Records |
Start from the beginning | Interacting with Data in an ASCII Text File |