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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

listbox arrow up and arrow down keys not working after on key down event 2

Status
Not open for further replies.

magicmandan

Programmer
Mar 9, 2010
13
Hi all,

I have an unbound single select listbox on an unbound form.

I have the following code on the On Key Down event of the list box so that users can remove an item from the listbox using the left arrow key on the keyboard.

After removing the selected item I want the user to have the ability to scroll through any other items in the list using the up and down keyboard keys and if desired remove another selected item.

The problem is that after an item is removed the up and down arrows on the keyboard will no longer scroll through the remaining items in the listbox unless i click on another control on the form and then click back on item in the listbox.

---KEYDOWN CODE----

Private Sub lbLM_KeyDown(KeyCode As Integer, Shift As Integer)

Dim strItemR As String
Dim strTXT As String
Dim I As Integer


If KeyCode = vbKeyLeft Then

If Me.lbLM.ListCount = 0 Then
Exit Sub
End If


strItemR = Me.lbLM.Column(0)

Me.lbLM.RemoveItem (strItemR)

If Me.frLM = 1 Then
strTXT = ""

For I = 0 To Me.lbLM.ListCount - 1
strTXT = strTXT & lbLM.Column(3, I) & ". "
Next I

Me.txtLM.value = strTXT

End If
Me.txtAllDL = allDislikes()

If Me.lbLM.ListCount = 0 Then
Else

Me.lbLM.Selected(0) = True
End If
End If

'MsgBox Screen.ActiveControl.Name


End Sub

---CODE END-----

I have found that if i uncomment the line: MsgBox Screen.ActiveControl.Name
then after a the msgbox is shown the up and down arrow keys work as they should.

Could anyone shed any light as to why the up down arrow keys do not work after remove item.

(I have a similar set up for populating the listbox above from another listbox (lbIngList) using the right arrow key and 'Me.lbLM.AddItem Item:=strItem' which works perfectly', i.e. the item is added and i can still scroll up and down lbIngList)


 
I think that you could add a requery on your combo box, and I suppose a refresh on the form after the removal is done, and perhaps that will do it. Or if you don't need a requery, probably as simple as changing the control focus to another control and back..

And if you need to end up setting it back to the last selected item in the list, then you could capture the current itemnumber in a variable, then tell the box to setfocus/select the same itemnumber to start the next go-round... of course it may not be as clear-cut simple as that.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Thanks very much for the response,

Unfortunately none of your suggestions have resolved the problem. I has already tried moving focus to another control and back again without success. I tried form refresh and listbox requery, but the only thing that works is displaying a msgbox which when closed gives the list box focus as intended and the arrow up and down keys function as they should. I shall have to rethink this functionality unless I can ge to the bottom of the problem.
 
Well, I'm sure another way will work - just have to find what it is.

Let me ask you this - your form, will it hurt if you re-open it? In other words, if you force the form to re-open altogether, will you lose any data in that process? If not, then you could do it this way:

1. User makes list change
2. DoCmd.OpenForm Form.Name
3. MyComboBox.SetFocus

...etc

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
I'd try something like this:
...
If KeyCode = vbKeyLeft Then
KeyCode = 0
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks very much for the replies,

Unfortunately I couldn't get this to work as desired so have abandoned this idea and gone back to just using a command button to remove the item from the list.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top