Searching the RecordSet
You might have wondered why I put the value of the field "ID" in the list box's ItemData property. I did this so that we would know the primary key for all the records in order to search for a record.
Put a text box somewhere on the form and call it "txtPhone". Then copy the following code to the project.Private Sub lstRecords_Click()rsMyRS.FindFirst "ID=" & Str(lstRecords.ItemData(lstRecords.ListIndex))txtPhone.Text = rsMyRS!PhoneEnd Sub
This will display the phone number of the selected person when clicking in the list box. It uses the FindFirst method of the RecordSet object. This takes a string parameter that is like what is after WHERE in a SQL expression. You state the field that you want to search in (here "ID"), then the evaluation criteria (here "=") and last the value to search for (here the ItemData of the selected item in the list box).
So what we did was to search for the record with the "ID" field value that was the same as the ItemData property of the selected item in the list box. Then we show the value of the "Phone" field in the text box.
No comments:
Post a Comment