Unselecting Item In Listbox

  • Thread starter Thread starter DS
  • Start date Start date
D

DS

I'm using this code to unselect an item in a listbox.

Private Sub List34_Click()
Dim I As Integer
For I = 0 To Me.List20.ListCount - 1
Me.List20.Selected(I) = False
Next I
End Sub

This works fine if the Listbox is not locked. However, I have an Edit
button on the form which unlocks all of the fields, after your done the
fields become locked again. Thats when this code runs to unselect
whatever was selected in the listbox. If I keep the listbox unlocked
and enabled it works fine, if I disable and lock the listbox the code
doesn't work. Any help appreciated.
Thanks
DS





This says that you can't lock a field that has unsaved changes. Is a
unselect a change?

Private Sub List34_Click()
Me.List20.Enabled = True
Me.List20.Locked = False
Dim I As Integer
For I = 0 To Me.List20.ListCount - 1
Me.List20.Selected(I) = False
Next I
Me.List20.Enabled = False
Me.List20.Locked = True
End Sub
 
Why not have your code unlock the listbox (if required) and lock it again
(if it was locked)?

Private Sub List34_Click()
Dim booLocked As BooleanDim I As Integer

booLocked = Me.List20.Locked

If booLocked Then
Me.List20.Locked = False
End If

For I = 0 To Me.List20.ListCount - 1
Me.List20.Selected(I) = False
Next I


If booLocked Then
Me.List20.Locked = True
End If

End Sub

or, more simply,

Private Sub List34_Click()
Dim booLocked As BooleanDim I As Integer


booLocked = Me.List20.Locked
Me.List20.Locked = False

For I = 0 To Me.List20.ListCount - 1
Me.List20.Selected(I) = False
Next I


Me.List20.Locked = booLocked

End Sub
 
Douglas said:
Why not have your code unlock the listbox (if required) and lock it again
(if it was locked)?

Private Sub List34_Click()
Dim booLocked As BooleanDim I As Integer

booLocked = Me.List20.Locked

If booLocked Then
Me.List20.Locked = False
End If

For I = 0 To Me.List20.ListCount - 1
Me.List20.Selected(I) = False
Next I


If booLocked Then
Me.List20.Locked = True
End If

End Sub

or, more simply,

Private Sub List34_Click()
Dim booLocked As BooleanDim I As Integer


booLocked = Me.List20.Locked
Me.List20.Locked = False

For I = 0 To Me.List20.ListCount - 1
Me.List20.Selected(I) = False
Next I


Me.List20.Locked = booLocked

End Sub
Thanks Doug....That worked. Sometimes you can't see the forrest for the
trees!!! Break Time!
DS
 
Back
Top