Condividi tramite


Visual Basic Concepts

Using the Data Control

The intrinsic Data control implements data access by using the Microsoft Jet Database engine –the same database engine that powers Microsoft Access. This technology gives you seamless access to many standard database formats and allows you to create data-aware applications without writing any code. The intrinsic Data control is best suited to smaller (desktop) databases, such as Access and ISAM databases.

You can use the intrinsic Data control to create applications that display, edit, and update information from many types of existing databases, including Microsoft Access, Btrieve, dBASE, Microsoft FoxPro®, and Paradox. You can also use it to access Microsoft Excel, Lotus 1-2-3, and standard ASCII text files as if they were true databases. In addition, the data control allows you to access and manipulate remote Open Database Connectivity (ODBC) databases such as Microsoft SQL Server and Oracle.

Note   Both the Data control and Remote Data control are included with Visual Basic for backward compatibility. However, because of the flexibility of ActiveX Data Objects (ADO), it's recommended that you create new database applications using the ADO Data control. For more details, see "Using the ADO Data Control."

The Data control, Remote Data control, and the ADO Data control are all conceptually similar: all three are "data controls" that connect a data source to a data-bound control. All three also share the same appearance — a set of four buttons that allow the user to go immediately to the beginning of the recordset, end of the recordset, and scroll backwards and forwards through the recordset.

Creating a Simple Database Application with the Data Control

To create a simple database application with the Data control

  1. Draw a Data control on the form. The Data control is an intrinsic control and is always available.

  2. Click the Data control to select it, and press F4 the display the Properties window.

  3. In the Properties window, set the Connect property to the type of database you want to use.

  4. In the Properties window, set the DatabaseName property to the file or directory name of the database you want to connect to.

  5. On the Properties window, set the RecordSource property to the name of the database table you want to access.

  6. Draw a TextBox control on the form.

  7. Click the TextBox control to select it, and on the Properties window set the DataSource property to the Data control.

  8. On the Properties window, set the DataField property to the name of the field in the database you want to view or modify.

  9. Repeat steps 6, 7, and 8 for each additional field you want to access.

  10. Press F5 to run the application.

The following data-related properties can be set at design time. The list suggests a logical order for setting the properties:

Note   Database technology is a complex science, and the suggestions below are not meant to be taken as rules.

  1. RecordsetType — The RecordsetType property determines if the recordset is a table, dynaset, or snapshot. The choice affects what recordset properties are available. For example, snapshot-type recordsets are more limited than dynaset recordsets.

  2. DefaultType — The DefaultType property specifies whether JET or ODBCDirect workspaces are used.

  3. DefaultCursorType — The DefaultCursorType property determines the location of the cursor. You can allow the ODBC driver to determine the cursor location, or specify server or ODBC cursors. The DefaultCursorType property is valid only when using ODBCDirect workspaces.

  4. Exclusive — Determines if the data is for a single- or multi-user environment.

  5. Options — The property determines the characteristics of the recordset. For example, in a multi-user environment, you can set the Options property to deny changes made by others.

  6. BOFAction, EOFAction — These two properties determine what will happen when the control is at the beginning and end of the cursor. Choices include staying at the beginning or end, moving to the first or last record, or adding a new record (at the end only).