Merge two columns for display in a ComboBox

AID: 5849
  • Status: Published

6640 points

  • Byharfang
  • TypeTips/Tricks
  • Posted on2011-05-28 at 21:12:33
Awards
  • Experts Exchange Approved
I've known this trick for so long that I can't give proper credit any more. It might have been in the very first “Northwind Traders” database I studied, shipped with Access 2.0. Anyway, the idea is to display one thing in the edit portion of a combo box, and another in the drop-down section. Like this:
TwoColumnCombo.png
  • 12 KB
  • two column combo with concatenated columns
two column combo with concatenated columns

The selected supplier is displayed as “Exotic Liquids, London”, but the list shows the same in two columns. This makes the list much more readable, and reveals the fact that the sort order is in fact by city and not by supplier name. This is incidentally a second trick, allowing the user to search the list visually by city, while still being able to select a supplier by name using “auto-expand”. Typing “sp” will select the “Specialty Biscuits” even if the list isn't sorted.

A demo database is probably superfluous, but here is one anyway.
ComboMergeCols.mdb
  • 272 KB
  • demo database, Access 2000 format
ComboMergeCols.mdb




How it's done



The combo box contains in fact four columns. The first is the key column with the supplier number, which will be stored as the value of the combo and perhaps in an underlying field; it is hidden entirely. The second contains the combined supplier name and city, and is almost hidden, in that it doesn't show in the drop-down list. The last two are the same fields in separate columns.

To almost hide a column, the width is set to the minimal accepted value, which should be below one pixel. Internally, all form metrics are calculated in twips, measuring 1/1440 inches, which is thus the minimum. In recent versions of Access, one can type directly “1 twip”; in older versions the measure is given in the system units of measure: 0.0007" or 0.0018 cm.

The property sheet contains:
Row Source:         SELECT
                      SupplierID,
                      CompanyName & ', '+City,
                      CompanyName,
                      City
                    FROM Suppliers
                    ORDER BY City;
Bound Column:       1
Column Count:       4
Column Widths:      0";0.0007";;1"
List Rows:          16
List Width:         Auto
Auto Expand:        Yes
Allow AutoCorrect:  No
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:

Select allOpen in new window


Notes: One column width can be left empty, it will take the remaining space. Auto expand is still useful because the displayed column and the first really visible column start with the same words. AutoCorrect should always be off in combos.



Alternatives



When it doesn't make sense to concatenate the columns, the value from any column can be displayed next to the combo box in a text box.
ComboColumn.png
  • 6 KB
  • combo with external display of a column
combo with external display of a column

The query has been reduced to three columns, and the property sheet has been modified like this:
Column Count:       3
Column Widths:      0";2"
List Rows:          4
List Width:         3"
                                    
1:
2:
3:
4:

Select allOpen in new window


The text box has the control source: =cboSupplier.Column(2)

In this case, the result is clearly inferior for my taste. However, there are many other situations where this would be best. Also, the same trick can be used to display columns that are present in the row source but not even displayed as column in the drop-down section. Such columns can serve as a good substitute for numerous calls to DLookup() to obtain the same information.



Conclusion



The trick is simple yet very useful. Discovering it also gave me a better understanding of what “the first visible column” really means: almost hidden is still visible. The idea to sort on the second column rather than on the first one came later; it doesn't often make sense but when it does it's a real time saver.


Markus G Fischer
(°v°)

¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
This article is part of the EE-book unrestricted Access.
    Asked On
    2011-05-28 at 21:12:33ID5849
    Tags

    Acces

    ,

    ComboBox

    ,

    harfang

    Topic

    Access Forms

    Views
    958

    Comments

    Add your Comment

    Please Sign up or Log in to comment on this article.

    Join Experts Exchange Today

    Gain Access to all our Tech Resources

    Get personalized answers

    Ask unlimited questions

    Access Proven Solutions

    Search 3.2 million solutions

    Read In-Depth How-To Guides

    1000+ articles, demos, & tips

    Watch Step by Step Tutorials

    Learn direct from top tech pros

    And Much More!

    Your complete tech resource

    See Plans and Pricing

    30-day free trial. Register in 60 seconds.

    Loading Advertisement...

    Top Access Forms Experts

    1. LSMConsulting

      8,400

      0 points yesterday

      Profile
      Rank: Savant
    2. DatabaseMX

      8,064

      0 points yesterday

      Profile
      Rank: Savant
    3. capricorn1

      5,300

      0 points yesterday

      Profile
      Rank: Savant
    4. peter57r

      4,800

      0 points yesterday

      Profile
      Rank: Savant
    5. mbizup

      4,664

      0 points yesterday

      Profile
      Rank: Genius
    6. Helen_Feddema

      4,300

      0 points yesterday

      Profile
      Rank: Genius
    7. eghtebas

      4,132

      0 points yesterday

      Profile
      Rank: Genius
    8. boag2000

      2,940

      0 points yesterday

      Profile
      Rank: Genius
    9. _agx_

      2,800

      0 points yesterday

      Profile
      Rank: Genius
    10. Bitsqueezer

      2,800

      0 points yesterday

      Profile
      Rank: Wizard
    11. bingie

      2,000

      0 points yesterday

      Profile
      Rank: Guru
    12. dqmq

      2,000

      0 points yesterday

      Profile
      Rank: Genius
    13. imnorie

      2,000

      0 points yesterday

      Profile
      Rank: Genius
    14. als315

      2,000

      0 points yesterday

      Profile
      Rank: Genius
    15. harfang

      1,680

      20 points yesterday

      Profile
      Rank: Genius
    16. hnasr

      1,500

      0 points yesterday

      Profile
      Rank: Genius
    17. etsherman

      1,500

      0 points yesterday

      Profile
      Rank: Sage
    18. jrogersok

      1,200

      0 points yesterday

      Profile
    19. ABeasley

      1,200

      0 points yesterday

      Profile
    20. eoinisme

      1,000

      0 points yesterday

      Profile
    21. sparab

      752

      0 points yesterday

      Profile
      Rank: Guru
    22. LPurvis

      720

      0 points yesterday

      Profile
      Rank: Genius
    23. khairil

      668

      0 points yesterday

      Profile
      Rank: Wizard
    24. Nick67

      668

      0 points yesterday

      Profile
      Rank: Sage
    25. sb9

      500

      0 points yesterday

      Profile
      Rank: Wizard

    Hall Of Fame