Error checking on user forms


Posted by Rob E on December 12, 2000 3:34 PM

I have a user form for collecting data and want to check that the data is valid as it is entered
At the moment I am using the text box on exit option to run code to check the data is within the needed values.
This all works OK and I am using msgbox to display an error message when the values are wrong.
However when I find an error I want to leave the user in that text box to correct the error however it always moves on to the next text box.
I have also tried using the same code in the after update option with no better result
I have put the textbox.setfocus in my code and this seems to make no difference

The code at present is

Private Sub makemargin_Exit(ByVal Cancel As MSForms.ReturnBoolean)
On Error GoTo errortrap
If Not makemargin = Empty Then
priceband = Empty
If makemargin < 0 Or makemargin > 7 Then
MsgBox "Margin must be between 0 and 7", vbExclamation
makemargin.SetFocus
Exit Sub
End If
End If
Exit Sub

But this always moves me on to the next text box on the form
Any ideas

Thanks
Rob

Posted by Jimmy on December 14, 2000 7:27 AM

Try Data Validation. See if it will do what you want.



Posted by Tim Francis-Wright on December 14, 2000 8:06 AM


I think that you need to set Cancel = True
after your MsgBox. (In a similar situation,
I used BeforeUpdate instead of Exit, so that the
value in the TextBox had no chance to do
anything, but there might be no need in your
case to change where the subroutine runs.)

Good Luck!