VBA Return focus to a text box within a frame

JST013

Board Regular
Joined
Mar 11, 2015
Messages
74
Hello all,

Today I am working with a text box that is the last in a frame. I've been trying to make a message box appear if the user types too many number in textbox1. I used an exit event for textbox1, but I guess that doesn't work because it is within frame1, and the next object in the tab order is textbox2 in frame2. To get around this I tried using an exit frame1 event which worked for the most part, but I wanted the focus to return to textbox1. I could backspace what was written in textbox1, but I could not write anything in textbox1. Here is my code...

What can I do to make it so I can type in textbox1 again?

Code:
Private Sub Frame1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        
    Dim itemcode As String
    Dim w


    itemcode = Me.tbItemCode
    itemcode = Replace(itemcode, " ", "")
    w = CountNumbers(itemcode)
    If w > 5 Then
        Cancel = True
        MsgBox ("Item code number has too many numbers. Please double check.")
        Me.Frame1.SetFocus
        With Me.tbItemCode
            .SelStart = 0
            .SelLength = Len(Me.tbItemCode.Text)
            .SetFocus
        End With
        
    End If
    
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hello

Is this what you want?

Code:
Private Sub TextBox10_Change()
If Len(Me.TextBox10.Text) > 5 Then
    MsgBox ("Item code number has too many numbers. Please double check.")
    Me.TextBox10.Text = ""
End If
End Sub
 
Upvote 0
Worf,

Thank you very much for you suggestion. Unfortunately it has the same effect. I still can not type in the text box after the message box pops up.

I may end up having to do a label that pops up when the number is too large. I just wish I could figure this out for future reference.
 
Upvote 0
I’m assuming the frame is inside a user form. Can you post a link to the workbook?
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,449
Members
449,083
Latest member
Ava19

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