Binding fields to a combo box

  • Thread starter Thread starter tyronne
  • Start date Start date
T

tyronne

I need urgent help to bind Company field and a City field to a Combo
box of Names,all this data comes from the same table, I just want if I
select a name, based on that selection it fills in the other 2 fields
automatically

Rgds

Tyronne
 
You will need to use a form and set the control source property of the two
fields on the form to something like this

=DLookUp("[CompanyField]","TableName","[NameField] = '" &
[Forms]![FormName]![ComboBoxName] & "'")

=DLookUp("[CityField]","TableName","[NameField] = '" &
[Forms]![FormName]![ComboBoxName] & "'")
 
Tyronne,

If the combo contains names, companies and cities, using a query such as
this:
SELECT [Name], Company, City
FROM tblMyTable

....then you can extract the company and city columns straight out of the
combo:
Private Sub cboMyCombo_AfterUpdate()
Me!txtCompany = Me!cboMyCombo.Column(1)
Me!txtCity = Me!cboMyCombo.Column(2)
End Sub

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
Tyronne,

I'm assuming the combo's RecordSource property is similar to the query I
showed.

The code I suggested, doesn't go in a macro, it goes in the combo's
AfterUpdate event. Open the form in design view, right-click the combo box
and select Properties from the context menu. Select the Events tab. Click in
the [After Update] property and you'll see two buttons appear at the extreme
right-hand-side. Click the rightmost button. If the [Choose Builder] dialog
is displayed, select [Code Builder], and click OK. Then enter the code
between the Private Sub... and End Sub lines.

Make sure to change the names of the fields and controls as necessary to
match the ones you actually have.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
Back
Top