Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access VBA: Find item in combo box based on non-bound column

I have a two-column combo box on an Access form representing a key-to-code mapping. The first column of the combo box is the 'bound column' (ie, the column used when MyComboBox.Value is called).

I need to dynamically set the Value of my combo box based on a value found in the second column. For eg, if my combo box source is:

Value | Code
===============
 A1    | ABCD
 A2    | EFGH
 A3    | IJKL

I can set the value of the combo box simply with ComboBox.Value = "A2", but how would I do the same using the second column? ComboBox.Value = "EFGH" obviously isn't valid. Essentially looking for logic along the lines of ComboBox.Value = ComboBox.ValueWhereSecondColumnEquals("EFGH")

like image 812
Kai Avatar asked Mar 12 '13 16:03

Kai


People also ask

How do you get a combo box to find a record in Access?

In the Controls group, click List Box or Combo Box. On the form, click where you want to put the list box or combo box. On the first page of the wizard, click Find a record on my form based on the value I selected in my combo box/list box, and then click Next.

What is bound column in combobox?

The BoundColumn property determines which column's value in the text box or combo box list will be stored when you make a selection. This allows you to display different data than you store as the value of the control.


2 Answers

And assuming it's not based on a table/query:

Dim i As Integer

For i = 0 To ComboBox.ListCount-1
    If ComboBox.Column(1, i) = "EFGH" Then
        ComboBox.Value = ComboBox.ItemData(i)
        Exit For
    End If
Next i
like image 64
RichardC Avatar answered Oct 16 '22 05:10

RichardC


Assuming that your combo is based on a table, you can DLookUp the value in the table:

 ComboBox.Value = Dlookup("Value","Table","Code='" & sCode & "'")
like image 25
Fionnuala Avatar answered Oct 16 '22 06:10

Fionnuala