Set Focus TextBox1 doesn't happen

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
3,904
Office Version
  1. 2007
Platform
  1. Windows
I am using the code shown below.

I type in TextBox1 & press return on the keyboard to start the search.
I see a Msgbox appear advising NO USER NAME FOUND
I then click ok BUT im now expecting to see the cursor flashing in TextBox1 but its not.

I get no error messages either.

Do you see a reason why.
Thanks

Rich (BB code):
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = vbKeyReturn Then
  Dim r As Range, f As Range, cell As String, added As Boolean
  Dim sh As Worksheet
  
  Set sh = Sheets("POSTAGE")
  sh.Select
  With ListBox1
    .Clear
    .ColumnCount = 4
    .ColumnWidths = "240;150;280;50"
    If TextBox1.Value = "" Then Exit Sub
    Set r = Range("I8", Range("I" & Rows.Count).End(xlUp))
    Set f = r.Find(TextBox1.Value, LookIn:=xlValues, LookAt:=xlPart)
    If Not f Is Nothing Then
      cell = f.Address
      Do
        added = False
        For i = 0 To .ListCount - 1
          Select Case StrComp(.List(i), f.Value, vbTextCompare)
            Case 0, 1
              .AddItem f.Value, i                 'Item
              .List(i, 3) = f.Row                 'Row Number
              .List(i, 2) = f.Offset(, -7).Value  'Customer
              .List(i, 1) = f.Offset(, -2).Text   'Date

              added = True
              Exit For
          End Select
        Next
        If added = False Then
              .AddItem f.Value                                 'Item
              .List(.ListCount - 1, 3) = f.Row                 'Row Number
              .List(.ListCount - 1, 2) = f.Offset(, -7).Value  'Customer
              .List(.ListCount - 1, 1) = f.Offset(, -2).Text   'Date
              
        End If
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
      TextBox1 = UCase(TextBox1)
      .TopIndex = 0
    Else
      MsgBox "NO USER NAME WAS FOUND USING THAT INFORMATION", vbCritical, "POSTAGE SHEET USER NAME SEARCH"
      TextBox1.Value = ""
      ListBox1.Clear
      TextBox1.SetFocus
      
    End If
  End With
  End If
End Sub
 

Some videos you may like

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,453
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
It does happen, but then the release of the return key triggers the exit of the textbox. You need to set the KeyCode to 0 in the code if you don't want to leave the control.
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
3,904
Office Version
  1. 2007
Platform
  1. Windows
I am sure you must be talking about the piece below but how / what do i need to change it to ?

Rich (BB code):
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = vbKeyReturn Then
  Dim r As Range, f As Range, cell As String, added As Boolean
  Dim sh As Worksheet
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,453
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
In the Else block, add:

Code:
KeyCode = 0
 
Solution

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
3,904
Office Version
  1. 2007
Platform
  1. Windows
Great and worked a treat.

Thanks
 

Watch MrExcel Video

Forum statistics

Threads
1,127,321
Messages
5,624,000
Members
416,004
Latest member
reitz1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top