Solved

Set Combo Box Record Source

Posted on 2002-07-05
14
169 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 52

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 52

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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 52

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA filters 2 81
Crystal reports - Formula Field code need assistance with code 17 102
Visual Studio search word table and return Cell index 8 86
2 Global Vars, 1 List Box 4 34
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…
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…
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…

752 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