Before Update and Keydown

G

Guest

Hi,

I have a textbox with 2 events: Before Update and Keydown. This textbox is
in a subform and once the user hits "Tab" or "Enter" the focus should be set
to a textbox outside of this subform. However, in my Before Update event I
have a Cancel = True if a certain condition isn't met. Because of this
"Cancel = True" part and the part in my Keydown event where I'm trying to set
the focus elsewhere, I get an error message.

To get around it I was thinking of using the textbox's Exit (or is Lost
Focus better?) event but I'm not sure how to set the focus to outside the
subform if the user hits only either "Tab" or "Enter".

Does anyone have any suggestions?
Here is my code:

Private Sub txtDate_BeforeUpdate(Cancel As Integer)
If IsNull(txtDate) Or txtDate = "" Then
MsgBox "Please enter the date"
Cancel = True
End If
End Sub

Private Sub txtDate_KeyDown(KeyCode As Integer, Shift As Integer)
Dim stForm As String, stSForm As String
stForm = "frmMain"
stSForm = "frmMain-Sub"
If KeyCode = vbKeyReturn Or KeyCode = vbKeyTab Then
KeyCode = 0
DoCmd.GoToControl Forms(stForm).Form(stSForm).Name
Forms(stForm).Form(stSForm).Controls("txtMonth").SetFocus
End If

End Sub

Thanks in advance!
 
A

Allen Browne

Use the LostFocus event instead of the KeyDown event. It won't fire until
the control's BeforeUpdate event is satisfied.

BTW, the BeforeUpdate event of a controls is not a satisfactory way to
ensure an entry was made. If the user never visits the control, or never
types anything there, the event does not fire. You can use the BeforeUpdate
event of the *form*, or set the Required property of the field to Yes in
table design.
 
G

Guest

Hi Allen,

Thanks for replying. I have a form Before Update event too so the text box's
Before Update event is not really necessary; I just like it so the user gets
the "Enter a date message" as soon as s/he deletes what was in there.

I'll try the Lost Focus event instead. Is there any way to trigger the
setfocus event (of something on the mainform) when only the Tab or Enter key
is pressed? That text box is the only control on the subform.

Thanks
 
A

Allen Browne

juicegully said:
Is there any way to trigger the
setfocus event (of something on the mainform) when only the Tab or Enter
key
is pressed? That text box is the only control on the subform.

No. The event will trigger under other conditions also, e.g. if they click
there with the mouse.
 
G

Guest

Hi Allen,

I used the Lost Focus event and it worked. It's strange because I used the
Lost Focus event in other places in my code. I think I didn't try it this
time because I had other scenarios with the Lost Focus event and I didn't get
the correct results. I think I also tried using the Exit event. Anyway,
thanks for your help!
 

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