only allow one list box to have a selection

deb

Active Member
Joined
Feb 1, 2003
Messages
395
I have two listboxes, lboFY and lboCY.

If the use makes a single select or multiselect on lboFY, I want to clear all selections that may have been selected in lboCY
and the other way around
If the use makes a single select or multiselect on lboCY, I want to clear all selections that may have been selected in lboFY

Can't have both listboxes with selections. Needs to one or the other.
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,469
You would need these two scripts.
Change ListBox names as needed
Code:
Private Sub ListBox1_Click()
'Modified  7/25/2019  10:49:25 PM  EDT
ListBox2.ListIndex = -1
MsgBox ListBox1.Value
End Sub
Private Sub ListBox2_Click()
'Modified  7/25/2019  10:49:25 PM  EDT
ListBox1.ListIndex = -1
MsgBox ListBox2.Value
End Sub
 

stumac

Active Member
Joined
Jul 16, 2010
Messages
471
For a list box you would use something like:
Code:
Private Sub [COLOR=#333333]lboCY[/COLOR]_Click()
If Me.[COLOR=#333333]lboFY[/COLOR].MultiSelect = 0 Then
    Me.[COLOR=#333333]lboFY[/COLOR]= Null
Else
    For Each x In Me.[COLOR=#333333]lboFY[/COLOR].ItemsSelected
        Me.[COLOR=#333333]lboFY[/COLOR].Selected(x) = False
    Next x
End If
End Sub
and the same code on the alternative list box with the control names switched round.
 

deb

Active Member
Joined
Feb 1, 2003
Messages
395
I am getting error 7777
You've used the ListIndex property incorrectly

any suggestions?
 

deb

Active Member
Joined
Feb 1, 2003
Messages
395
Tried but this did nothing.
no errors and did not prevent selection of the other listbox
 

stumac

Active Member
Joined
Jul 16, 2010
Messages
471
Are you sure you have the correct control names?

I have quickly tested it using the exact names and it works no problem for me.
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
5,364
Office Version
2013
Platform
Windows
Hi,
try following

Code:
Private Sub lboCY_Enter()
    ClearSelections Me.lboFY
End Sub


Private Sub lboFY_Enter()
    ClearSelections Me.lboCY
End Sub


Private Sub ClearSelections(ByVal ListBox As Object)
    Dim SelectedItem As Integer
    For SelectedItem = 0 To ListBox.ListCount
        ListBox.Selected(SelectedItem) = False
    Next SelectedItem
End Sub
Dave
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,469
Are you refering to my posting post. If you Listbox is name ListBox1
I mentioned in my post you need to modify the listbox names. I tested my script and it worked for me. But it does not work on Multiselect listbox. Sorry

I am getting error 7777
You've used the ListIndex property incorrectly

any suggestions?
 
Last edited:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
33,909
Office Version
365
Platform
Windows
@MAIT
Not sure if you realised, but this is in the Access Forum
 

Forum statistics

Threads
1,085,951
Messages
5,386,903
Members
402,025
Latest member
saresum

Some videos you may like

This Week's Hot Topics

Top