Keeping Focus on Control

G

Guest

Hi all,
I am trying to set up a control that needs to have data in it, and it does
not, to have it then put focus back on that control. I have tried using
DoCmd.GoToControl and Me.[control].SetFocus on the lost focus event, but it
always puts focus into the next control instead of the one I want it to be
in. Almost as if it cannot reset itself to the same control.
Help is always appreciated, thanks!
-gary
 
A

Allen Browne

Cancel the controls Exit event, e.g.:

Private Sub Text0_Exit(Cancel As Integer)
If IsNull(Me.Text0) Then
Cancel = True
End If
End Sub

Realistically, this is not the best approach. A user can click anywhere, so
they may not even visit the control. If the control is bound to a field,
open the table in design view, and set the Required property of the field to
Yes.

Alternatively, if you want the user to warn the user but allow them to save
the record if the field is blank, use the BeforeUpdate event of the *form*
(not control.)
 
R

ruralguy via AccessMonster.com

You are correct in that it cannot set the focus to itself. Just set Cancel =
True in either the BeforeUpdate event or the OnExit event of the control.

Gary said:
Hi all,
I am trying to set up a control that needs to have data in it, and it does
not, to have it then put focus back on that control. I have tried using
DoCmd.GoToControl and Me.[control].SetFocus on the lost focus event, but it
always puts focus into the next control instead of the one I want it to be
in. Almost as if it cannot reset itself to the same control.
Help is always appreciated, thanks!
-gary
 
G

Guest

Thank you Allen!
Thankfully, the user enters the form on this control, so they are now forced
to do the entry!
-gary

Allen Browne said:
Cancel the controls Exit event, e.g.:

Private Sub Text0_Exit(Cancel As Integer)
If IsNull(Me.Text0) Then
Cancel = True
End If
End Sub

Realistically, this is not the best approach. A user can click anywhere, so
they may not even visit the control. If the control is bound to a field,
open the table in design view, and set the Required property of the field to
Yes.

Alternatively, if you want the user to warn the user but allow them to save
the record if the field is blank, use the BeforeUpdate event of the *form*
(not control.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Gary Dolliver said:
Hi all,
I am trying to set up a control that needs to have data in it, and it does
not, to have it then put focus back on that control. I have tried using
DoCmd.GoToControl and Me.[control].SetFocus on the lost focus event, but
it
always puts focus into the next control instead of the one I want it to be
in. Almost as if it cannot reset itself to the same control.
Help is always appreciated, thanks!
-gary
 
G

Guest

Thank you!
-gary

ruralguy via AccessMonster.com said:
You are correct in that it cannot set the focus to itself. Just set Cancel =
True in either the BeforeUpdate event or the OnExit event of the control.

Gary said:
Hi all,
I am trying to set up a control that needs to have data in it, and it does
not, to have it then put focus back on that control. I have tried using
DoCmd.GoToControl and Me.[control].SetFocus on the lost focus event, but it
always puts focus into the next control instead of the one I want it to be
in. Almost as if it cannot reset itself to the same control.
Help is always appreciated, thanks!
-gary

--
HTH - RuralGuy (RG for short) acXP WinXP Pro
Please post back to this forum so all may benefit.

Message posted via AccessMonster.com
 

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