Deselecting items in a ListBox

M

Martin Dashper

I have a ListBox in which I only want to be able to select 1 item. No
problem you say. Turn off Multiselect and away you go. The problem is
that, once an item has been selected, the only way to deselect it is
to select another item. Hence it is impossible to deselect all items
once one has been selected.
I had assumed that using the Ctrl key would do the job but no. It
works in a multiselect listbox (either simple or extended) but then,
of course, the user can select more than 1 item.

I am using Access 2002

Martin Dashper
 
G

Guest

Put a "Clear" command button on your form. Here is the code to put in the
Click event. Change the names to suit your form:

Dim ctl As Control

Set ctl = Me.lstBillProdOffering
For Each varItem In ctl.ItemsSelected
ctl.Selected(varItem) = False
Next
 
M

Martin Dashper

Yes, but how do I get the list to do that itself?
What I want is to be able to select an item and then, by clicking
(ideally a Ctrl-Click) on the same item, to deselect it.

Martin
 
G

Guest

I don't think you can.

Martin Dashper said:
Yes, but how do I get the list to do that itself?
What I want is to be able to select an item and then, by clicking
(ideally a Ctrl-Click) on the same item, to deselect it.

Martin
 
D

Douglas J Steele

To reset a single-select list box, set its value to Null.

To accomplish what you're trying to do, declare a couple of module-level
variables:

Dim mbooCtrl As Boolean
Dim mstrSelected As String

(this assumes that your listbox is bound to a text field. If it's bound to a
numeric field, use Dim mlngSelected As Long, or whatever else is
appropriate)

Now, add the following code:

Private Sub lstEmployee_Click()

' If this is the same record as previously selected AND
' the Ctrl key is depressed, clear the listbox
If Me.lstEmployee = mstrSelected And mbooCtrl Then
Me.lstEmployee = Null
Else
' Otherwise, just keep track of which record has been selected.
mstrSelected = Me.lstEmployee
End If

End Sub

Private Sub lstEmployee_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)

' Sets mbooCtrl to True if the Ctrl key is depressed
' along with the mouse click, False otherwise

mbooCtrl = (Shift And acCtrlMask)

End Sub
 
M

Martin Dashper

Many thanks Doug, it works a treat.

Martin


To reset a single-select list box, set its value to Null.

To accomplish what you're trying to do, declare a couple of module-level
variables:

Dim mbooCtrl As Boolean
Dim mstrSelected As String

(this assumes that your listbox is bound to a text field. If it's bound to a
numeric field, use Dim mlngSelected As Long, or whatever else is
appropriate)

Now, add the following code:

Private Sub lstEmployee_Click()

' If this is the same record as previously selected AND
' the Ctrl key is depressed, clear the listbox
If Me.lstEmployee = mstrSelected And mbooCtrl Then
Me.lstEmployee = Null
Else
' Otherwise, just keep track of which record has been selected.
mstrSelected = Me.lstEmployee
End If

End Sub

Private Sub lstEmployee_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)

' Sets mbooCtrl to True if the Ctrl key is depressed
' along with the mouse click, False otherwise

mbooCtrl = (Shift And acCtrlMask)

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