Problem with SetFocus code (1 Viewer)

tacieslik

Registered User.
Local time
Today, 20:41
Joined
May 2, 2001
Messages
244
Keep getting a runtime error 2465 - Can't find the field . . .

This is my code:

Code:
Private Sub btnSortByMonthAZ_Click()

    Forms![frmSales]![sfrmSales].Form!fldMonth.SetFocus
    DoCmd.RunCommand acCmdSortAscending
 
End Sub


I'm trying to set the focus of a field within a sub form so that I can sort the records. The subform is in datasheet view. Any ideas?
 

Mile-O

Back once again...
Local time
Today, 20:41
Joined
Dec 10, 2002
Messages
11,316
Subforms don't have fields; they have controls. Set the focus to the control bound to the field.
 

tacieslik

Registered User.
Local time
Today, 20:41
Joined
May 2, 2001
Messages
244
Sorry 'fldMonth' is a bad name for my control, but it is the control, if you see what I mean
Thanks for the reply
 

PierreR

Registered User.
Local time
Today, 17:11
Joined
Jul 21, 2004
Messages
101
I believe the problem is the command button is on the main form and the setfocus is to the control on the subform. It works if you put the button on the subform. I've had the same problem, and would also like to know how to do this.

- Miles?
 

PierreR

Registered User.
Local time
Today, 17:11
Joined
Jul 21, 2004
Messages
101
I tried this tacieslik:

Code:
Private Sub btnSortByMonthAZ_Click()

    sfrmSales!fldMonth.SetFocus
    DoCmd.RunCommand acCmdSortAscending
 
End Sub

This code works to the point of setting the focus on the first record in the fldMonth object, but then it gives an error message; Run-time Error #2046: the command or action 'SortAscending' isn't available now.

We would be really grateful if you could tell us what is going on here, Miles.
 
R

Rich

Guest
Set focus to the subform first and then the control on the subform
 

tacieslik

Registered User.
Local time
Today, 20:41
Joined
May 2, 2001
Messages
244
Thanks for your replies.

So Rich, is this correct:
Code:
Private Sub btnSortByMonthAZ_Click()

    Forms!frmSales!sfrmSales.SetFocus
    fldMonth.SetFocus
    DoCmd.RunCommand acCmdSortAscending
 
End Sub

I'm sure I've used this code in the past, but I can't find a project with it being used. Is it possible that I need to add a reference in the VB Application? Or is my code still wrong?
Thanks for your time.
 

Users who are viewing this thread

Top Bottom