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
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try putting the code in the OK button click event procedure instead:

Code:
Private Sub CommandButton1_Click()
    If TextBox2.Value = "" Then
        MsgBox "fill it in"
        TextBox2.SetFocus
        TextBox2.SelStart = 0
    Else
'       Whatever
    End If
End Sub
 
Upvote 0
Thanks Andrew,

It works in the click event, however I have about 5 fields that need to be validated as soon as the user leaves that field because they are used to validate other fields, which is why I need the focus to go back to the field that is not entered correctly so they can fix the problem before going to the next field.

Marie
 
Upvote 0
Well, maybe you can approach it this way:

Code:
Private Sub TextBox2_Enter()
    If TextBox1.Value = "" Then
        MsgBox "Fill me in"
        TextBox1.SetFocus
    End If
End Sub
 
Upvote 0
That made it a little clearer as to what is happening but it still doesn't work.

I run that code and here's what happens.

Senario 1 : From Textbox2 click on textbox1 The message pops up and the focus jumps to textbox1 as it should

Senario 2: From Textbos2 click on textbox3 you get the message, click ok, and you get the message again, then the focus jumps to textbox1 as specified in the code.

Marie
 
Upvote 0
I don't know why you should be getting the message when you click on TextBox3.

The event procedure is for TextBox2. Why does it fire when you click TextBox3?
 
Upvote 0
Sorry my bad :oops: , I had forgotten to change the code from exit to enter. The focus is right when you use the code you posted, but it doesn't validate if the box is empty when you leave the box, it just reminds you to fill it in.

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