Solved

combo box question

Posted on 2015-01-07
12
206 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
  • 4
  • 2
  • 2
  • +3
12 Comments
 
LVL 84
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 119

Expert Comment

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

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 119

Expert Comment

by:Rey Obrero
ID: 40535652
where is the rowsource coming from?
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 49

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 34

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now