setting the focus in a control of the parent form

G

Guest

I have a form with 2 levels of nested subforms. The main form is student. It
has a semester subform, which in turn has a courses subform. If the user goes
directly to the courses subform to create new semester courses but there is
no semester record created yet, I want to save the semester record with the
displayed default values from its subform. I put the following code in the
AfterUpdate event of the course field of the lowest level form.

Private Sub Course_AfterUpdate()
If Me.Parent.RecordsetClone.BOF Then
' save record in parent
Me.Parent.SaveSemRec.SetFocus
End If
End Sub

If this works, I can save the record in the GotFocus event of the SaveSemRec
control (which is enabled and visible but barely shows). But it doesn't get
to that point. The above code does not work. The message I get is error 2110
Can't move focus to control SaveSemRec. Why doesn't it work? Is it because
the parent is not the main form? Is there another way to get this to work?
 
M

MacDermott

Hello, Ruth!

I suspect your trouble may be related to the fact that the focus can't leave
a subform until the current (dirty) record has been saved.

I'd suggest that you create relationships from your student table to your
semester table, and from semester to courses. Enforce referential integrity
with cascade updates, and your problems should go away.

HTH
- Turtle

ruth said:
I have a form with 2 levels of nested subforms. The main form is student. It
has a semester subform, which in turn has a courses subform. If the user goes
directly to the courses subform to create new semester courses but there is
no semester record created yet, I want to save the semester record with the
displayed default values from its subform. I put the following code in the
AfterUpdate event of the course field of the lowest level form.

Private Sub Course_AfterUpdate()
If Me.Parent.RecordsetClone.BOF Then
' save record in parent
Me.Parent.SaveSemRec.SetFocus
End If
End Sub

If this works, I can save the record in the GotFocus event of the SaveSemRec
control (which is enabled and visible but barely shows). But it doesn't get
to that point. The above code does not work. The message I get is error 2110
Can't move focus to control SaveSemRec. Why doesn't it work? Is it because
the parent is not the main form? Is there another way to get this to
work?
 
G

Guest

I have all those relationships and they do cascade. The problem is that the
semester key information has defaults on the form. If there is no need to
change the defaults that's when the user may go directly to enter the course
information. In this case, Access will let them enter info on the line but
they can't save it because there is no related semester record. I just want
to trap this condition before it causes a problem and save the semester
record with the defaults. It doesn't have to be in the AfterUpdate event. I
tried the Enter event first, but Access seems to go there when loading the
form, even if no one clicks in the subform. What event can I use that will
indicate that the user is ready to enter data in the form before the dirty
indication has been set?
 
W

Wayne Morgan

Going up usually isn't a problem. Going the other way, you have to set the
focus to the subform control then to the control on the subform (2 steps).
With this being another subform though, there may be a problem. However,
even if the focus works, just having the Default Values in the form won't
make the form "dirty", so there is no record to be saved.

I suspect the reason you can't move the focus is because you may have
another error interfering. You are trying to enter courses for a non
existing semester record. Therefore, there is no value in the fields linking
the two forms. If you have the tables that feed the two forms linked on this
field (they should be) then you won't be able to create the record on the
many side until you have a matching record on the one side.

Use the Enter event of the subform control of the second subform to pop-up a
message box telling the user that they didn't select a semester and asking
if they want to continue with the default information for the semester. If
they answer Yes, then save the semester record. To do that, you can change
the value of one of the fields then save the record. Since the subform
control is a control on the Parent form, then the parent doesn't need to be
referred to here. The parent is only referred to from the subform and you
aren't there yet (although it looks like you are). Just changing the value
of one control is enough to make the record dirty so that it can be saved.
The default value of the other controls will them be saved automatically.

Example:
Me.txtMyTextbox = Me.txtMyTextbox.DefaultValue
Me.Dirty = False 'this saves the record

As you can tell from the above, there is a difference between the subform
and the subform control. The subform control is a control on the parent form
that holds a subform. You can actually swap out which form is being used as
a subform just by changing the Source Object property of the subform
control.
 

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