Using ActiveX Data Objects
In this exercise, you will use Visual Basic and ADO to connect to a data source and create a recordset. A partial solution has been provided for you.
* To open the ADO Project
- Start Visual Basic 5.0 .
- From the New Project dialog, select the Existing tab.
- Locate WA\Practice\Ch07\Lab7ado.vbp and click Open .
- When prompted to add the project to Visual SourceSafe, click No .
- From the Project menu, click References .
Note that a reference has been made to the Microsoft OLE DB ActiveX Data Objects 1.0 Library .
* To connect to an ODBC data source
- In the General Declarations section of frmADO , create the following form-level object variable:
Dim rs As New ADODB.Recordset
- Open the code window for the cmdConnect_Click event.
- Add the following ADO code to open a recordset:
rs.Open "SELECT LastName, FirstName FROM Employees", _ "DSN=nwind;", adOpenKeyset
* To fill the textboxes with data from the employees table - From the object list box on the code window choose (General).
- From the procedure list box on the code window choose FillTextBoxes .
- In this procedure, you will write the code to fill the text boxes as in the example below.
txtFirstName.Text = rs!firstname txtLastname.Text = rs!lastname
* To examine the code in the rest of the application - Review the code in the cmdFirst , cmdPrevious , cmdNext and cmdLast click events.
- Save your work.
- Start and test the application.