There are two ways to present data to the user. You can manually place the contents of the current record's fields in appropriate controls, such as a text box, or you can bind the controls to the Recordset object.

Referencing Fields in a Recordset

In order to show a record's value to the user, you must reference the fields of the recordset. The most efficient technique is to reference the field name directly. You can use the Fields collection, which is more explicit code, but it is less efficient. The following example code directly references the First_Name field of a recordset:

' most efficient
txtFirstName.Text = rsStudents!First_Name

' explicit, but less efficient
txtFirstName.Text = rsStudents.Fields("First_Name").Value
  


Note  Some databases such as Microsoft Access allow spaces in field names. Visual Basic supports this syntax, but square brackets must be placed around field names that contain a space.

Manually Populating Controls

After a recordset has been created, you can manually reference fields of the recordset in order to present its values to the user. For example, if the Visual Basic form you are using has two text boxes, one to display the student's first name and a second to display the student's last name, you can manually populate the text boxes. This process must occur each time the user navigates to a new record. The following example code populates the text boxes on a form:

Sub FillControls()
    txtFirstName.Text = rsStudents!First_Name
    txtLastName.Text = rsStudents!Last_Name
End Sub
  

Binding Controls to a Recordset

New to Visual Basic is the ability to bind controls to objects. This is similar to the technique that you use to bind controls to an ActiveX Data control. Using the Recordset object, you can bind any data-aware control to any field in a recordset. As the user navigates from record to record, the text box automatically shows the record's values.

The following example code binds a text box to a field of an existing Recordset object:

Set txtFirstName.DataSource = rsStudents
txtFirstName.DataField = "First_Name"
  
Set txtLastName.DataSource = rsStudents
txtLastName.DataField = "Last_Name"
  


Note  Use the binding technique as an alternative to writing a general procedure that manually populates controls on a form.