×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Focus Remain in Textbox during loop (VBA6)

Focus Remain in Textbox during loop (VBA6)

Focus Remain in Textbox during loop (VBA6)

(OP)
Imagine you have an excel spreadsheet with data in a column. You want to loop through each cell in the column and give the user an option to keep what is in the cell or change it.

I made a simple userform (textbox and a button) that simulates looping through data. Each time new data is automatically entered in the textbox, I would like it to be highlighted automatically...because it is 99% certain the user will want to enter something different.

When they pick the button, what ever is in the textbox will be entered in the cell and the code moves on to the next cell, adds the next cell's value to the text box, repeat

The problem is I can only get the text to highlight on the first iteration.

CODE --> vba

Private iCounter As Integer

Private Sub UserForm_Initialize()
    iCounter = 1
    TextBox1.Text = CStr(iCounter)
End Sub

Private Sub TextBox1_Enter()
    'This works only the first time
    TextBox1.SetFocus
    TextBox1.SelStart = 0
    TextBox1.SelLength = Len(TextBox1.Text)
End Sub

Private Sub CommandButton1_Click()
    If iCounter < 3 Then
        iCounter = iCounter + 1
    Else
        Unload Me
    End If
    TextBox1.Text = CStr(iCounter)
End Sub 

I have read about retaining focus using the BeforeUpdate event on the text box but I cannot get it to work...it prevents me from selecting the button

CODE --> vba

Private Sub UserForm_Initialize()
    iCounter = 1
    TextBox1.Text = CStr(iCounter)
End Sub

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    Cancel = True
    TextBox1.SelStart = 0
    TextBox1.SelLength = Len(TextBox1.Text)
End Sub

Private Sub CommandButton1_Click()
    If iCounter < 3 Then
        iCounter = iCounter + 1
    Else
        Unload Me
    End If
    TextBox1.Text = CStr(iCounter)
End Sub 

Everyone seems to need to check to see if the textbox1.value adheres to some rule, I don't need to check it...i just need it to be highlighted each time a default value is entered automatically in the textbox.

Ant help is appreciated.

RE: Focus Remain in Textbox during loop (VBA6)

Is this an external VB6 program or a VBA program?

RE: Focus Remain in Textbox during loop (VBA6)

(OP)
VBA

RE: Focus Remain in Textbox during loop (VBA6)

(OP)
I just found a video that talks about view tab order. I changed my tab order to focus on the textbox first, then the button second. Not sure if this was instrumental to the solution, but thought I would add that in.

I then rewrote my code as follows and it does exactly what I want:

CODE --> VBA

Private Sub UserForm_Initialize()
    iCounter = 1
    TextBox1.Text = CStr(iCounter)
End Sub

Private Sub CommandButton1_Click()
    If iCounter < 3 Then
        iCounter = iCounter + 1
        TextBox1.Text = CStr(iCounter)
        TextBox1.SetFocus
        TextBox1.SelStart = 0
        TextBox1.SelLength = Len(TextBox1.Text)
    Else
        Unload Me
    End If
    
End Sub 

RE: Focus Remain in Textbox during loop (VBA6)

Setting the focus to the text box on button click fixed your problem.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources