Dcount in report (1 Viewer)

Sipozove

New member
Local time
Today, 03:19
Joined
Mar 23, 2022
Messages
9
I'm trying to use a report for the first time.
I want it to display some statistics.

I can do just fine in VBA though a button which output a message.

Code:
Case1 = Nz(DCount("*", "[tbl2022]", "[Field7] = '" & Replace("AAA", "'", "''") & "' "), 0)
Case2 = Nz(DCount("*", "[tbl2022]", "[Field7] = '" & Replace("BBB", "'", "''") & "' "), 0)

The popup message was a quick and effective output in the beginning.
Not anymore. :)

How can I create a report that displays the same information?
 

GPGeorge

Grover Park George
Local time
Yesterday, 18:19
Joined
Nov 25, 2004
Messages
1,855
Create a query that displays the two values you need, i.e. Case1 and Case2. I have a hard time, though, understanding the syntax there.

Use that query as the recordsource for the report.

Create two text box controls on the report. Bind one to the field "Case1" and one to the field "Case2".

There are a couple of additional concerns, though, suggested by the table and field names shown.
"tbl2022" suggests you are planning to segregate data in different tables by year. That's a problem waiting to spring itself on you next year. More than that, it offers NO clue as to what data is in that table.

"Field7" ? Why not name it for the contents in it? Accepting generic names like that makes future maintenance a nightmare.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:19
Joined
May 7, 2009
Messages
19,231
add an 2 unbound textbox to your report.
the ControlSource of one is:

=Nz(DCount("*", "[tbl2022]", "[Field7] = '" & Replace("AAA", "'", "''") & "' "), 0)

the ControlSource of the other:

=Nz(DCount("*", "[tbl2022]", "[Field7] = '" & Replace("BBB", "'", "''") & "' "), 0)
 

plog

Banishment Pending
Local time
Yesterday, 20:19
Joined
May 11, 2011
Messages
11,645
DCount will not return null, no need for NZ()
 

Sipozove

New member
Local time
Today, 03:19
Joined
Mar 23, 2022
Messages
9
Create a query that displays the two values you need, i.e. Case1 and Case2. I have a hard time, though, understanding the syntax there.

Use that query as the recordsource for the report.

Create two text box controls on the report. Bind one to the field "Case1" and one to the field "Case2".

There are a couple of additional concerns, though, suggested by the table and field names shown.
"tbl2022" suggests you are planning to segregate data in different tables by year. That's a problem waiting to spring itself on you next year. More than that, it offers NO clue as to what data is in that table.

"Field7" ? Why not name it for the contents in it? Accepting generic names like that makes future maintenance a nightmare.
George,

Thank you for your concern.
This table is needed because of an operation happening this year. i don't plan to create any table each year.
The names of the fields are something I have to change but doing it carefully ;-)

I thought about creating a query but I didn't want to do ti "just for that". I'll look into it.
 

Sipozove

New member
Local time
Today, 03:19
Joined
Mar 23, 2022
Messages
9
add an 2 unbound textbox to your report.
the ControlSource of one is:

=Nz(DCount("*", "[tbl2022]", "[Field7] = '" & Replace("AAA", "'", "''") & "' "), 0)

the ControlSource of the other:

=Nz(DCount("*", "[tbl2022]", "[Field7] = '" & Replace("BBB", "'", "''") & "' "), 0)
arnelgp,

I get this error: "The Expression you entered contains invalid syntax"
It works in a form but not in a report.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:19
Joined
May 7, 2009
Messages
19,231
actually you dont need to use Replace since there is no single quote on your Expression:

=DCount("*", "[tbl2022]", "[Field7] = 'AAA'")

=DCount("*", "[tbl2022]", "[Field7] = 'BBB'")
 

Sipozove

New member
Local time
Today, 03:19
Joined
Mar 23, 2022
Messages
9
I just broke something... :eek:
I'll come back to you...
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 21:19
Joined
Feb 19, 2002
Messages
43,260
This table is needed because of an operation happening this year. i don't plan to create any table each year.
Could you have a similar need in the future? If so, include a field with the date and use more generic names. Do NOT "hard code" things you generalize.

Think of this advice as "defensive programming".
 

Users who are viewing this thread

Top Bottom