Enable TextBox when MultiSelect option selected

T

TJEngel

Newbie here!

So, I have been able to use the following code on a List Box in a form to
enable a text box when the List Box option "Other" is selected:

Private Sub FollowUp_AfterUpdate()
If Me!FollowUp = 5 Then
Me.Text53.Enabled = True
Else
Me.Text53.Enabled = False
Me.Text53.Value = ""
End If
End Sub

On this same form, I also have a MultiSelect List Box, also with an "Other"
option, and I would also like to enable/disable a text box if the user
selects this "Other" option. However, running "AfterUpdate" with similar
code doesn't seem to be working, and I would guess that it has something to
do with the fact that this is a MultiSelect List Box. Yes?

If so, is there code that will allow me to enable the text box when "Other"
is selected?
 
D

Douglas J. Steele

You cannot reference multiselect list boxes in that way. You need to loop
through the ItemsSelected collection, even if only one entry is selected:

Private Sub MyListbox_AfterUpdate()
Dim booOther As Boolean
Dim varSelected As Variant

booOther = False
If Me!MyListbox.ItemsSelected.Count > 0 Then
For Each varSelected In Me!MyListbox.ItemsSelected
If Me!MyListbox.ItemData(varSelected) = "Other" Then
booOther = True
Exit For
End If
Next varSelected
End If

If booOther Then
Me!SomeTextbox.Enabled = True
Else
Me!SomeTextbox.Enabled = False
Me!SomeTextbox.Value = ""
End If

End Sub
 
L

Linq Adams via AccessMonster.com

What

If Me!FollowUp = 5 Then

is actually sying is

If Me!FollowUp.Value = 5 Then

Because Value is the Default Property, it can be omitted in code.

When Multi-Select is chosen the ListBox no longer has a Value Property.

So you need to iterate thru all the items to see if your "Other" option has
been selected:

Private Sub List0_AfterUpdate()

Dim varItm As Variant
For Each varItm In List0.ItemsSelected
If List0.ItemData(varItm) = "Your Other Option" Then
'Do whatever here
End If
Next varItm

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top