473,409 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Using 2 Items for Combo Box? Displayed Choice and Underlying Value

Hi,

I am pulling data from a SQL table to build a combo box in Visual Basic. Is there a way to use 2 columns for the combo box: one that displays the choice and another to be the actual value?

I have two columns: "Description" and "ID", I want the users to be able to see the Description column as the choices, and the "ID" column will be underlying as the actual value.

Here is my code that only displays the "Description". Could someone help me add in "ID"? Thanks.


Private Sub LoadCombo()

Dim rsData As ADODB.Recordset
Dim strSQL As String

cboNYScenDesc.Clear

strSQL = "SELECT Description, ID " & _
"FROM Table1 " & _
"ORDER BY Description "
Set rsData = New ADODB.Recordset
rsData.Open strSQL, cnnDB, adOpenDynamic, adLockOptimistic

With rsData
If Not .BOF And Not .EOF Then
.MoveFirst
Do While Not .EOF
cboCombo.AddItem Trim(!Description)
.MoveNext
Loop
End If
.Close
End With
Set rsData = Nothing

End Sub
Nov 19 '07 #1
1 1343
lotus18
866 512MB
Hi,

I am pulling data from a SQL table to build a combo box in Visual Basic. Is there a way to use 2 columns for the combo box: one that displays the choice and another to be the actual value?

I have two columns: "Description" and "ID", I want the users to be able to see the Description column as the choices, and the "ID" column will be underlying as the actual value.

Here is my code that only displays the "Description". Could someone help me add in "ID"? Thanks.


Expand|Select|Wrap|Line Numbers
  1. Private Sub LoadCombo()
  2.  
  3.     Dim rsData As ADODB.Recordset
  4.     Dim strSQL As String
  5.  
  6.     cboNYScenDesc.Clear
  7.  
  8.     strSQL = "SELECT Description, ID " & _
  9.              "FROM Table1 " & _
  10.              "ORDER BY Description "
  11.     Set rsData = New ADODB.Recordset
  12.     rsData.Open strSQL, cnnDB, adOpenDynamic, adLockOptimistic
  13.  
  14.     With rsData
  15.             Do While Not .EOF
  16.                 cboCombo.AddItem Trim(!Description)
  17.                 .MoveNext
  18.             Loop
  19.         .Close
  20.     End With
  21.     Set rsData = Nothing
  22.  
  23. End Sub
  24.  
Nico please use the Code Tag I modified your codes above to make it simple ^

Is there a way to use 2 columns for the combo box: one that displays the choice and another to be the actual value?
I think you need to create your own user control. : )
Nov 20 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
0
by: mollyf | last post by:
I've got a combo box that I want to bind a collection to. I got the error "Complex Data Binding accepts as a data source either an IList or an IListSource" so I tried using the BindingSource,...
5
by: Kevin Wilcox | last post by:
In Access 2003 I have a continuous form with a combo. I'm trying to find a way to change the displayed values of the combo depending on whatever value was selected in the combo in the previous...
10
by: bettyboo | last post by:
Hi all I got some really useful advice from here with my last question so I'm back for more! Only my second post so please bear with me - I'm very new to this so this will probably not be as...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.