Solved

Set Combo Box Record Source

Posted on 2002-07-05
14
165 Views
Last Modified: 2010-05-02
Right now I have this situation:

The Source for the form frmUser is tblUser.
One of the fields of this table is called UserContact (Long Integer). The txtContact textbox (Hidden) is bound to this field.

The cmbContact combobox is unbound and used to display the contacts (qryContactsCombo is the command object name).


The combo box gets it's values through this code:

' SET COMBO BOX
    Dim conn As New ADODB.Connection
    Dim RecSet As New ADODB.Recordset
    Dim sSQL As String
    conn.Open "DSN=Addressbook"
    RecSet.Open "SELECT * FROM tblContacts", conn, adOpenStatic, adLockReadOnly
    If RecSet.RecordCount <= 0 Then Exit Sub
    RecSet.MoveFirst
    Do Until RecSet.EOF
        cmbContact.AddItem RecSet.Fields("ConQuickname")
        cmbContact.ItemData(cmbContact.NewIndex) = RecSet.Fields("ConId")
        If RecSet.Fields("ConId") = txtContact Then
            cmbContact.ListIndex = cmbContact.NewIndex
        End If
        RecSet.MoveNext
    Loop
    Set RecSet = Nothing
    Set conn = Nothing

When I change the value in the combobox then the value from ItemData will be saved in the txtContact.

I am exploring for the first time the Data Environement control. I was hoping to be able to get the values for the combobox directly from the Data Environement control into the combobox. So I wouldn't need the code above. The command object looks like this right now:

SELECT ConQuickName, ConId
FROM tblContacts
ORDER BY ConQuickName

I need to display ConQuickName in the combobox.
The saved value should be ConId (right now in the ItemData property of the combobox or whereever it needs to be).

Is it possible to get this by setting the properties of the combobox without using the txtContact textbox?

Actually it is similar to access where you would use the RecordSource, RowSource and BoundColumn property. Can this be done?

Thanks ...

BL
0
Comment
Question by:beni_luedi
  • 7
  • 3
  • 2
  • +2
14 Comments
 
LVL 50

Accepted Solution

by:
Ryan Chong earned 100 total points
ID: 7131421
Not quite understand the situation..

You can get the conid by using cmbContact.ItemData(cmbContact.ListIndex)

What is the use of UserContact in txtcontact?

or can you explain more clearly on what you intend to do?

regards
0
 

Author Comment

by:beni_luedi
ID: 7131439
My question exactly ...

I try it another way:

I have a combobox that should display a contact name. The list of names is saved in the table tblContact (field: ConQuickName).

But the combobox is bound to the table tblUser (field: UserContact). This field is a Long Integer. This long integer value is saved in the tblContact (field: ConId).

How can I display the Contact's name in the combobox and at the same time save the Long integer as bound value?

If possible I don't want to use code to populate the list.

Thanks ...

BL
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 7131451
The simplest method to retrieve 2 (or more) tables fields/data is using the "Join" Statement in your SQL statement.

Example:

Select tblContacts.*, tblUser.UserContact FROM tblContacts Inner Join tblUser on tblContacts.userid = tblUser.userid

So, you can eventually populate the combobox from the fields above.

Get the idea?

regards
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:beni_luedi
ID: 7131466
In case you don't understand my second comment, then perhaps you can answer me those questions.

1. Can I populate a combobox by SQL statement or only by code like the one in my first comment with the AddItem method?

2. My combobox is bound to an Long Integer field in a database. Instead of displaying the Long Integer value can I display a string that is uniquely related to the Long Integer value?
0
 

Author Comment

by:beni_luedi
ID: 7131472
I know what you mean, but I don't know how to put it into my combo box.

Where is the value that gets saved in the bound field?
Where is the value that gets displayed in the combo box?

I mean which property do I have to set in the combobox?

Can I populate the list by SQL statement?

Thanks ...

BL
0
 

Author Comment

by:beni_luedi
ID: 7131480
I know what you mean, but I don't know how to put it into my combo box.

Where is the value that gets saved in the bound field?
Where is the value that gets displayed in the combo box?

I mean which property do I have to set in the combobox?

Can I populate the list by SQL statement?

Thanks ...

BL
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7131488
You use the .BoundColumn property, set the .BoundColumn equal to the fieldname of the long integer value and the .ListField property equal to the fieldname of the descriptive field. Then use the .BoundText property to return the value from the boundcolumn field when you select an item.
0
 

Author Comment

by:beni_luedi
ID: 7131510
Where are these properties?
0
 

Author Comment

by:beni_luedi
ID: 7131770
I have to use a DataCombo control not a ComboBox control.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7131796
I assumed that you were using the datacombo, this is far better for data-binding than the standard control, even though I don't like binding controls to recordsets in the first place, if you are going to do it then use the datacombo then you have the properties I mentioned. That explains why you couldn't find them in the first place.
0
 
LVL 3

Expert Comment

by:PNJ
ID: 7132418
(Don't forget that RecordCount may never return anything other than "-1" with certain drivers... so your code could always exit. In your original code it's safe to remove the line "If RecSet.RecordCount <= 0 Then Exit Sub" or change it to "If RecSet.EOF Then Exit Sub")
0
 

Author Comment

by:beni_luedi
ID: 7132427
Hi TimCottee,

What else would you do if not connect the control to the recordset? Is there a better way?
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 7137076
Any reason why "C"!? beni_luedi?
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7154419
I changed the grade since the asker didn't comment on the request for feedback.

** Mindphaser - Community Support Moderator **
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

831 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