Solved

combo box question

Posted on 2015-01-07
12
215 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 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
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.

 
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40535652
where is the rowsource coming from?
0
 
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 35

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

806 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