Douglas,
I tried to use what you have told me on my today's issue but seems the
scenario is more complex than I can handle.
I have a form having three fields: IDate, report and version. I would like
to automatically generate the version so I try to construct a DMax function
in After update event procedure of report as below:-
Me.Version = Nz(DMax("Version", "Table1", Year([IDate]) = Year([IDate]) AND
Report = Report),0)+1
I do not know how to reference to the values just entered into the
fields as
the criteria. Is it a correct syntax for DMax or it is not inapplicable to
this function?
Thanks,
Scott
As I said in my explanation, strCustomerId is a variable that holds
the
Customer Id for the customer of interest.
What that code is doing is concatenating the value of the Customer Id into
the Where clause. Since it's a text value, it must be enclosed in quotes.
if
strCustomerId was STEE123, you'd end up with:
CustomerID = 'STEE123' And Year([InvoiceDate]) = 2006
It's not a case of "the text data requires enclosed with &, double
quote
and
sigle quote, the numeric data requires enclosed with & and double
quote
and
assume date & time requires none of above."
The & is there strictly for concatenation purposes.
Text data requires quotes. You can use either single or double quotes:
I
could have written that as
"CustomerId = """ & strCustomerId & """ And Year([InvoiceDate]) =
2006"
(to represent a single double quote, you need to put two in a row) or
"CustomerId = " & Chr$(34) & strCustomerId & Chr$(34) & " And
Year([InvoiceDate]) = 2006"
(Chr$(34) represents ")
In fact, there are some issues using a single quote as I did in the
original
example. If the text value contained a single quote in it (say it was
a
name, not an Id, and the name was O'Reilly), you'd run into problems.
I
talk
about this in my May, 2004 "Access Answers" column in Pinnacle
Publication's
"Smart Access". You can download the column (and sample database) for free
at
http://www.accessmvp.com/DJSteele/SmartAccess.html
Numeric data doesn't require quotes. If CustomerId was a numeric
field,
you'd use:
"CustomerId = " & lngCustomerId & " And Year([InvoiceDate]) = 2006"
The quotes are there because you're creating a string.
Dates require # delimiters (as opposed to quotes). As well, the date needs
to be in mm/dd/yyyy format, regardless of what you may have set your Short
Date format to in Regional Settings. (Okay, this isn't strictly true. You
can use any unambiguous format, such as yyyy-mm-dd or dd mmm yyyy. The
point
is, you can't rely on using dd/mm/yyyy format, because Access will assume
it's mm/dd/yyyy and misinterpret it for the first 12 days of each
month.
In
other words, it will always interpret 12/04/2006 as 04 Dec, 2006, not
as
12
April, 2006.)
--
Doug Steele, Microsoft Access MVP
(no e-mails, please!)
Douglas,
Thanks for your explanation.
In your example, it appears to have two criteria:
"CustomerId = '" & strCustomerId & "'
Year([InvoiceDate]) = 2006"
They are connected with AND.
The 2nd one is easy to understand while I don't understand the 1st
one.
Is CustomerId a field name? What does the condition of strCustomerId
mean?
Why does the condition is enclosed with &, double quote and sigle quote?
According to your explanation below, the text data requires enclosed with
&,
double quote and sigle quote, the numeric data requires enclosed with
&
and
double quote and assume date & time requires none of above.
Thanks,
Scott
message
The criteria for DMax (and for all the other domain aggregate
functions,
for
that matter) should like like a WHERE clause without the word WHERE in
front
of it. The following assumes that CustomerId is a text field, and that
the
customer id of interest is stored in variable strCustomerId. If
CustomerId
was numeric, you would remove the single quotes in the string:
DMax("InvoiceAmount", "MyTable", "CustomerId = '" & strCustomerId & "'
And
Year([InvoiceDate]) = 2006")
--
Doug Steele, Microsoft Access MVP
(no e-mails, please!)
I need to use DMax() function in event procedures but do not
understand
the
syntax of criteria part especially with two criteria. Can someone
explain
it to me.
Thanks,
Scott