Andre Vioti
Andre Vioti

Reputation: 27

Moving the cursor to another textbox after Enter Event - VBA

Could you please help me?

With the code below, I can "Setfocus" and change the "Backcolor" of "Me.txtFT".

But, when I am trying to move the cursor to "Me.txtFT" after an "Enter" event on "txtPT", the cursor is not moving, it keeps on the "txtPT".

I am using the code below.

Private Sub txtPT_Enter()
    If Trim(Me.txtPT.Value & vbNullString) = 0 Then
        MsgBox """FT field"" must contain a value before continue. Please try again": _
        Me.txtFT.SetFocus: _
        Me.txtFT.BackColor = &H80FFFF: _
        Exit Sub
    End If
End Sub

Could you please help me?

Upvotes: 1

Views: 424

Answers (1)

Mathieu Guindon
Mathieu Guindon

Reputation: 71177

The condition is testing the length of txtPT but the message is warning about txtFT, so it's unclear from the code what the intent might be.

Instead of handling the entry into control B to validate the value of control A, consider handling the exit from control A - and cancel it losing the focus with an invalid value in the first place:

Private Sub txtFT_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    With txtFT
        If Len(Trim(.Text & vbNullString)) = 0 Then
            Cancel.Value = True
            .BackColor = &H80FFFF
            MsgBox "[FT] cannot be empty!"
        End If
    End With
End Sub

Upvotes: 1

Related Questions