Solved

Set Combo Box Record Source

Posted on 2002-07-05
14
167 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
  • +2
14 Comments
 
LVL 51

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 51

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 51

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

Technology Partners: 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!

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

735 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