iamnu said:
I said I was getting a syntax error, but I should have said a run time
error # 2110..."Microsoft Access can't move the focus to the control
TelephoneBookSubForm". The code getting the error
is...Forms!TelephoneBook!TelephoneBookSubForm.SetFocus and I have this
code in the BeforeUpdate event of the Main Form
In my case, applicable data can simply be a person's name.
Actually, you didn't *say* you were getting any kind of error, so I assumed
you just weren't seeing the focus change, for the reason I described. That
reason still holds true, by the way -- you must both move the focus to the
subform *and* move the focus to the control on the subform. However, now
that you tell me both what error you're getting and what event you're
running the code in, I can see why you're getting the error.
You can't move the focus to the subform in the main form's BeforeUpdate
event. That's because, in order for the focus to go to the subform, the
main form's record must be saved. But in the form's BeforeUpdate event, the
record hasn't been saved yet. Moving the focus to the subform would force
the form to save the record, which would fire the BeforeUpdate event, which
would try to move the focus to the subform, which would fire the
BeforeUpdate event ... I hope you see the problem.
Instead of using the form's BeforeUpdate event, I suggest you use the
AfterUpdate events of the controls that should be completed first. For
example, if there are two controls, FirstName and LastName, then you could
write a function to move the focus to the subform when they are both filled
in, and then call that function from the AfterUpdate event of each of the
two controls. This function would go in the General section of the form's
Module, and might look like this:
'----- start of function code -----
Private Function GoToSubformIfComplete()
If IsNull(Me.LastName) _
Or IsNull(Me.FirstName) _
Then
' Record is incomplete, so do nothing.
Else
' Set the focus to cboDirectory on the subform.
With Me.TelephoneBookSubForm
.SetFocus
.Form!cboDirectory.SetFocus
End With
End If
End Function
'----- end of function code -----
Now, you still have to call this function from the AfterUpdate events of
FirstName and LastName. You can do that easily, without even writing event
procedures, by using a function expression in the AfterUpdate *properties*
of those controls. Just select both controls (in design view) and open
their joint property sheet. On the Event tab of the property sheet, in the
AfterUpdate property line, enter:
=GoToSubformIfComplete()
And that should do it. Note: if you are already using the AfterUpdate event
of one or both of these controls, you won't be able to do it this way,
because you'd be replacing the existing event code (or macro), not just
adding to it. In that case, you'd have to create an actual event procedure
for the control, and have that event procedure both execute whatever was
previously being executed, and also call the function
GoToSubformIfComplete().