Using Dcount function on subform (1 Viewer)

grenee

Registered User.
Local time
Yesterday, 23:51
Joined
Mar 5, 2012
Messages
210
Good Day all.

My mainform is "CourseDays" and subform is "CoursesSubForm". The "CoursesSubForm" data source is a table "Courses" which contains a field "Subject".

I add a calculated field on the mainform to count the number of records in the subform. This is my code:

=DCount(”[Subject]”,”[CoursesSubForm]”)
An error message results.
Can anyone explain my problem?
Thanks in advance.
 

grenee

Registered User.
Local time
Yesterday, 23:51
Joined
Mar 5, 2012
Messages
210
Thanks for you swift intervention.

My error message is simple "#Error
 

vbaInet

AWF VIP
Local time
Today, 07:51
Joined
Jan 22, 2010
Messages
26,374
My mainform is "CourseDays" and subform is "CoursesSubForm".

=DCount(”[Subject]”,”[CoursesSubForm]”)
This is the format of a DCount() function:
Code:
DCount("[Field]", "[[COLOR="red"][B]Table/Query[/B][/COLOR]]", "Criteria")
As you can see, the second parameter only takes the name of a table or a query and not that of a subform.

If you want to count the number of Subject records there are in the subform and return/display it in your main form, then:
1. place a textbox in the header or footer of your subform. Call it txtCountSubjects
2. enter the expression =Count("[Subject]") into the Control Source
3. place a textbox in the header/footer of your main form
4. in its Control Source use the Expression Builder to locate the txtCountSubjects textbox and the expression should look something like this:
Code:
=[B][COLOR="Blue"]SubformControlName[/COLOR][/B]!txtCountSubjects

OR

=[B][COLOR="blue"]SubformControlName[/COLOR][/B].Form!txtCountSubjects
... note that I'm using a Count function here, not a DCount.
 

grenee

Registered User.
Local time
Yesterday, 23:51
Joined
Mar 5, 2012
Messages
210
Thanks VBAInet.
That was a wonderful Sunday morning treat.

All's well. I can get going now.
 

vbaInet

AWF VIP
Local time
Today, 07:51
Joined
Jan 22, 2010
Messages
26,374
It can't be better than a good Sunday roast... but you said "morning" so you must be in another part of the equator from me ;)
 

murray83

Games Collector
Local time
Today, 07:51
Joined
Mar 31, 2017
Messages
728
This is the format of a DCount() function:
Code:
DCount("[Field]", "[[COLOR="red"][B]Table/Query[/B][/COLOR]]", "Criteria")
As you can see, the second parameter only takes the name of a table or a query and not that of a subform.

If you want to count the number of Subject records there are in the subform and return/display it in your main form, then:
1. place a textbox in the header or footer of your subform. Call it txtCountSubjects
2. enter the expression =Count("[Subject]") into the Control Source
3. place a textbox in the header/footer of your main form
4. in its Control Source use the Expression Builder to locate the txtCountSubjects textbox and the expression should look something like this:
Code:
=[B][COLOR="Blue"]SubformControlName[/COLOR][/B]!txtCountSubjects

OR

=[B][COLOR="blue"]SubformControlName[/COLOR][/B].Form!txtCountSubjects
... note that I'm using a Count function here, not a DCount.

this is exactly what i was looking for and very simply laid out and explained ( which helped ) cheers
 

Users who are viewing this thread

Top Bottom