First, in reviewing my previous reply, I fogot to state that
literal dates MUST be enclosed in # signs, much like text
must be enclosed in quotes.
Always getting 1 implies that there is never a match on the
Inspection_date field. Either the values in Inspection_date
do not match a date or we made a mistake in the line of
code.
The most likely reason for the date not matching is because
it contains a time component. Check to see if you are
setting Inspection_date to Now() instead of Date(). If
that's the case, then either change how you set the
Inspection_date field or change the expression to:
Me.Sample_no = Nz(DMax("Sample_no", _
"Inspection result_tbl", _
"DateValue(Inspection_date) = " & _
Format(Me.Inspection_date, "\#yyyy-m-d\#")), 0) + 1
If that's not the problem, post back with a Copy/Paste of
the code so I can review it.
--
Marsh
MVP [MS Access]
I think there's a problem with the design of my form.It's still not fixed
yet.
The entry is always 1.
shiro wrote:
Here is the code :
Me.Sample_no = Nz(DMax("Sample_no", "Inspection result_tbl", _
"Inspection_date = " & Me.Inspection_date), 0) + 1
Sample_no data type is number (long integer)
Inspection_date data type is Date/time (medium date)
[] Hope I explain it clearly.
Very clearly and there's the problem ;-)
When you put a date literal into an expression it must be in
an unambiguous style using either / or - as separators.
Because the local settings in Windows can change just about
anything about how a date value is converted to a string,
the only reliable thing you can do is use the Format
function.
I think this is a semi universal example of how to format a
date when concatenating a date value into a string:
Me.Sample_no = Nz(DMax("Sample_no", _
"Inspection result_tbl", "Inspection_date = " & _
Format(Me.Inspection_date, "\#yyyy-m-d\#")), 0) + 1
If you want to see the difference between your and my
expressions, enter the following in the Immediate window:
?"Inspection_date = " & Me.Inspection_date
?"Inspection_date = " & Format(Me.Inspection_date,
"\#yyyy-m-d\#")