PC Review


Reply
Thread Tools Rate Thread

Data validation in Userform sub

 
 
Marcolino
Guest
Posts: n/a
 
      4th Aug 2009
If a user enters the wrong info into a textbox control, how do I program the
userform to display an error message and return the user to the textbox to
enter the correct info?

I've tried using the Exit event with a MsgBox inside a Do While...Loop, but
either the MsgBox keeps looping, or the focus switches to the next control,
even if I use the SetFocus method:

Private Sub ControlNumb_Exit(ByVal Cancel As MSForms.ReturnBoolean)

Do While IsNumeric(NewResEntryForm.ControlNumb.Value) = False _
Or Len(NewResEntryForm.ControlNumb.Value) <> 5

response = MsgBox("Please enter a valid control number", vbOKOnly)

If response = vbOK Then
NewResEntryForm.ControlNumb.SetFocus
Exit Sub
End If

Loop

End Sub

Please help, thanks.
 
Reply With Quote
 
 
 
 
Jacob Skaria
Guest
Posts: n/a
 
      4th Aug 2009
Try the below.

Private Sub ControlNumb_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsNumeric(Me.ControlNumb) = False Or Len(Me.ControlNumb) <> 5 Then
MsgBox "Please enter a valid control number", vbOKOnly
Cancel = True
End If
End Sub

If this post helps click Yes
---------------
Jacob Skaria


"Marcolino" wrote:

> If a user enters the wrong info into a textbox control, how do I program the
> userform to display an error message and return the user to the textbox to
> enter the correct info?
>
> I've tried using the Exit event with a MsgBox inside a Do While...Loop, but
> either the MsgBox keeps looping, or the focus switches to the next control,
> even if I use the SetFocus method:
>
> Private Sub ControlNumb_Exit(ByVal Cancel As MSForms.ReturnBoolean)
>
> Do While IsNumeric(NewResEntryForm.ControlNumb.Value) = False _
> Or Len(NewResEntryForm.ControlNumb.Value) <> 5
>
> response = MsgBox("Please enter a valid control number", vbOKOnly)
>
> If response = vbOK Then
> NewResEntryForm.ControlNumb.SetFocus
> Exit Sub
> End If
>
> Loop
>
> End Sub
>
> Please help, thanks.

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic userform help - Data Validation James Microsoft Excel Programming 3 15th Apr 2009 02:48 PM
Userform - validation data. Please help needed Eduardo Microsoft Excel Programming 2 12th Dec 2008 06:47 PM
Data validation using TextBox in UserForm Chris Microsoft Excel Programming 3 26th Jan 2007 08:37 AM
Data validation within a userform kjenner7 Microsoft Excel Programming 4 9th Mar 2006 03:37 PM
Using a Userform instead of Data Validation Knut Dahl Microsoft Excel Programming 2 10th Apr 2005 11:02 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:38 AM.