Cannot Activate ActiveX Textbox

Pyrgos

New Member
Joined
Feb 22, 2012
Messages
30
Hello there,

I have the following code on an ActiveX Listbox stating that if I press Shift+Tab, a Textbox will be activated. The trouble is, it won't activate, and I find it very weird. SetFocus is not possible to use. Here is the code:

Code:
Private Sub lstSearch_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Then
    txtSearch.Activate
    Call DoTheMagic(lstSearch.List(0))
ElseIf Shift = 1 And KeyCode = vbKeyTab Then
    lstSearch.Selected(0) = False
    txtSearch.Activate ‘IT DOES NOT ACTIVATE, IT ONLY DESELECTS THE LISTBOX
End If
End Sub

If I take the Tab key from the code and set the condition to activate the textbox in case of pressing the Shift key only, then txtSearch will be activated successfully:

Code:
Private Sub lstSearch_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Then
    txtSearch.Activate
    Call DoSteamMagic(lstSearch.List(0))
ElseIf Shift = 1 Then
    lstSearch.Selected(0) = False
    txtSearch.Activate ‘IT WILL ACTIVATE WITH SUCCESS
End If
End Sub

Setting the TAB key only instead of Shift+TAB has the same result.

Any ideas on how can I get this working?

Thank you in advance.
 

Some videos you may like

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Worf

Well-known Member
Joined
Oct 30, 2011
Messages
4,010
Hello

- Maybe it has to do with the fact that tab is used to cycle through the controls
- Excel 2007 won’t let me use the activate method with the text box
- The following code presents three ways to go from the list box to the text box: pressing return, shift + tab or shift + up arrow

Code:
' this code goes at the UserForm module


Private Sub lstSearch_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift%)
If KeyCode = vbKeyReturn Then
    txtSearch.SetFocus
    MsgBox "done with return key"
 ElseIf KeyCode = vbKeyUp And Shift = 1 Then
    txtSearch.SetFocus
    MsgBox "done with shift + up arrow key"
End If
End Sub


Private Sub UserForm_Initialize()
Me.lstSearch.List = Split("Grete/Ingrid/Joan/Lisa/Alberto", "/")
Me.lstSearch.TabIndex = 1
Me.txtSearch.TabIndex = 0
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,109,028
Messages
5,526,335
Members
409,696
Latest member
EERS

This Week's Hot Topics

Top