By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

multiselect listbox

P: n/a
hey... i have an unbound multiselect listbox on a form that i want to use to
populate text boxes on that form. so if a user selects the 3rd item in a
list of 20, how can i have that item show up in a text box? then, how can i
have a second selected item show up in a different text box, without
affecting the 1st one? (lboxOptions, txtboxOptions0, txtboxOptions1, etc..)

--
Greg

Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200705/1

May 11 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Fri, 11 May 2007 13:50:38 GMT, "kaosyeti via AccessMonster.com" <u15580@uwewrote:
>hey... i have an unbound multiselect listbox on a form that i want to use to
populate text boxes on that form. so if a user selects the 3rd item in a
list of 20, how can i have that item show up in a text box? then, how can i
have a second selected item show up in a different text box, without
affecting the 1st one? (lboxOptions, txtboxOptions0, txtboxOptions1, etc..)
A multiselect listbox exposes an ItemsSelected collection which you can use, but it's not ordered by the user's choice.
I'm not sure how you'd determine the "first" choice vs the "second" choice, unless you store that information in
variables/arrays etc ... you could perhaps trigger this off the Click event of the listbox ...

See this link: http://msdn2.microsoft.com/en-us/lib...ffice.10).aspx
Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
May 11 '07 #2

P: n/a
i have it close to working, but i've run into one problem. i have code in
the onclick and beforeupdate events to load the info into textboxes on the
same form. the problem is that i want to limit the listbox to 8 choices max,
and when the 9th is selected, a message box pops up to indicate that 8 is the
limit. the problem is, even though i'm trying to cancel at the beforeupdate
event, it still selects the 9th item. here's my code:

Private Sub lboxOptions_Click()
Dim frm As Form
Dim ctl As Control
Dim i As Long
Dim varItm As Variant
Dim strOptionCode As String

Dim currCount As Long

If booOpened Then
i = 0
numSelected = 0
booOpened = False
End If
Set frm = Forms!formNewVehicleFinder
Set ctl = frm!lboxOptions
If ctl.ItemsSelected.Count numSelected Then
For Each varItm In ctl.ItemsSelected
currCount = ctl.ItemsSelected.Count
strOptionCode = DLookup("[optioncode]", "tblNewOptions", "[option]
= " & Chr(34) & ctl.ItemData(varItm) & Chr(34))
Me.Controls("txtboxoption" & i).Value = strOptionCode
i = i + 1
Next varItm
numSelected = numSelected + 1
Else
numSelected = numSelected - 1
Me.Controls("txtboxoption" & numSelected).Value = ""
End If
End Sub

Private Sub lboxOptions_BeforeUpdate(Cancel As Integer)
If numSelected = 8 Then
Call MsgBox("Too many options were selected.", vbExclamation,
"Selection Error")
Cancel = -1
Me.lboxOptions.Undo
End If
End Sub

where booOpened is set to true in the form-open event and it, along with
numselected are set as public variables at the top of the code for the form.
what am i missing?

Scott McDaniel wrote:
>>hey... i have an unbound multiselect listbox on a form that i want to use to
populate text boxes on that form. so if a user selects the 3rd item in a
list of 20, how can i have that item show up in a text box? then, how can i
have a second selected item show up in a different text box, without
affecting the 1st one? (lboxOptions, txtboxOptions0, txtboxOptions1, etc..)

A multiselect listbox exposes an ItemsSelected collection which you can use, but it's not ordered by the user's choice.
I'm not sure how you'd determine the "first" choice vs the "second" choice, unless you store that information in
variables/arrays etc ... you could perhaps trigger this off the Click event of the listbox ...

See this link: http://msdn2.microsoft.com/en-us/lib...ffice.10).aspx

Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
--
Greg

Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200705/1

May 12 '07 #3

P: n/a
i got it working with the following in the beforeupdate event:

Dim n As Long
If numSelected = 8 Then
n = Me.lboxOptions.ListIndex
If Me.lboxOptions.Selected(n) = False Then
numSelected = numSelected - 1
Me.Controls("txtboxoption7").Value = ""
Cancel = True
Else
Cancel = True
Call MsgBox("Too many options were selected.", vbExclamation,
"Selection Error")
Me.lboxOptions.Selected(n) = False
End If
End If

but now i realize what you meant with regard to first choice, second choice.
since i was clicking down the list when i was testing this, i just was
clearing out the last textbox (numbered 0-7), rather than the correct textbox.
so if a user selects 6 items, then unselects the 2nd one, i'm dumping the 6th
one. how would you suggest hitting this with a variable/array solution
(keeping in mind i know nothing about arrays). thanks.

ka******@comcast.net wrote:
>i have it close to working, but i've run into one problem. i have code in
the onclick and beforeupdate events to load the info into textboxes on the
same form. the problem is that i want to limit the listbox to 8 choices max,
and when the 9th is selected, a message box pops up to indicate that 8 is the
limit. the problem is, even though i'm trying to cancel at the beforeupdate
event, it still selects the 9th item. here's my code:

Private Sub lboxOptions_Click()
Dim frm As Form
Dim ctl As Control
Dim i As Long
Dim varItm As Variant
Dim strOptionCode As String

Dim currCount As Long

If booOpened Then
i = 0
numSelected = 0
booOpened = False
End If
Set frm = Forms!formNewVehicleFinder
Set ctl = frm!lboxOptions
If ctl.ItemsSelected.Count numSelected Then
For Each varItm In ctl.ItemsSelected
currCount = ctl.ItemsSelected.Count
strOptionCode = DLookup("[optioncode]", "tblNewOptions", "[option]
= " & Chr(34) & ctl.ItemData(varItm) & Chr(34))
Me.Controls("txtboxoption" & i).Value = strOptionCode
i = i + 1
Next varItm
numSelected = numSelected + 1
Else
numSelected = numSelected - 1
Me.Controls("txtboxoption" & numSelected).Value = ""
End If
End Sub

Private Sub lboxOptions_BeforeUpdate(Cancel As Integer)
If numSelected = 8 Then
Call MsgBox("Too many options were selected.", vbExclamation,
"Selection Error")
Cancel = -1
Me.lboxOptions.Undo
End If
End Sub

where booOpened is set to true in the form-open event and it, along with
numselected are set as public variables at the top of the code for the form.
what am i missing?
>>>hey... i have an unbound multiselect listbox on a form that i want to use to
populate text boxes on that form. so if a user selects the 3rd item in a
[quoted text clipped - 11 lines]
>>scott@takemeout_infotrakker.com
www.infotrakker.com
--
Greg

Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200705/1

May 12 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.