Set Focus and DoCmd GoToControl

B

B F Cole

I'm trying to check if a user leaves a field blank, post an error message,
and then return to the field for entry. The code will go to any other
field, but not the field that I want. Here's sample code:

This is executed as an event on field exit.

If ISNULL(Me.FIELD1) Then
iAnswer = MsgBox("Product Code can not be blank...re-enter or Press ESC
to Cancel...",vbCritical)
Forms!frmForm1!FIELD1.SetFocus
End If

This code goes to the next field in the tab list instead of FIELD1. If you
substitute FIELD3, FIELD4, etc in the above, it will go to FIELD3, FIELD4,
etc..

This code also behaves in a similar manner.

If ISNULL(Me.FIELD1) Then
iAnswer = MsgBox("Product Code can not be blank...re-enter or Press ESC
to Cancel...",vbCritical)
Set ctl = Forms!frmForm1!FIELD1
DoCmd.GoToControl ctl.Name
End If

Any suggestions on how to return to FIELD1?

Thanks,
Bill
 
D

Douglas J Steele

Don't use either. Simply set Cancel = True to cancel the Exit, meaning that
focus stays on the field.
 
A

Allen Browne

What event are you using?

The problem is probably that the SetFocus is occurring too early (before it
has left this control), and so has no effect, as Access resumes normal
processing after the SetFocus, and so jumps to the next control.

But there is a logical problem with your approach. You cannot use any of the
control's events to ensure an entry, since the user may never visit that
control (e.g. clicking somewhere else without ever going to that one.) The
only event that can prevent that is the BeforeUpdate event of the *form*.

Of couse, you could just open the table in design view and set the field's
Required property to yes, and no code is needed.
 
B

B F Cole

Cancel worked fine...thanks.

Bill


Douglas J Steele said:
Don't use either. Simply set Cancel = True to cancel the Exit, meaning
that
focus stays on the field.
 

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