Solved

Set Combo Box Record Source

Posted on 2002-07-05
14
158 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 49

Accepted Solution

by:
Ryan Chong earned 100 total points
Comment Utility
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
Comment Utility
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 49

Expert Comment

by:Ryan Chong
Comment Utility
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
 

Author Comment

by:beni_luedi
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:beni_luedi
Comment Utility
Where are these properties?
0
 

Author Comment

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

Expert Comment

by:TimCottee
Comment Utility
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
Comment Utility
(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
Comment Utility
Hi TimCottee,

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

Expert Comment

by:Ryan Chong
Comment Utility
Any reason why "C"!? beni_luedi?
0
 
LVL 6

Expert Comment

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

** Mindphaser - Community Support Moderator **
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 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

10 Experts available now in Live!

Get 1:1 Help Now