SET FOCUS to Original userform text field

chipsworld

Board Regular
Joined
May 23, 2019
Messages
161
Office Version
  1. 365
Hey everyone...need some guidance.
I am working on a simple userform to input data to a sheet, but am trying to eliminate duplicates...

The idea in the below code is that if the number already exists, the user gets a pop saying that the number has already been entered and then I want to set the focus back to the first text box (txtanum). Currently I can not stop it from tabbing to the second text box...how can I achieve this?

Here is the afterupdate code for the first test box on the userform. I have tried this in the "exit" field as well...always moves to the second text field....

VBA Code:
Private Sub txtanum_AfterUpdate()
Dim ws As Worksheet

Set ws = Sheets("dBase")

If Me.txtanum.Text > "" Then
    With ws
    Dim Rw As Long, Fnd As Range
            lkup = Me.txtanum.Value
        Set Fnd = .Range("A:A").Find(lkup, LookIn:=xlValues, LookAt:=xlPart, SearchDirection:=xlPrevious)
            If Not Fnd Is Nothing Then
        MsgBox "That 'A' number has already been entered into the system." & vbCrLf _
        & "Please use the 'Record Update' form to add or correct data.", vbOKOnly
        txtanum.Text = ""
        txtanum.SetFocus
        End If
    End With

   
End If

End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try exit event:

VBA Code:
Private Sub txtanum_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  Dim ws As Worksheet
  Dim Fnd As Range
  
  Set ws = Sheets("dBase")
  With Me.txtanum
    If .Value <> "" Then
      Set Fnd = ws.Range("A:A").Find(.Value, , xlValues, xlWhole, , , False)
      If Not Fnd Is Nothing Then
        MsgBox "That 'A' number has already been entered into the system." & vbCrLf _
          & "Please use the 'Record Update' form to add or correct data.", vbOKOnly
        .Value = ""
        Cancel = True
      End If
    End If
  End With
End Sub
 
Upvote 0
Solution
Try to use this => SendKeys "+{TAB}" instead => txtanum.SetFocus
 
Upvote 0
It turns out that control doesn't go to the next textbox (on Tab) until after the _AfterUpdate routine is finished, so any .SetFocus you do has no effect.

But I did the following, with a simple UserForm with two textboxes (1 and 2) and a module-level string variable that indicates which control should get focus (if the variable is "", then nothing unusual happens).

Module code:

VBA Code:
Option Explicit

Private NextControl As String

Private Sub TextBox1_AfterUpdate()
  NextControl = ""
  With Me.TextBox1
    If .Text = "go back" Then
      NextControl = "TextBox1"
      .Text = vbNullString
    End If
  End With
End Sub

Private Sub TextBox2_Enter()
  If Len(NextControl) Then
    Me.Controls(NextControl).SetFocus
  End If
End Sub
 
Upvote 0
Try exit event:

VBA Code:
Private Sub txtanum_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  Dim ws As Worksheet
  Dim Fnd As Range
 
  Set ws = Sheets("dBase")
  With Me.txtanum
    If .Value <> "" Then
      Set Fnd = ws.Range("A:A").Find(.Value, , xlValues, xlWhole, , , False)
      If Not Fnd Is Nothing Then
        MsgBox "That 'A' number has already been entered into the system." & vbCrLf _
          & "Please use the 'Record Update' form to add or correct data.", vbOKOnly
        .Value = ""
        Cancel = True
      End If
    End If
  End With
End Sub
Works perfectly! Thank you so very much. I just couldn't figure this one out for anything! LOL
 
Upvote 0
Im glad to help you, thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,799
Members
449,095
Latest member
m_smith_solihull

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