PC Review


Reply
Thread Tools Rate Thread

Combobox display 1 value, access another

 
 
lcoreilly
Guest
Posts: n/a
 
      24th Mar 2010
Hi All,
I have a combobox that gets filled with the following code:

'load combobox with list of grantee institutions
With ComboBox1
For i = 5 To LastRow
.AddItem Worksheets("data").Cells(i, 2)
Next i
End With

End Sub

The drop down menu contains a list of schools, but the data element I
really need access to is the ID number, which is in column A. Is
there a way to access this value? Maybe using the offset function? I
can't seem to figure it out.

Thanks in advance.
 
Reply With Quote
 
 
 
 
B Lynn B
Guest
Posts: n/a
 
      24th Mar 2010
ComboBox lists can have multiple columns and you can arrange them however you
see fit, using the BoundColumn, ColumnCount, and ColumnWidth properties. In
this particular case if you set Bound Column to 2, ColumnCount to 2, and
ColumnWidth to something like "20 pt;0 pt", the control will display the list
of schools, but its value will equal the code stored from column A of the
sheet.

Adjust the "20 pt; 0 pt" to however wide you really want the first column to
be. Know that you can set the second column to show both the school and its
associated code side by side in the ComboBox dropdown list if that would be
helpful to your users. Also worth noting, the first item in the list is
index number 0, thus the "i - 5" bit of my added line of code.

With ComboBox1
For i = 5 To LastRow
.AddItem Worksheets("data").Cells(i, 2)
.List(i - 5, 2) = Cells(i, 1)
Next i
End With


"lcoreilly" wrote:

> Hi All,
> I have a combobox that gets filled with the following code:
>
> 'load combobox with list of grantee institutions
> With ComboBox1
> For i = 5 To LastRow
> .AddItem Worksheets("data").Cells(i, 2)
> Next i
> End With
>
> End Sub
>
> The drop down menu contains a list of schools, but the data element I
> really need access to is the ID number, which is in column A. Is
> there a way to access this value? Maybe using the offset function? I
> can't seem to figure it out.
>
> Thanks in advance.
> .
>

 
Reply With Quote
 
B Lynn B
Guest
Posts: n/a
 
      25th Mar 2010
Sorry, I just realized I should have included one more bit of info. To
return the alternate value, you'll need to call it specifically. i.e.:

myValue = ComboBox1.Column(2)

I was thinking the BoundColumn would take care of that, but in my test file,
something about that theory isn't working. So the line above is how to get
at it directly.

"lcoreilly" wrote:

> Hi All,
> I have a combobox that gets filled with the following code:
>
> 'load combobox with list of grantee institutions
> With ComboBox1
> For i = 5 To LastRow
> .AddItem Worksheets("data").Cells(i, 2)
> Next i
> End With
>
> End Sub
>
> The drop down menu contains a list of schools, but the data element I
> really need access to is the ID number, which is in column A. Is
> there a way to access this value? Maybe using the offset function? I
> can't seem to figure it out.
>
> Thanks in advance.
> .
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      25th Mar 2010
One more:

Option Explicit
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub CommandButton2_Click()
With Me.ComboBox1
If .ListIndex < 0 Then
'nothing selected
Beep
Exit Sub
End If

MsgBox .List(.ListIndex, 1)
End With
End Sub
Private Sub UserForm_Initialize()
Dim LastRow As Long
Dim iCtr As Long
Dim wks As Worksheet

Set wks = Worksheets("Data")

With wks
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With

With Me.ComboBox1
.ColumnCount = 2
.ColumnWidths = "55;0" 'hide the second column
For iCtr = 5 To LastRow
.AddItem wks.Cells(iCtr, "B").Value
.List(.ListCount - 1, 1) = wks.Cells(iCtr, "A").Value
Next iCtr
End With
End Sub


lcoreilly wrote:
>
> Hi All,
> I have a combobox that gets filled with the following code:
>
> 'load combobox with list of grantee institutions
> With ComboBox1
> For i = 5 To LastRow
> .AddItem Worksheets("data").Cells(i, 2)
> Next i
> End With
>
> End Sub
>
> The drop down menu contains a list of schools, but the data element I
> really need access to is the ID number, which is in column A. Is
> there a way to access this value? Maybe using the offset function? I
> can't seem to figure it out.
>
> Thanks in advance.


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Access Problem: Combobox.setfocus fires combobox.OnClick event Hal Levy Microsoft Access Form Coding 5 31st Jul 2009 03:54 AM
Access combobox does not display all decimal digits if end on zero Savvoulidis Iordanis Microsoft Access Form Coding 5 5th Sep 2008 07:13 AM
ComboBox display =?Utf-8?B?Umlja2V0eTEwNw==?= Microsoft Access Forms 1 4th Sep 2007 11:16 PM
Combobox Display/Value =?Utf-8?B?UGFydmVlbg==?= Microsoft VB .NET 1 21st Oct 2005 10:16 PM
Display a combobox depending on another combobox Veli Izzet Microsoft Access 2 11th Aug 2005 07:25 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:42 AM.