issue with textbox in form

A

Associates

Hi,

I was wondering if i could get some help with the form.

I'd like to be able to take the cursor back to the text-box where there is
an error. The error may be produced because the entry data doesn't satisfy
the condition.

For example, there is a textbox called age. I have the following code

Private Sub Age_Exit(Cancel As Integer)
If Me.Age < 20 Then
MsgBox "The age cannot be less than 20. Please check it again"
End If

me.Age = "" ' Clear the box
End Sub

How do i get the cursor back to the Textbox - age? i have tried to use
..setfocus but to no avail.

Thank you in advance
 
A

Allen Browne

Add the line:
Cancel = True

It might be better to use the BeforeUpdate event procedure of the control.
 
S

Steve Schapel

Associates,

As an aside, you wouldn't try to set the textbox's value to "" a
zero-length string. This line in your code should be:
Me.Age = Null

In any case, I am going to suggest another approach, namely to use the
Before Update event to make this check. Something like this:

Private Sub Age_BeforeUpdate(Cancel As Integer)
If Me.Age < 20 Then
MsgBox "The age cannot be less than 20. Please check it again"
Cancel = True
Me.Age.Undo
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top