Connecting Tech Pros Worldwide Forums | Help | Site Map

how to set focus on field in tabbed form?

Mary
Guest
 
Posts: n/a
#1: Apr 11 '06
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
Guest
 
Posts: n/a
#2: Apr 11 '06

re: how to set focus on field in tabbed form?


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" <mmcgillivray@msn.com> wrote in message
news:1144709473.256369.36120@z34g2000cwc.googlegro ups.com...[color=blue]
> 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
>[/color]


Mary
Guest
 
Posts: n/a
#3: Apr 11 '06

re: how to set focus on field in tabbed form?


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
focus to the null field.

Br@dley
Guest
 
Posts: n/a
#4: Apr 11 '06

re: how to set focus on field in tabbed form?


Mary wrote:[color=blue]
> 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 focus to the null field.[/color]

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

Br@dley


tina
Guest
 
Posts: n/a
#5: Apr 11 '06

re: how to set focus on field in tabbed form?


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" <mmcgillivray@msn.com> wrote in message
news:1144717015.270371.81760@v46g2000cwv.googlegro ups.com...[color=blue]
> 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
> focus to the null field.
>[/color]


Mary
Guest
 
Posts: n/a
#6: Apr 11 '06

re: how to set focus on field in tabbed form?


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
Guest
 
Posts: n/a
#7: Apr 11 '06

re: how to set focus on field in tabbed form?


Tina,

I figured I was missing something!

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

Mary

tina
Guest
 
Posts: n/a
#8: Apr 11 '06

re: how to set focus on field in tabbed form?


you're welcome! :)


"Mary" <mmcgillivray@msn.com> wrote in message
news:1144721463.127551.291390@z34g2000cwc.googlegr oups.com...[color=blue]
> Tina,
>
> I figured I was missing something!
>
> That did the trick. Thank you so much for your help!
>
> Mary
>[/color]


tlyczko
Guest
 
Posts: n/a
#9: Apr 16 '06

re: how to set focus on field in tabbed form?


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
Guest
 
Posts: n/a
#10: Apr 16 '06

re: how to set focus on field in tabbed form?


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" <tlyczko@gmail.com> wrote in message
news:1145215932.434355.92360@v46g2000cwv.googlegro ups.com...[color=blue]
> 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
>[/color]


Closed Thread