Got record using a value from a list box

  • Thread starter Thread starter MrPAUL
  • Start date Start date
M

MrPAUL

I'm currently designing a contacts database. I want to be
able to select a contact from a list box and have the
correct information appear. Each contact has a Contact ID
(primary key), but the value in the list box is the
contact name. How do I get the database to search for the
value and retrieve the correct record? Code for this would
be very helpful.
 
Mr Paul, If you are using a bound form, which in your example would be bound
to a table/query based on your Contacts table, then all you have to do is
add a combobox to your form and follow the wizard that launches when you
place it on the form. The 3rd option on the initial window states "Find a
record on my form based on the value I select in my combo box. Finish
through the wizard and your set. If the wizard isn't launching when you
place the combobox on your form make sure the little wand with the stars
button located in the toolbox toolbar is selected before placing the control
on your form. Hope this helps

If you have a combo on your form basically what you need to do is
double-click the combo while in design view to bring up the properties
window. Click the All tab and set the following:
Row Source Type: Table/Query
Row Source: Name of the query that has your PrimaryKey and the Name field
added to the control grid.
Column Count: 2
Column Widths: 0";1" (this will hide the PK field so that only the name is
displayed)
Bound Column: 1
Limit to List: Yes (Probably)

Now click the event tab of the properties window. Click the ellipsis next
to the After Update event. This will open the code window. Type in
something like this replacing the names here with the names of your
controls/fields.

Private Sub myCombo_AfterUpdate()
' Find the record that matches the control.
Dim rs As Object

Set rs = Me.Recordset.Clone
rs.FindFirst "[ID] = " & str(Me![myCombo])
Me.Bookmark = rs.Bookmark
End Sub

Hope this helps!
 
Back
Top