Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to set focus on field in tabbed form?

374 views
Skip to first unread message

Mary

unread,
Apr 10, 2006, 6:51:13 PM4/10/06
to
I have a tabbed form. Tab 1 has fields that are mandatory. I am trying
to validate that these fields are not null, and cancel updating if any
of them are. The following code works, except the form closes instead
of setting focus to the null field. What am I doing wrong? Do I need to
refer to the tab name when I set focus? I sure would appreciate any
help!

Here is my code:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Len(Me.TherapyStartDate & "") = 0 Then
Cancel = True
MsgBox "You must enter a value in Date!"
Me!TherapyStartDate.SetFocus
ElseIf Len(Me.TherapyStartTime & "") = 0 Then
Cancel = True
MsgBox "You must enter a value in Start Time!"
Me!TherapyStartTime.SetFocus
ElseIf Len(Me.TherapyEndTime & "") = 0 Then
Cancel = True
MsgBox "You must enter a value in End Time!"
Me!TherapyEndTime.SetFocus
End If
End Sub

tina

unread,
Apr 10, 2006, 8:53:37 PM4/10/06
to
well, when the form closes, did you click a command button that runs a Close
command? or did you click the "X" button on the form's Title Bar (far right
corner at the top)? or are you saying the the form closes "out of the blue",
even though you made no attempt to close it?

hth


"Mary" <mmcgil...@msn.com> wrote in message
news:1144709473....@z34g2000cwc.googlegroups.com...

Mary

unread,
Apr 10, 2006, 8:56:55 PM4/10/06
to
I click on a command button to close, but if these fields are null, I
don't want it to close. I want it to remain on the current form and set

Br@dley

unread,
Apr 10, 2006, 9:13:50 PM4/10/06
to

Try setting the focus to the tab page first, then the field on that page?
--
regards,

Br@dley


tina

unread,
Apr 10, 2006, 9:34:49 PM4/10/06
to
cancelling the BeforeUpdate event only cancels the write to the table, it
doesn't cancel the Close command. you need to explicitly save the record in
the Close button code, and then handle the error that will arise from the
cancelled update, so that the Close command never runs, as

On Error GoTo Err_Handle

DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close , , acSaveNo

Err_Exit:
Exit Sub

Err_Handle:
Select Case err.Number
Case Not 2501
MsgBox err.Number & " " & err.Description, "Unknown error"
End Select

hth


"Mary" <mmcgil...@msn.com> wrote in message

news:1144717015....@v46g2000cwv.googlegroups.com...

Mary

unread,
Apr 10, 2006, 9:41:03 PM4/10/06
to
My tab page is named "Session".

I tried adding:

Me!Session.SetFocus

before the lines that set focus to the null fields. It closes anyway.

Thanks for your response.

Mary

unread,
Apr 10, 2006, 10:11:03 PM4/10/06
to
Tina,

I figured I was missing something!

That did the trick. Thank you so much for your help!

Mary

tina

unread,
Apr 10, 2006, 11:46:02 PM4/10/06
to
you're welcome! :)


"Mary" <mmcgil...@msn.com> wrote in message

news:1144721463.1...@z34g2000cwc.googlegroups.com...

tlyczko

unread,
Apr 16, 2006, 3:32:12 PM4/16/06
to
Can this code be used to ensure that a SUBFORM does not close without
having at least one record added via the subform and/or that the
SUBFORM cannot be closed if at least one or more fields in the subform
are not filled in???

Would one put this in the OnExit event of the subform??

Thank you, Tom

tina

unread,
Apr 16, 2006, 6:36:54 PM4/16/06
to
no, you can't use the posted code in a subform, because you don't "close" a
subform - you close the main form that the subform is "sitting" on. see my
more detailed answer in your thread started this date and titled "Why is my
BeforeUpdate code not working??".

hth


"tlyczko" <tly...@gmail.com> wrote in message
news:1145215932....@v46g2000cwv.googlegroups.com...

0 new messages