Solved

combo box question

Posted on 2015-01-07
12
213 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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 …

920 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

16 Experts available now in Live!

Get 1:1 Help Now