Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Focus Remain in Textbox during loop (VBA6)

Status
Not open for further replies.

lardman363

Automotive
Feb 8, 2013
173
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:
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:
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.
 
Replies continue below

Recommended for you

Is this an external VB6 program or a VBA program?
 
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:
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
 
Setting the focus to the text box on button click fixed your problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor