setting focus problems

Pookiemeister

Active Member
Joined
Nov 26, 2015
Messages
316
Office Version
  1. 365
  2. 2010
On my form I have six text boxes and a submit button and a cancel button. When the form initializes all text boxes and buttons are disabled except for the first text box and the cancel button. Once the user enters a value in the first textbox and the length is greater than 4 characters then the second textbox is enabled and so on. This will ensure that no textboxes get missed. Once the last textbox contains a set amount of characters the submit buttons is then enabled. The problem that I am having with the code below is that when the user re-enters the txtbxCnt textbox it will clear the contents as expected but jump down to the cancel button. I need the focus to stay inside txtbxCnt textbox. Inside the Private Sub txtbxCnt_Enter() I even tried setting the focus there but that didn't work either.

Code:
Private Sub txtbxCnt_Enter()

    If Len(Me.txtbxCnt.Value) > 0 And IsNumeric(Me.txtbxCnt.Value) Then
        Me.txtbxCnt.Value = Me.txtbxCnt.Value & "ct"
    Else
        Me.txtbxCnt.Value = ""
        Me.txtbxCnt.SetFocus
    End If

End Sub

VBA Code:
Private Sub txtbxCnt_Change()
    If Len(Me.txtbxCnt.Value) > 0 Then
        Me.txtbxMgAmt.Enabled = True
    Else
        Me.txtbxMgAmt.Enabled = False
    End If
End Sub

Private Sub txtbxCnt_Enter()
    If Len(Me.txtbxCnt.Value) > 0 And IsNumeric(Me.txtbxCnt.Value) Then
        Me.txtbxCnt.Value = Me.txtbxCnt.Value & "ct"
    Else
        Me.txtbxCnt.Value = ""
    End If
End Sub

Private Sub txtbxCnt_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(Me.txtbxCnt.Value) > 0 And IsNumeric(Me.txtbxCnt.Value) Then
        Me.txtbxCnt.Value = Me.txtbxCnt.Value & "ct"
    Else
        Me.txtbxCnt.Value = ""
    End If
End Sub

Thank You
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

chipsworld

Board Regular
Joined
May 23, 2019
Messages
120
Just a suggestion, but...

Why not just verify that all boxes are filled at the end when they hit the submit button. Would probably be easier to code and track. if you do it that way, you don't need to enable along the way.

I would probably use the AfterUpdate event instead of Change (Never can get that to work right for me.) Just use your IF statements to verify that everything is as it shoul dbe before allowing the user to proceed.

In the Afterupdate, you can verify everything is as you need it to be and then either pass along to the next box or force the user to stay where they are.

Another idea you could try...you could use the mousedown event to clear the textbox. That would allow the focus to stay in that textbox...

Just an idea from a semi-beginner...
 

Watch MrExcel Video

Forum statistics

Threads
1,130,154
Messages
5,640,447
Members
417,143
Latest member
boukadidanizar

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