473,419 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

dcount syntax

hey.. i have a control in a report that needs to count the records from a
table that match the parameters that the user selects when creating the
report. the report is created with formquerybuilder and the parameters are
department, upsource, and date. i can't get the syntax down for this dcount:

=DCount("*","tblcontactdata","[date] Between #" & [txtboxstartdate] & "# And
#" & [txtboxenddate] & "#" & "' AND '" & "'[upsource]='" & "'forms!
formquerybuilder!cboxupsource'" & "' AND '" & "'[department]='" & "'forms!
formquerybuilder!cboxdepartment'")

what have i got wrong?

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200511/1
Nov 30 '05 #1
7 6335
Try this instead:
=DCount("*","tblcontactdata","[date] Between #" & [txtboxstartdate] &
"# And #" & [txtboxenddate] & "# AND [upsource]='" &
forms!formquerybuilder!cboxupsource &
"' AND [department]='" forms!formquerybuilder!cboxdepartment & "'")

Some notes about figuring out the proper syntax:
1. Date values must be wrapped with # signs, which you did properly.
ex: [date] = #01/01/2005#
2. Text values must be wrapped with ' signs (single quotes).
ex: [upsource] = 'my source'
You had single quotes around the field names [upsource] and
[department] which were unnecessary, as well as around the AND after
the txtboxenddate.
3. Numerical values do NOT require any wrapping.
ex: [MyNumber] = 12
4. When referring to the value of a field on a form or report, do
not put single quotes around them, or the function will put the text
within the quotes rather than the value of the field.
ex: If the cboxupsource field has text in it:
"[upsource] = '" & forms!fromquerybuilder!cboxupsource & "'"
(note the placement of the single quotes and double quotes)

Hope that helps,
Jana

If the cboxupsource field as a number in it:
"[upsource] = " & forms!formquerybuilder!cboxupsource

If the cboxupsource field has a date in it:
"[upsource] = #" & forms!formquerybuilder!cboxupsource & "#"

Nov 30 '05 #2
sorry, got my name in the middle of that last part...

Nov 30 '05 #3
Jana,
hrm. i almost have it working. this is my new expression:

=DCount("*","tblcontactdata","[date] Between #" & [txtboxstartdate] & "# And
#" & [txtboxenddate] & "# AND [upsource]=" & Chr(34) & Forms!formquerybuilder!
cboxupsource & Chr(34) & " AND [department]=" & Chr(34) & Forms!
formquerybuilder!cboxdepartment & Chr(34))

first, let me say that my use of chr(34) instead of the single-quote method
is not a reflection on you or your answer, but just lets me see more clearly.
this is something i saw in other posts while looking for answers to my
problem. to me, '" looks too close to "' for my taste.

anyhow, it seems to work fine. except that (and i forgot to mention this
earlier) these fields may be null (only the start and end dates are required).
the form that creates the report doesn't NEED the user to specify a
department. if no department is specified, the report runs on a combined
department total. how can i get this to work with an idea similar to:

iif(isnull(forms!formquerybuilder!cboxdepartment=t rue),"","[department]=" &
Chr(34) & Forms!formquerybuilder!cboxdepartment & Chr(34) &)

that way, if the field is null, the expression would look like this:

[date] between #10/1/05# and #11/30/05# and ""

the idea being that concatenating the where clause with "" will do nothing
but if the department IS specified, then it would look like this:

[date] between #10/1/05# and #11/30/05# and [department]="Electronics"

the same would go for upsource as well. i hope i explained everything so it
makes sense.

Jana wrote:
Try this instead:
=DCount("*","tblcontactdata","[date] Between #" & [txtboxstartdate] &
"# And #" & [txtboxenddate] & "# AND [upsource]='" &
forms!formquerybuilder!cboxupsource &
"' AND [department]='" forms!formquerybuilder!cboxdepartment & "'")

Some notes about figuring out the proper syntax:
1. Date values must be wrapped with # signs, which you did properly.
ex: [date] = #01/01/2005#
2. Text values must be wrapped with ' signs (single quotes).
ex: [upsource] = 'my source'
You had single quotes around the field names [upsource] and
[department] which were unnecessary, as well as around the AND after
the txtboxenddate.
3. Numerical values do NOT require any wrapping.
ex: [MyNumber] = 12
4. When referring to the value of a field on a form or report, do
not put single quotes around them, or the function will put the text
within the quotes rather than the value of the field.
ex: If the cboxupsource field has text in it:
"[upsource] = '" & forms!fromquerybuilder!cboxupsource & "'"
(note the placement of the single quotes and double quotes)

Hope that helps,
Jana

If the cboxupsource field as a number in it:
"[upsource] = " & forms!formquerybuilder!cboxupsource

If the cboxupsource field has a date in it:
"[upsource] = #" & forms!formquerybuilder!cboxupsource & "#"


--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200511/1
Nov 30 '05 #4
No offense taken, it was a point worth making :)

Let's try this instead:

=DCount("*","tblcontactdata","[date] Between #" &
[txtboxstartdate] & "# And #" & [txtboxenddate] & "#" &
IIF(IsNull(Forms!formquerybuilder!cboxupsource) = True, "",
" AND [upsource]=" & Chr(34) & Forms!formquerybuilder!cboxupsource &
Chr(34)) &
IIF(IsNull(Forms!formquerybuilder!cboxdepartment) = True, "",
" AND [department]=" & Chr(34) & Forms!formquerybuilder!cboxdepartment
& Chr(34)))

Rather than worrying about whether the Where clause will like it if it
ends in And "", why not make that part of the conditional text? That's
what I've done here. Give it a shot, let me know how it works out.

Good luck,
Jana

Dec 1 '05 #5
Also, you might need to use Chr(39), which is a single quote, rather
than Chr(34) which is a double quote. I'm not sure if that will make a
difference, though.

Jana

Dec 1 '05 #6
it worked perfectly. this was the final expression:

=DCount("*","tblcontactdata","[date] Between #" & [txtboxstartdate] & "# And
#" & [txtboxenddate] & "#" & IIf(IsNull(Forms!formquerybuilder!cboxupsource)
=True,""," AND [upsource]=" & Chr(34) & Forms!formquerybuilder!cboxupsource &
Chr(34)) & IIf(IsNull(Forms!formquerybuilder!cboxdepartment)= True,""," AND
[department]=" & Chr(34) & Forms!formquerybuilder!cboxdepartment & Chr(34)))

my only question is, and this is just for my own curiosity, why do people in
the know (you and others on this and other forums) use And, AND + & as if
they had different uses at different times. for example, you have:

....[txtboxstartdate] & "# And #" & [txtboxenddate]...

in one spot and:

....)=True,""," AND [upsource]=" & Chr...

in another. not to mention the ampersands all over the place. i mean, i
know we need them to get the expression right but why choose one type over
the other? or is there a difference? anyhow, thanks for all of your help.

Jana wrote:
Also, you might need to use Chr(39), which is a single quote, rather
than Chr(34) which is a double quote. I'm not sure if that will make a
difference, though.

Jana


--
Message posted via http://www.accessmonster.com
Dec 2 '05 #7
Here's the deal with AND vs. And vs. &:
There is no difference between AND and And, we just use the uppercase
convention within an SQL statement to indicate that it is a reserved
word in the SQL language. SQL is not case specific, so lowercase "and"
would work, too! So, a typical SQL would be written like this: "SELECT
* FROM tblMyTable WHERE fldMyField = "blah" AND fldMyOtherField = "blah
blah", even though "select * from tblmytable where fldmyfield = "blah"
and fldmyotherfield = "blah blah" would work just as easily. As you
can see, using the uppercase convention for reserved words makes it
easier to read an SQL statement.

The & is used for string concatenation. So, when you want to string
together a bunch of pieces of text into one string, you would do
something like this:
MyString = "This is " & "my string concatenation " & "example!"
This stores into the variable MyString "This is my string concatenation
example!" We had to use the & in your item because we were using the
IIF function to determine what text to add to your criteria for your
query because we were building the statement on the fly rather than
having criteria that was static. Here's another example that uses some
logic (in Visual Basic) to determine the contents of a string:
MyString = "This person is "
If MyAge < 21
MyString = MyString & "a minor."
Else
MyString = MyString & "over 21."
End If

Hope that takes some of the mystery out of it :)

Jana

Dec 6 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Simon Matthews | last post by:
Hope someone can help an Access beginner! I've just started keeping my surgical logbook on access and it's a simple flat-file affair. I have created several queries that will list cases...
15
by: sara | last post by:
Hi I'm pretty new to Access here (using Access 2000), and appreciate the help and instruction. I gave myself 2.5 hours to research online and help and try to get this one, and I am not getting...
1
by: Geoff | last post by:
FrmBookings is a subform linked to the form FrmParty. The DCount function checks for previous bookings and it works fine but why do I need such a long-winded syntax ie Forms!.Form!! in the...
2
by: Wingz | last post by:
Hiya, Fairly new to Access and was wondering what the best way to perform Dcounts on groups in an Access report. For example, I have 10 employees and the different instances of jobs they can...
1
by: weety | last post by:
Hi =DCount("*","qryBc003_2_Xstats") The above syntax which was working for old tables is not working for new tables or queries , I have updated the changes the new query is working fine with...
7
by: Michael R | last post by:
Good afternoon. I'm stucked in composing the syntax for DCount expression in a select query. The query qryCustomers has CustomerID field, the DCount function uses tblLoans with LoanDate and Id fields...
3
by: davidwelli | last post by:
Hello, All help is appreciated, I'm struggling with the following. Using Access 2003 (front end) connecting to Oracle 7 database (back end). I'm trying to create a query in access that will...
5
MGrowneyARSI
by: MGrowneyARSI | last post by:
Hello I'm having some truble with some dcounts that I am useing to see if I want to show 2 buttons or not the dcountss are looking at two querrys that are limited to records were a bolean field is...
4
by: 6afraidbecause789 | last post by:
I was able to glean syntax online for a DCount on a form that counts the number of 'misbehavior' incidents for students for the current day: =DCount("StudentID","Incidents","StudentID=" & Forms!...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.