PreviousTutorials and Guide to Samples (9.1 revision 1) Next

Lesson 3: Writing Code with ActiveX Data Objects

Show this topic in Library frames

The ADO Data control provides an easy way to access data, but you can accomplish more if you write a bit of code, as well. For example, you can execute SQL queries and display the results in a bound DataGrid control.

This lesson uses the bound DataGrid control you created in Lesson 2, with a few changes, to display the results of a SQL statement. At the end of this lesson, you will be able to enter a capacity into a text field and find the rooms that hold that capacity or greater. Such a form might be handy for a meeting planner.

To execute a SQL statement through ADO:

  1. Click on the Text control in the Toolbox.

  2. Drag your cursor in the Form1 window to create a Text control.

  3. This creates a text entry box your user can use to provide input on the form.

  4. Click on the Label control in the Toolbox.

  5. Drag your cursor in the Form1 window to create a Label control. This creates a label for the text box.

  6. In the Caption field of the Properties window, type Enter Capacity:. This provides an instruction for the user.

  7. Now that the controls are in place, you can write the SQL statement that retrieves the records you want to see.

  8. Choose View > Code from the main menu. Visual Basic displays the Code window.

  9. In the leftmost list box, choose the Text1 object. Visual Basic creates a blank sub procedure called Text1_Change. Enter the following code to create the SQL statement.

  10. Click the Start button on the Toolbar. Visual Basic runs the application and displays the results.

  11. To test out your application, type a value in the text box. Note the results in the DBGrid.

  12. When you change the value in the text box, the Microsoft Jet engine parses the SQL statement and issues an ODBC request, which is passed to the Pervasive.SQL engine. Because the Microsoft Jet engine parses the SQL statement, your application's SQL statement must comply with Jet SQL syntax. For more information about this syntax, refer to the Visual Basic help.

  13. When finish testing the application, close the Form1 window and return to the form design view.

Chapter contents
Publication contents

Prev topic: Lesson 2: Using the ADO Data Control (ADODC)
Next topic: Lesson Summary