SetFocus in IF statement

AccessNub

Registered User.
Local time
Today, 07:55
Joined
Jul 22, 2006
Messages
71
OK, so this one is probably really easy to solve but im stuck, I have an IF statement After Update to make sure that the field has the required information. If it doesn't then it voids the entry. At this point I want the cursor to be set to the box it just cleared out but for some reason it keeps jumping to the next tab control.

Private Sub Box_AfterUpdate()
If Len(Me.Box) <> 9 Then
Me.Box = Null
Me.Box.SetFocus
End If
End Sub

Can you guys tell me what I need to do to make it go?

Thanks in advance.
 
The place to do validation is the before update event. If validation fails, you add:

Cancel = True

to stop the update.
 
The place to do validation is the before update event. If validation fails, you add:

Cancel = True

to stop the update.

Private Sub Box_BeforeUpdate()
If Len(Me.Box) <> 9 Then Cancel = True
End Sub

Ok, I changed my code to look like the above, but with before update I cant clear the field using Me.box = Null or Me.box.Undo.

How do I clear the box?
 
Sure you can. How are you trying that doesn't work?
 
Sure you can. How are you trying that doesn't work?

I get a runtime error.

The macro or function set to the BeforeUpdate or ValidationRule property for this field is preventing Database from saving the data in this field.

I am not sure why its trying to save any data, the form has 5 unbound fields and no record source.
 
Try:

Code:
If Len(Me.Box) <> 9 Then
  Cancel = True
  Me.Box.Undo
End If
 
Ok, I went that route, The code steps through fine, but the field on the form doesn't clear. I tried both Me.Box.Undo and Me.Box = Null. The Null gives me an error while the Undo doesn't seem to do anything at all.
 
Now that I think about it, I think Undo only works with bound controls/forms. What are you trying to do with this form? Perhaps it would make more sense to check the length before the final processing is done.
 
Now that I think about it, I think Undo only works with bound controls/forms. What are you trying to do with this form? Perhaps it would make more sense to check the length before the final processing is done.

its basically for the first box if they dont enter a 9 digit number then it needs to fail, i just want it to clear as well. The data in this form feeds into other forms and queries
 

Users who are viewing this thread

Back
Top Bottom