Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deselecting in Listboxes 1

Status
Not open for further replies.

347pg

Technical User
Jan 22, 2009
31
0
0
US
I have a form which has multiple listboxes. Most of these (except one) are multi-select. The one I'm concerned about is a non-multi-select. It is an either-or box, i.e. you can choose this item or the other, but not both. I've set "MultiSelect" in properties to "None" which allows the either-or, but if the user decides they do not want to use this listbox after all and tries to deselect a previously selected option, it will not deselect. Is there a way to deselect options in a Listbox that has multi-select set to "None" without going to VBA, or setting multi-select to "Simple" or "Extended"?
Thanks for your help.
pw
 
347pg . . .

1st I posted the wrong code. It should be:
Code:
[blue]Private Sub [purple][B][I]YourListboxName[/I][/B][/purple]_AfterUpdate()
   Dim lbx As ListBox
   Static valLast As String
   
   Set lbx = Me.[purple][B][I]YourListboxName[/I][/B][/purple]
   
   If lbx.Column(0) = valLast Then
      lbx = Null
      valLast = ""
   Else
      valLast = lbx.Column(0)
   End If
   
   Set lbx = Nothing
   
End Sub[/blue]
Earlier you wanted to be able to de-select a [blue]single select[/blue] listbox. Thats all the code does. Click to select ... then click the same value again to de-select ... or select an alternate value.

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Aceman1
That works great! I applied it to both of my single selects. Now if the user makes a mistake, they don't have to use the 'Clear All' button. Thanks for all your help.
pw
 
AM1
I've changed my code to the following:


Private Sub cmdClearChoices_Click()

Dim varItm As Variant

With Me!lstTechType

For Each varItm In .ItemsSelected
.Selected(varItm) = False
Next varItm

End With


With Me!lstProjSize

For Each varItm In .ItemsSelected
.Selected(varItm) = False
Next varItm

End With

With Me!ProjList

For Each varItm In .ItemsSelected
.Selected(varItm) = False
Next varItm

End With

With Me!PhaseList

For Each varItm In .ItemsSelected
.Selected(varItm) = False
Next varItm

End With

With Me!SkillList

For Each varItm In .ItemsSelected
.Selected(varItm) = False
Next varItm

End With

With Me!TRDList

For Each varItm In .ItemsSelected
.Selected(varItm) = False
Next varItm

End With

Me!lstTailSiteSelect.Value = Null
'Me!lstTechType.Value = Null
Me!lstSimCom.Value = Null
'Me!lstProjSize.Value = Null
'Me!ProjList.Value = Null
'Me!PhaseList.Value = Null
'Me!SkillList.Value = Null
'Me!TRDList.Value = Null
Me!KeyWord1.Value = Null
Me!KeyWord2.Value = Null
Me!KeyWord3.Value = Null
Me!KeyWord4.Value = Null
Me!KeyWord5.Value = Null

End Sub

'==============================================================================================================
'This sub allows the Simple/Complex Listbox selection to be deselected without using the "Clear Choices" button
'==============================================================================================================

Private Sub lstSimCom_AfterUpdate()
Dim lbx As ListBox
Static valLast As String

Set lbx = Me.lstSimCom

If lbx.Column(0) = valLast Then
lbx = Null
valLast = ""
Else
valLast = lbx.Column(0)
End If

Set lbx = Nothing



End Sub

'===================================================================================================================
'This sub allows the Tail Site Selection Listbox selection to be deselected without using the "Clear Choices" button
'===================================================================================================================

Private Sub lstTailSiteSelect_AfterUpdate()
Dim lbx As ListBox
Static valLast As String

Set lbx = Me.lstTailSiteSelect

If lbx.Column(0) = valLast Then
lbx = Null
valLast = ""
Else
valLast = lbx.Column(0)
End If

Set lbx = Nothing


End Sub


The deslecting in the single select listboxes works great. However, I am noticing that when I use the 'clearchoices' button on my form, the selections in the single selects don't always clear. Sometimes they do, but sometimes not. Do I have a conflict somewhere or is something overriding my clearchoices sub routine?

Thanks
pw
 
347pg . . .

In the [blue]Tag[/blue] property of each [blue]textbox, combobox, listbox[/blue] enter a question mark ([purple]?[/purple]) ... ([red]no quotations please[/red]). Then in your [blue]cmdClearChoices[/blue] button, try the following:
Code:
[blue]   Dim ctl As Control
   
   For Each ctl In Me.Controls
      If ctl.Tag = "?" Then ctl = Null
   Next[/blue]
[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
dhookom, AM1
When I set the tag to ? in each control and pressed 'clearchoices', only the single select listboxes cleared out. I used a combination of Allen Browne's ClearList Function for the listboxes and

Me!KeyWord1.Value = Null
Me!KeyWord2.Value = Null
Me!KeyWord3.Value = Null
Me!KeyWord4.Value = Null
Me!KeyWord5.Value = Null

to clear out the text boxes and it seems to be working well.

I also had to put in these lines to use the function for each list box:

Call ClearList(Forms!frmViewProposalData!lstTechType)
Call ClearList(Forms!frmViewProposalData!lstTailSiteSelect)
Call ClearList(Forms!frmViewProposalData!lstSimCom)
Call ClearList(Forms!frmViewProposalData!lstProjSize)
Call ClearList(Forms!frmViewProposalData!ProjList)
Call ClearList(Forms!frmViewProposalData!PhaseList)
Call ClearList(Forms!frmViewProposalData!SkillList)
Call ClearList(Forms!frmViewProposalData!TRDList)

Is there a way to cycle through them on the whole for rather than listing each list box on a separate line?

Function code I used:

Function ClearList(lst As ListBox) As Boolean
Dim varItem As Variant

If lst.MultiSelect = 0 Then
lst = Null
Else
For Each varItem In lst.ItemsSelected
lst.Selected(varItem) = False
Next
End If

ClearList = True

Exit_ClearList:
Exit Function

End Function
 
I would set each of the listbox's tag property to
Tag: ClearLBO
Then use code like:
Code:
Dim ctl as Control
For Each ctl in Me.Controls
   If ctl.Tag = "ClearLBO" Then
      Call ClearList(ctl)
   End If
Next

Duane
Hook'D on Access
MS Access MVP
 
Hmmmm........
I used your code but left the "?" in the tags and changed the "ClearLBO" to "?" in the code. I got a mismatch error so I changed the tags and code to "ClearLBO" and now it works.
Do you know what the issue is with using question marks in the Tags?
this would be good to know for the future. Before Aceman1 mentioned it, I didn't know what the tags were for. Seems like using tags can simplify coding a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top