ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to re-set focus to the text box after deleting invalid data? (https://www.excelbanter.com/excel-programming/343105-how-re-set-focus-text-box-after-deleting-invalid-data.html)

[email protected]

How to re-set focus to the text box after deleting invalid data?
 
Hello:
I would appreciate help with the following:

I have a user form with text box.
If the user enters an invalid data into the text box, I display a
message box stating "DATE ENTERED IS INVALID. PLEASE RE-ENTER"

When the user clicks OK, the invalid data gets deleted using:
txtbox1.value =""

I then want the same textbox (txtbox1) to get focus so that the user
can re-enter data.
However, I notice that the cursor moves to the NEXT text box (eg
txtbox2)
I have tried using the code
txtbox1.setfocus" OR
txtbox1.SelStart = 0

However, none seems to work. I am obviously missing something simple.
Would really appreciate your help

TIA
Vinay


Tom Ogilvy

How to re-set focus to the text box after deleting invalid data?
 
If the textbox is on a userform, your validation code should be in the Exit
event. You will see this event has an argument CANCEL.

If the entry is not validated, then in that part of your code just put in a
line

Cancel = True

and the textbox will not be exited.

--
Regards,
Tom Ogilvy

wrote in message
oups.com...
Hello:
I would appreciate help with the following:

I have a user form with text box.
If the user enters an invalid data into the text box, I display a
message box stating "DATE ENTERED IS INVALID. PLEASE RE-ENTER"

When the user clicks OK, the invalid data gets deleted using:
txtbox1.value =""

I then want the same textbox (txtbox1) to get focus so that the user
can re-enter data.
However, I notice that the cursor moves to the NEXT text box (eg
txtbox2)
I have tried using the code
txtbox1.setfocus" OR
txtbox1.SelStart = 0

However, none seems to work. I am obviously missing something simple.
Would really appreciate your help

TIA
Vinay




Executor

How to re-set focus to the text box after deleting invalid data?
 
Hi Vinay,

Tou can use the exit event for the textbox:

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsDate(TextBox1.Text) Then
MsgBox "DATE ENTERED IS INVALID. PLEASE RE-ENTER",
vbExclamation, "Retry"
Me.TextBox1.Text = ""
Cancel = True
End If
End Sub

Hoop This Helps

Wouter



All times are GMT +1. The time now is 12:39 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com