ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Validating an entry on a user form (https://www.excelbanter.com/excel-programming/421428-validating-entry-user-form.html)

Risky Dave

Validating an entry on a user form
 
Hi,

I have a form that collects inputs from the user and verifies it against a
set of criteria.

As it stands at the moment, the code that does the evaluation is triggered
by the TextBox Exit event (when the user presses the enter key, tabs or
simply clicks elsewhere). This also causes the cursor to move on to the next
box in the tab index list.

How do I interrupt the tab index list in the event of an error (or point the
code back at the textBox in question) so that if the user inputs an invalid
value, the cursor remains in that TextBox?

TIA

Dave

royUK[_94_]

Validating an entry on a user form
 

Use SetFocus, something like this


Code:
--------------------
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me
If Not IsNumeric(.TextBox1.Value) Then
.TextBox1.Value = vbNullString
MsgBox "Only numeric entries allowed", vbCritical, "Input error"
.TextBox1.SetFocus
End If
End With
End Sub
--------------------


--
royUK

Hope that helps, RoyUK
For tips & examples visit my 'web site' (http://www.excel-it.com/)
------------------------------------------------------------------------
royUK's Profile: http://www.thecodecage.com/forumz/member.php?userid=15
View this thread: http://www.thecodecage.com/forumz/sh...ad.php?t=41848


Rick Rothstein

Validating an entry on a user form
 
The Exit event declaration header automatically provides a Cancel
argument... if you don't want to exit the TextBox, just set Cancel=True
inside the appropriate section of your code.

--
Rick (MVP - Excel)


"Risky Dave" wrote in message
...
Hi,

I have a form that collects inputs from the user and verifies it against a
set of criteria.

As it stands at the moment, the code that does the evaluation is triggered
by the TextBox Exit event (when the user presses the enter key, tabs or
simply clicks elsewhere). This also causes the cursor to move on to the
next
box in the tab index list.

How do I interrupt the tab index list in the event of an error (or point
the
code back at the textBox in question) so that if the user inputs an
invalid
value, the cursor remains in that TextBox?

TIA

Dave



royUK[_95_]

Validating an entry on a user form
 

I didn't think of that, much neater


Code:
--------------------
Option Explicit

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me.TextBox1
If Not IsNumeric(.Value) Then
.Value = Empty
Cancel = True
End If
End With
End Sub
--------------------


--
royUK

Hope that helps, RoyUK
For tips & examples visit my 'web site' (http://www.excel-it.com/)
------------------------------------------------------------------------
royUK's Profile: http://www.thecodecage.com/forumz/member.php?userid=15
View this thread: http://www.thecodecage.com/forumz/sh...ad.php?t=41848



All times are GMT +1. The time now is 02:52 PM.

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