Share this Page URL

Chapter 5. Using Visual Basic to Automat... > Using an Object Model - Pg. 188

Using Visual Basic to Automate Your Database adoRst.ActiveConnection = CurrentProject.Connection adoRst.Open "Employees", , adOpenDynamic, adLockOptimistic lngValue = Me.txtEmpID.Value adoRst.Find ("EmployeeID = " & lngValue) Me.txtLastName.Value = adoRst("LastName") adoRst.Close Set adoRst = Nothing End Sub 188 The declarations include variables for the text boxes used in this routine and a variable ( lngValue ) for the employee ID we enter. The Find method uses the EmployeeID field and the number entered to find the appropriate record. The value for the LastName field for that record is then assigned to the Value property of the text box. Here, as we always should, the recordset is closed and the object is set to Nothing so that system resources are freed. The DAO and ADO examples in these two sections show some straightforward ways to use code to retrieve data from tables and add it to forms where you and other users will work with it. In Part II, "The Database Application," you'll see more examples of how to use code such as this with the forms and reports that we'll create. Quick Check Q. How do you refer to a control on a form? A. Depending on where you are creating the reference, you can use the Forms collection, the form's name, and then the name of the control, as in Forms!MyForm!MyTextBox. Q. What does the AllForms collection represent?