PDA

View Full Version : Solved: Validating entry on a userform (Excel '97)



phendrena
03-09-2009, 08:03 AM
Hi,

I'm currently using the following to validate entry on a userfrom :
If Trim(Me.txtTwo.Value) = "" Then
Me.txtTwo.SetFocus
MsgBox "Please enter the Customers Name!"
Exit Sub
End If

Instead of using this code, is there anyway that I can validate it based on the amount of characters entered? For example, the field must contain at least 3 characters?

Thanks,

nst1107
03-09-2009, 09:00 AM
Try
If Len(Trim(Me.txtTwo.Value)) < 3 Then
Me.txtTwo.SetFocus
MsgBox "Please enter the Customers Name!"
Exit Sub
End If

phendrena
03-09-2009, 09:25 AM
Thank you, that works a treat.

mdmackillop
03-09-2009, 11:05 AM
You can also "check as you go"
Private Sub txtTwo_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Len(Trim(txtTwo.Value)) < 3 Then
Cancel = True
MsgBox "Please enter the Customers Name!"
txtTwo.Text = ""
End If
End Sub

phendrena
03-16-2009, 07:58 AM
You can also "check as you go"
Private Sub txtTwo_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Len(Trim(txtTwo.Value)) < 3 Then
Cancel = True
MsgBox "Please enter the Customers Name!"
txtTwo.Text = ""
End If
End Sub
Interesting idea, I'll bear this one in mind for use later. I prefer to let people enter everything and then do the check at the very end as if a popup appears mid-way through the data entry it could cuase other problems if they don't notice and just keep typing :)

mdmackillop
03-16-2009, 10:00 AM
No problem. Of course you can't keep typing with a message box on the screen!

phendrena
03-16-2009, 10:34 AM
No problem. Of course you can't keep typing with a message box on the screen!