Subform Issue

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a subform (subDonations) that I want to change based upon a choice in
in an option group (grpReports). I am using the following code, but it isn't
working. Any suggestions?

Thanks.

If Me.grpReports = 1 Then
Me.subDonations.SourceObject = subDonationsByContacts
ElseIf Me.grpReports = 2 Then
Me.subDonations.SourceObject = subDonationsByChurches
ElseIf Me.grpReports = 3 Then
Me.subDonations.SourceObject = subDonationsByForeignContacts
ElseIf Me.grpReports = 4 Then
e.subDonations.SourceObject = subDonationsByForeignChurches
End If
 
"it isn't working" doesn't give us much to go on. assuming that the subform
records are linked to the current mainform record via a primary key/foreign
key value, you'll probably need to reset the value of the following property
as well:

subDonations.LinkChildFields

the value of the LinkMasterFields property should not change, since it is
the primary key field of the parent record.

also requery the subform, as

Me!subDonations.Form.Requery

suggest you consider using a Select Case statement instead of the If
statement, and a separate procedure, to make your code shorter and easier to
maintain, as

Private Sub isChoice(ByVal strSourceObject As String, _
ByVal strChild As String)

With Me!subDonations
.SourceObject = strSourceObject
.LinkChildFields = strChild
.Form.Requery
End With

End Sub

replacing the If statement in your code, with

Select Case Me!grpReports
Case 1
isChoice "subDonationsByContacts", _
"NameOfForeignKeyField"
Case 2
isChoice "subDonationsByChurches", _
"NameOfForeignKeyField"
Case 3
isChoice "subDonationsByForeignContacts", _
"NameOfForeignKeyField"
Case 4
isChoice "subDonationsByForeignChurches", _
"NameOfForeignKeyField"
End Select

substitute the correct "name of the foreign key field", of course, for each
subform.

hth
 
Hi Ed,

Try wrapping the names of the subforms in double quotes. That should clear
up your issue. If not, let me know what event the code is executing under.

Lance
 
When I put in the double quotes, I get the following message:

Run-Time error '3011':

The Microsoft Jet database engine could not find the object
'~sq_crfmDonationsByIndividualMenu~sq_csubDonations'. Make sure the object
exists and that you spell its name and the path name correctly.

Any other ideas?

Thanks again!
 
Hi Ed,

I just noticed that the last elseif statement might have a typo:

It should be Me.subDonations, but it looks like the "M" was dropped off the
Me.

Let me know if that doesn't fix it.
 
Morning Ed,

I did some further looking into the error and it was usually tied to
corruption in the database. If you are still having the problem, maybe make
a back up copy of your database and then try the Compact and Repair option.

Other than that, I am in a loss since your code looks correct. Is there
more code inside the function or sub where this code resides? Maybe set a
break point and step through it and let me know where exactly it is breaking.
 
Dear Ed

I think you will find the problem is in the way the subform is bein
referenced

I suspect the code "Me.subDonations.SourceObject = " should in fact b
"Me.subDonations.Form.SourceObject = ". This is a common mistake, an
due to the peciliarities of the syntax Access uses

Regards

Graham
Ed Shanleywrote
I have a subform (subDonations) that I want to change based upon
choice in
 
i think you're confusing the SourceObject and RecordSource properties. a
form object doesn't have a SourceObject property; it does have a
RecordSource property. a subform control has a SourceObject property, but
not a RecordSource property. the subform control's Form property refers to
the form object properties collection of the form that is contained in the
subform control.

if the name of the subform *control* is subDonations, then the correct
syntax is
Me!subDonations.SourceObject

a common mistake in subform syntax is using the name of the subform as it's
listed in the Forms tab in the database window, instead of using the name of
the subform *control* in the main form. these two Name properties can be the
same value, or different values. to get the name of the subform control:
open the main form in design view. click on the subform (within the main
form design view) to select it. open the Properties box, select the Other
tab, and look at the Name property.

hth
 
Back
Top