Solved

combo box question

Posted on 2015-01-07
12
219 Views
Last Modified: 2015-01-07
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

Open in new window



Thanks!
0
Comment
Question by:terpsichore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +3
12 Comments
 
LVL 85
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

by:terpsichore
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

by:Rey Obrero (Capricorn1)
ID: 40535469
@terpsichore
you need to revised the rowsource of the combo box to have a unique field as the bound column.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 51

Expert Comment

by:Gustav Brock
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

Open in new window

/gustav
0
 

Author Comment

by:terpsichore
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

by:Rey Obrero (Capricorn1)
ID: 40535652
where is the rowsource coming from?
0
 
LVL 51

Expert Comment

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

Strange. Tested and worked here.

/gustav
0
 

Author Comment

by:terpsichore
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

by:
Jeffrey Coachman earned 250 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 38

Assisted Solution

by:PatHartman
PatHartman earned 250 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

by:terpsichore
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

by:Jeffrey Coachman
ID: 40536052
;-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question