how to place the cursor in the field of a subform

G

Gator

Why won't the following code place the cursor in the field "FundNumber" in
the subform?

Private Sub Command10_Click()
On Error GoTo Err_Command10_Click

If IsNull(Me.PayDate) Then
Me!PayDate.SetFocus
Else
Me.TabCtl27.Pages(0).SetFocus
Me.PayablesDetailssubform1.SetFocus
DoCmd.GoToRecord , , acFirst
Me!PayablesDetailssubform1.Form.Controls(FundNumber).SetFocus

If IsNull(Me.PayablesDetailssubform1) Then
Me.TabCtl27.Pages(0).SetFocus
Me.PayablesDetailssubform1.SetFocus
DoCmd.GoToRecord , , acFirst
Me!PayablesDetailssubform1.Form.Controls(FundNumber).SetFocus

Else
Me!PayDate.SetFocus
DoCmd.GoToRecord , , acNewRec
Me!PayDate.SetFocus

End If
End If


Exit_Command10_Click:
Exit Sub

Err_Command10_Click:
MsgBox Err.Description
Resume Exit_Command10_Click

End Sub
 
D

Dennis

Try:

Form!PayablesDetailssubform1.FundNumber.SetFocus

or

Me!PayablesDetailssubform1.Form!FundNumber.SetFocus
 
G

Gator

the error...as before....is...
MS Access cannot move the focus to the control PayablesDetailssubform1
 
D

Dennis

When you created the subform CONTROL in your main form, what name does the
CONTROL have?

For example, I created a subform control named "subMySub1", and in it is the
subform named "subAllMyDetailData".

When you reference the subform, you must use the CONTROL name (what you
called the control on the main form), and not the actual SUBFORM name.
 
G

Gator

When I set the breakpoint...the code breaks when it hits this line...
Me.PayablesDetailssubform1.SetFocus
 
D

Douglas J. Steele

You need to set focus to the subform control on the parent form first, then
set focus to the control on the subform.

Me!PayablesDetailssubform1.SetFocus
Me!PayablesDetailssubform1.Form.Controls(FundNumber).SetFocus


Be aware that the name of the subform control on the parent form can be
different than the name of the form being used as a subform. You must use
the subform control name in both places.
 
D

Douglas J. Steele

If FundNumber is the name of the control, it needs to be in quotes.

Me!PayablesDetailssubform1.Form.Controls("FundNumber").SetFocus

(Sorry, I read your post rather quickly, and missed the fact that it wasn't
a variable)
 
G

Gator

We are definitely getting somewhere...the only thing is when I enter a date
and push enter or tab, the Main form moves to a new record instead of going
to the subform and checking to see if is null or not to determine whether to
stay put or goto new record in Main form.
thanks
 
D

Douglas J. Steele

Enter a date where: on the parent form or the subform?

And what does this have to do with the code you've shown? That's the Click
event of a button.
 
G

Gator

What if the code is set as....

Private Sub PayDate_Exit(Cancel As Integer)
On Error GoTo Err_Command10_Click

If IsNull(Me.PayDate) Then
Cancel = True
Else
Me.TabCtl27.Pages(0).SetFocus
Me!PayablesDetailssubform1.SetFocus

If IsNull(Me!PayablesDetailssubform1) Then
Me!PayablesDetailssubform1.Form.Controls("FundNumber").SetFocus

Else
Me!PayDate.SetFocus
DoCmd.GoToRecord , , acNewRec
End If
End If


Exit_Command10_Click:
Exit Sub

Err_Command10_Click:
MsgBox Err.Description
Resume Exit_Command10_Click

End Sub

Sincerely...
 
G

Gator

Doug...It's going to be easier for me to tab out or enter after the date is
entered in PayDate...This code I have here on the Enter event is giving me an
error that says...

"The expression On Current you entered as the event property setting
produced the following error: Procedure declaration does not match
description of event or procedure having the same name."

Below is all the code I'm using...

Private Sub Form_Current()

'Set the focus to PayDate
Me.PayDate.SetFocus

End Sub

==============================================

Private Sub PayDate_Enter(cancel As Integer)
On Error GoTo Err_Command10_Click

If IsNull(Me.PayDate) Then
cancel = True

Else
Me.TabCtl27.Enabled = True
Me.TabCtl27.Pages(0).SetFocus
Me!PayablesDetailssubform1.SetFocus
Me!PayablesDetailssubform1.Form.Controls("FundNumber").SetFocus
End If


Exit_Command10_Click:
Exit Sub

Err_Command10_Click:
MsgBox Err.Description
Resume Exit_Command10_Click
End Sub
 
D

Douglas J. Steele

I don't see why you'd get a problem with your Form_Current sub, but your
Enter sub is incorrect.

Private Sub PayDate_Enter(cancel As Integer)

needs to be

Private Sub PayDate_Enter()

Always let Access generate the stubs for you, just in case...
 
G

Gator

What is happening is I'm getting an error saying that the focus cannot move
to the control PayDate when the form loads when I try to run the form. All I
want is on a new record for the focus to be in PayDate and then once that is
entered and the enter button on the keyboard is pressed the cursor moves to
the first control of the first record in the subform on the first tab on the
tab control. IF PayDate is Null on the press of Enter, then do nothing and
keep the cursor in Paydate waiting for an entry.
thanks

Private Sub Form_Current()

'Set the focus to PayDate
Me.PayDate.SetFocus

End Sub


Private Sub PayDate_Enter()
On Error GoTo Err_Command10_Click

If IsNull(Me.PayDate) Then
Cancel = True

Else
Me.TabCtl27.Pages(0).SetFocus
Me!PayablesDetailssubform1.SetFocus
DoCmd.GoToRecord , , acNewRec
Me!PayablesDetailssubform1.Form.Controls("FundNumber").SetFocus
End If


Exit_Command10_Click:
Exit Sub

Err_Command10_Click:
MsgBox Err.Description
Resume Exit_Command10_Click
End Sub
 
D

Douglas J. Steele

Not really sure why it's not letting you set focus, but the On Enter event
doesn't mean that the Enter key's been pressed in the control. On Enter is
an event that occurs before the control actually receives the focus from
another control on the same form.
 
G

Gator

I really appreciate you sticking it out with me...Is there any suggestion on
how I might approach my intention described in my previous reply? The
current code is shown below my previous reply. It doesn't seem like it
should be complicated...but I can't make it work. Just to kinda rehash....
1. Open form and set cursor to PayDate
A. IF PayDate is blank...
a. by pressing enter, or...
b. by pressing tab
B. THEN keep cursor in PayDate
C. ELSE move cursor to...
a. the control on the subform on the tab control.
Thanks
 
D

Douglas J. Steele

Set focus to PayDate in the form's Current event.

In the BeforeUpdate event of the PayDate text box, put code like:

Private Sub PayDate_BeforeUpdate(Cancel As Integer)

If IsDate(Nz(Me.PayDate, "X") = False Then
Cancel = True
Else
Me!NameOfSubformControl.SetFocus
Me!NameOfSubformControl.Form!NameOfControlOnSubform.SetFocus
End If

End Sub
 
G

Gator

I used the code exactly as you proposed...I get an error saying "you must
save the field before you execute the GoTo Control action, the GoTo Control
method, or the SetFocus method. You you help me with this please?
 

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