Partager via


Visual Basic Concepts

Create a Simple Data-Bound Form

Once you've created a Command object in a Data Environment designer to serve as a data source, you can easily create a data-bound form by dragging the Command object onto a blank form. You can then add a mechanism for navigating through the records that are displayed.

In this topic, you'll create a form that displays records from the Customers table in the Northwind Traders sample database. Then you'll create command buttons for moving to the next or previous record.

Note   This topic is part of a series that walks you through creating a simple database application that interacts with data in Nwind.mdb. It begins with the topic, Interacting with Data in a Microsoft Jet/Microsoft Access Database.

To create a simple data-bound form

  1. Drag a Command object from the Data Environment designer to a blank form.

  2. Create command buttons that let you navigate through records.

Drag a Command Object from the Data Environment Designer to a Blank Form

Much of the tedium of creating a data-bound form can be avoided in Visual Basic by dragging a Command object from the Data Environment designer to the form. Visual Basic automatically creates text box controls to display data from the Command object's recordset and sets data properties that bind the controls to fields in the recordset.

For example, to create a form that displays data from the Customers table in the Northwind Traders sample database, follow the steps in the "Create a Data Environment Command Object" topic to create a CustomersTable command. Then simply drag the Command object from the Data Environment designer onto a blank form. To view both the form and data environment simultaneously so you can perform the drag operation, select Tile Horizontally, Tile Vertically, or Cascade from the Window menu.

Create Command Buttons to Navigate through Records

By basing a data-bound form on a data environment Command object's recordset, 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:

MyDataEnvironment.rsCustomersTable.MoveNext

The code uses the MoveNext method of the CustomersTable command's underlying recordset, rsCustomersTable. It refers to the recordset as a property of the command's data environment designer, MyDataEnvironment.

Similarly, you can create a Previous button by adding a command button to the form and change its Caption and Name properties to Previous. Then add the following code to the command button's Previous_Click event procedure:

MyDataEnvironment.rsCustomersTable.MovePrevious

When you run the form, Visual Basic displays the records in the Customers table and lets you move forward and backward through the recordset.

Step by Step

This topic is part of a series that walks you through creating a simple database application that interacts with data in Nwind.mdb.

To See
Go to the next step Create a Data Grid Form Based on a Query
Start from the beginning Interacting with Data in a Microsoft Jet/Microsoft Access Database