Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Set Combo Box Record Source

Posted on 2002-07-05
14
Medium Priority
?
173 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 54

Accepted Solution

by:
Ryan Chong earned 400 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 54

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 54

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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…
Suggested Courses

824 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