Visual Basic Reference
Provides access to data stored in databases using any one of three types of Recordset objects. The Data control enables you to move from record to record and to display and manipulate data from the records in bound controls. Without a Data control or an equivalent data source control like the RemoteData control, data-aware (bound) controls on a form can't automatically access data.
You can perform most data access operations using the Data control without writing any code at all. Data-aware controls bound to a Data control automatically display data from one or more fields for the current record or, in some cases, for a set of records on either side of the current record. The Data control performs all operations on the current record.
If the Data control is instructed to move to a different record, all bound controls automatically pass any changes to the Data control to be saved in the database. The Data control then moves to the requested record and passes back data from the current record to the bound controls where it's displayed.
The Data control automatically handles a number of contingencies including empty recordsets, adding new records, editing and updating existing records, and handling some types of errors. However, in more sophisticated applications, you need to trap some error conditions that the Data control can't handle. For example, if the Microsoft Jet database engine has a problem accessing the database file, doesn't have permission, or can't execute the query as coded, a trappable error results. If the error occurs before your application procedures start or due to some internal errors, the Error event is triggered.
The DataList, DataCombo, DataGrid, and MSHFlexGrid controls are all capable of managing sets of records when bound to a Data control. All of these controls permit several records to be displayed or manipulated at once.
The intrinsic Picture, Label, TextBox, CheckBox, Image, OLE, ListBox and ComboBox controls are also data-aware and can be bound to a single field of a Recordset managed by the Data control. Additional data-aware controls like the MaskedEdit and RichTextBox controls are available in the Professional and Enterprise Editions and from third-party vendors.
Once the application begins, Visual Basic uses Data control properties to open the selected database, create a Database object and create a Recordset object. The Data control's Database and Recordset properties refer to the newly created Database and Recordset objects which may be manipulated independently of the Data control with or without bound controls. The Data control is initialized before the initial Form_Load event for the form on which it is placed. If any errors occur during this initialization step a non-trappable error results.
When Visual Basic uses the Jet database engine to create a Recordset, no other Visual Basic operations or events can occur until the operation is complete. However, other Windows-based applications are permitted to continue executing while the Recordset is being created. If the user presses CTRL+BREAK while the Jet engine is building a Recordset, the operation is terminated, a trappable error results, and the Recordset property of the Data control is set to Nothing. In design time, a second CTRL+BREAK causes Visual Basic to display the Debug window.
When you use a Data control to create a Recordset object or when you create a Recordset object in code and assign it to the Data control, the Microsoft Jet database engine automatically populates the Recordset object. As a result, bookmarks (and for snapshot-type Recordset objects, recordset data) are saved in local memory; the user doesn't need to manipulate the Data control, and you don't need to invoke the MoveLast method in code. Page locks used to create the Recordset are released more quickly, making it possible for other Recordset objects to access the same data. Recordset objects created in code but not assigned to the Data control aren't automatically populated by the Jet engine. Populate these objects through code. Because of the way that the Data control populates its Recordset in the background, an additional cloned Recordset might be created.
You can manipulate the Data control with the mouse, moving from record to record or to the beginning or end of the Recordset. The EOFAction and BOFAction properties determine what happens when the user moves to the beginning or end of a Recordset with the mouse. You can't set focus to the Data control.
Use the Validate event and the DataChanged property to perform last minute checks on the records being written to the database.
Data Access Objects
You can use the Database and Recordset data access objects created by the Data control in your procedures. The Database and Recordset objects each have properties and methods of their own, and you can write procedures that use these properties and methods to manipulate your data.
For example, the MoveNext method of a Recordset object moves the current record to the next record in the Recordset. To invoke this method, you could use this code:
The Data control is capable of accessing any of the three types of Jet engine Version 3.0 Recordset objects. If you don't select a recordset type, a dynaset-type Recordset is created.
In many cases, the default type and configuration of the Recordset object created is extremely inefficient. That is, you might not need an updatable, fully-scrollable, keyset-type cursor to access your data. For example, a read-only, forward-only, snapshot-type Recordset might be far faster to create than the default cursor. Be sure to choose the most efficient Type, Exclusive, Options and ReadOnly properties possible for your situation.
Note The constants used to request a specific Recordset type when using the Data control are different than the constants used to determine the type of Recordset created or to create a Recordset using the OpenRecordset method.
To select a specific type of Recordset, set the Data control's RecordsetType property to:
Important The Data control cannot be used to access Recordset objects created with the dbForwardOnly option bit set.
Professional and Enterprise Editions
As far as data access is concerned, the primary difference between the Learning Edition, Professional and Enterprise Editions of Visual Basic is the ability to create new data access objects. In the Learning Edition, you can't declare (with the Dim keyword) variables as data access objects in code. This means that only the Data control can create Database and Recordset objects.
In the Professional and Enterprise Editions, you can create a new Recordset object and assign it to the Data control's Recordset property. Any bound controls connected to the Data control permit manipulation of the records in the Recordset you created. Make sure that your bound controls' DataField properties are set to field names that are valid in the new Recordset.
Another important option when using the Data control is the ability to execute stored queries. If you create a QueryDef object beforehand, the Data control can execute it and create a Recordset using the QueryDef object's stored SQL, Connect and other properties. To execute a QueryDef, set the Data control's RecordSource property to the QueryDef name and use the Refresh method.
If the stored QueryDef contains parameters, you need to create the Recordset and pass it to the Data control.
The Data control can also manage what happens when you encounter a Recordset with no records. By changing the EOFAction property, you can program the Data control to enter AddNew mode automatically.
You can program the Data control to automatically snap to the top or bottom of its parent form by using the Align property. In either case, the Data control is resized horizontally to fill the width of its parent form whenever the parent form is resized. This property allows a Data control to be placed on an MDI form without requiring an enclosing Picture control.
Using With Access 2000 Databases
The Data control can also be used to connect to Access 2000 databases. For details, see Intrinsic Data Control Is Usable with Access 2000 Databases.