Solved

# combo box question

Posted on 2015-01-07
Medium Priority
229 Views
Dear experts -
I have an unbound combo box on a form that is populated from a union query.
Each ROW is unique, but not the first field.
I am listing:
Account, Brand, Division - there are multiple brands in a given account (from different table), for example.
Thus, the data can look like this:
1,100,0
1,101,0
2,0,0
3,0,200
3,0,201

etc.  [each number is a foreign key from another table]

The question - I have code that can scan the combo box and come upon the right record, which i then want to be THE ONE SELECTED. But since there is no unique first field, I am not sure how to do this. I seem to have the 'row number' - can I highlight the right record based on that?

Note that the code IS hitting the right record (i.e., when i put a breakpoint on 'j=true' it stops at the right item in the combo box.

``````For i = 0 To Me.cboBrand_ID.ListCount - 1
If Val(Me.cboBrand_ID.Column(0, i)) = Nz(Me.txtAccount_ID, 0) And Val(Me.cboBrand_ID.Column(1, i)) = Nz(Me.txtBrand_ID, 0) And Val(Me.cboBrand_ID.Column(2, i)) = Nz(Me.txtAccountdivision_ID, 0) Then
j = True
Me.cboBrand_ID.Value = Me.cboBrand_ID.ItemData(i)  'DOES NOT WORK, SINCE itemdata is not unique!

Exit For
End If
Next i
``````

Thanks!
0
Question by:terpsichore
• 4
• 2
• 2
• +3

LVL 86

Expert Comment

ID: 40535310
You'd have to define the "right record". Which data point(s) in your combo's list give you the information you need to pinpoint the specific record you want?

You can get values from the individual columns of data like this (remembering they're zero-based):

Me.YourCombo.Column(1) << gets the value from the SECOND column
Me.YourCombo.Column(2) << gets the value from the THIRD column
0

Author Comment

ID: 40535315
it is the combination of the 3 data points (and yes, the column designation is pointing to the right column). thanks.
The above code seems to be correctly cycling through the items and finds the match which matches the 3 data elements.
0

LVL 120

Expert Comment

ID: 40535469
@terpsichore
you need to revised the rowsource of the combo box to have a unique field as the bound column.
0

LVL 53

Expert Comment

ID: 40535502
Try using the listindex:
``````For i = 0 To Me.cboBrand_ID.ListCount - 1
If Val(Me.cboBrand_ID.Column(0, i)) = Nz(Me.txtAccount_ID, 0) And Val(Me.cboBrand_ID.Column(1, i)) = Nz(Me.txtBrand_ID, 0) And Val(Me.cboBrand_ID.Column(2, i)) = Nz(Me.txtAccountdivision_ID, 0) Then
j = True
Me!cboBrand_ID.ListIndex = i
Exit For
End If
Next i
``````
/gustav
0

Author Comment

ID: 40535638
unfortunately that give me a runtime error 7777 'you've used the listindex property incorrectly'...
The issue with rey's comment is that there is not unique identifying field among the items being shown... it is the combination of them...
0

LVL 120

Expert Comment

ID: 40535652
where is the rowsource coming from?
0

LVL 53

Expert Comment

ID: 40535666
>  'you've used the listindex property incorrectly'

Strange. Tested and worked here.

/gustav
0

Author Comment

ID: 40535673
rowsource is a union query built from 3 separate tables - accounts + brands + divisions (corporate entities). i put the account_ID into the first (0) column, the brand into the second (1), and the division into the third (2).
0

LVL 74

Accepted Solution

Jeffrey Coachman earned 1000 total points
ID: 40535743
I would ask, why the need to use a Union Query for the row source...
I typically will only use a Union for a Combobox if I am adding a row like: "All" or "None"
it is the combination of the 3 data points
...Then it may be still be possible (however rare) to have all three datapoints being the same values for more than one record, ... correct
...Then what?

If not, then you could try concatenating all three fields into one "Composite" key (in a query made from your UNION query, ...)
Then make this new query the Rowsource, and set the bound column to be the composite key...
Something like this:
SELECT [fld1] & [fld2] & [fdl3] AS CompPK, YourUnionQuery.fld1,YourUnionQuery.fld2,YourUnionQuery.fld3
FROM YourUnionQuery;

Now you would have the same "Fields", ..but now you could set the composite key as the bound column, ...and the combobox selection should then work.

JeffCoachman
0

LVL 41

Assisted Solution

PatHartman earned 1000 total points
ID: 40535747
Combos do not work correctly unless there is a unique identifier.

I don't understand the purpose of this.  What is the bound field?  If you choose "row 2" and save the 1, how can you later distinguish it from "row 1"?

This looks like you should be using three separate, cascading combos instead of 1.

If you are going to stick with one, you can concatenate the three fields and use that as your unique identifier.  Use a separator or fix the width of each column.

Select Account & "-" & Brand & "-" & Division as UniqueID, Account, Brand, Division
From ....
Order By Account & "-" & Brand & "-" & Division ;
0

Author Closing Comment

ID: 40535907
This is the best solution for what I'm trying to do - and thanks everyone for the great input.
0

LVL 74

Expert Comment

ID: 40536052
;-)
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.