Set Focus TextBox1 doesn't happen

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,213
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
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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.
 
Upvote 0
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
 
Upvote 0
In the Else block, add:

Code:
KeyCode = 0
 
Upvote 0
Solution

Forum statistics

Threads
1,214,376
Messages
6,119,181
Members
448,871
Latest member
hengshankouniuniu

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
Back
Top