Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms Programming / December 2005

Tip: Looking for answers? Try searching our database.

Deselecting items in a ListBox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martin Dashper - 05 Dec 2005 16:05 GMT
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
Klatuu - 05 Dec 2005 16:31 GMT
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

> 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
[quoted text clipped - 8 lines]
>
> Martin Dashper
Martin Dashper - 06 Dec 2005 11:11 GMT
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

>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:
[quoted text clipped - 18 lines]
>>
>> Martin Dashper
Klatuu - 06 Dec 2005 13:22 GMT
I don't think you can.

> 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
[quoted text clipped - 24 lines]
> >>
> >> Martin Dashper
Douglas J Steele - 06 Dec 2005 14:10 GMT
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

Signature

Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

> 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
[quoted text clipped - 24 lines]
> >>
> >> Martin Dashper
Martin Dashper - 13 Dec 2005 09:31 GMT
Many thanks Doug, it works a treat.

Martin

>To reset a single-select list box, set its value to Null.
>
[quoted text clipped - 62 lines]
>> >>
>> >> Martin Dashper
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.