Issues with forms and set focus

princesstech

New Member
Joined
May 6, 2003
Messages
17
Hi,

I have a form with several text boxes. At least one of those text boxes is set to validate on exit, generate an error message then return the cursor to the text box so the EU can make the correction.

Here's the code I'm using, and I can't find anything wrong with it.

Code:
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox2 = "" Then
MsgBox "fill it in"
TextBox2.SetFocus
End If
End Sub

What am I doing wrong? Any help would be appreciated.

TIA

Marie
 
My test UserForm had 2 TextBoxes. With the code I posted, if I clicked on TextBox2 without having completed TextBox1, focus returned to TextBox1. You don't need any Exit event procedures, but you will need Enter event procedures for each of your TextBoxes, with appropriate code.
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi Andrew,

Sorry I haven't gotten back to you on this one sooner, I took a long weekend from work :)

I created a new form with only the 2 text boxes, and your code worked great until I added a third text box. When I add the 3rd text box, the focus goes to that 3rd text box when I tab out of the first text box into the 2nd one.

I was thinking it was just my machine, so I tried it on a different computer, both computers are Windows 2k with Office XP.

Any other ideas? This one really has me stumped :confused:
 
Upvote 0
Very strange!

Back to the Exit event - try this:

Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If TextBox1.Value = "" Then
        MsgBox "Fill me in"
        Cancel = True
    End If
End Sub

The secret seems to be setting the Cancel property to True.
 
Upvote 0
Thank you for your time and your patience, that works beautifully. I would never have though to set the cancel property to true.

Thanks again!

:D

Marie
 
Upvote 0

Forum statistics

Threads
1,221,691
Messages
6,161,322
Members
451,696
Latest member
Senthil Murugan

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