How do I return associated values from selected values in a DataCombo control?

Posted on 2006-05-31
Last Modified: 2010-04-30
I have a table with the following data:

desc           id       code
-----------  ------     -----
officer         3         off
manager     2         mgr
employee    1         emp

I have a datacombo control called dcStaff and the ListField property is set to desc.

How do I return the code associated to the desc the user selects?

Please show example...

Question by:seckel
    LVL 13

    Assisted Solution


    add a combobox to your testapp, and paste this code:

    Option Explicit

    'desc           id       code
    '-----------  ------     -----
    'officer         3         off
    'manager         2         mgr
    'employee        1         emp
    Private Type DescCode
       Desc As String
       code As String * 3
    End Type

    Dim d(1 To 3) As DescCode

    Private Sub Combo1_Click()
    Dim n As Long
       For n = 1 To 3
          If d(n).Desc = Combo1.Text Then
             MsgBox "Code: " & d(n).code & vbCrLf & "id: " & n
          End If
    End Sub

    Private Sub Form_Load()
    d(3).Desc = "officer"
    d(2).Desc = "manager"
    d(1).Desc = "employee"
    d(3).code = "off"
    d(2).code = "mgr"
    d(1).code = "emp"
    Combo1.AddItem d(3).Desc
    Combo1.AddItem d(2).Desc
    Combo1.AddItem d(1).Desc
    End Sub

    Author Comment


    You have a good approach, but I'm looking for a way to bind a control to a database table.

    I would have to manually code for each record.  Ok, if I have a small table, like the example, but tedious if I want to use a table with hundreds of records.

    Do you have any other suggestions?
    LVL 13

    Expert Comment


    sorry i'm not into databases

    (i really need to learn that)

    however i can give you a link on database with combobox:
    LVL 6

    Expert Comment

    Just set

    DataCombo1.DataField = "code"
    LVL 27

    Accepted Solution

    rstStaff.Bookmark = dcStaff.SelectedItem
    Debug.Print rstStaff("id"),rstStaff("code")

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    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…

    755 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

    19 Experts available now in Live!

    Get 1:1 Help Now