us date format

B

Bob Wickham

Hi,

I have a form where the user enters a date in each record.
In many records the date stays the same so I have written the following
code that inserts the previous records value into the current record.

Private Sub PaymentDate_AfterUpdate()

Me![PaymentDate].DefaultValue = "#" & Me![PaymentDate] & "#"

End Sub

Works OK, except when the value is inserted it displays as US date mm/dd/yy.
I'm typing dd/mm/yy in the first record
I live in Australia and want it to appear as dd/mm/yy in the next record.

I believe I need to convert the date to a number with CLng but I've
tried all sorts of ways and cant get it to happen.

Any ideas.
Thanks.

Bob
 
M

Marshall Barton

Bob said:
I have a form where the user enters a date in each record.
In many records the date stays the same so I have written the following
code that inserts the previous records value into the current record.

Private Sub PaymentDate_AfterUpdate()

Me![PaymentDate].DefaultValue = "#" & Me![PaymentDate] & "#"

End Sub

Works OK, except when the value is inserted it displays as US date mm/dd/yy.
I'm typing dd/mm/yy in the first record
I live in Australia and want it to appear as dd/mm/yy in the next record.

I believe I need to convert the date to a number with CLng but I've
tried all sorts of ways and cant get it to happen.


I think all you need to do is set the text box's Format
property to:
dd/mm/yy
 
B

Bob Wickham

Marshall said:
Bob said:
I have a form where the user enters a date in each record.
In many records the date stays the same so I have written the following
code that inserts the previous records value into the current record.

Private Sub PaymentDate_AfterUpdate()

Me![PaymentDate].DefaultValue = "#" & Me![PaymentDate] & "#"

End Sub

Works OK, except when the value is inserted it displays as US date mm/dd/yy.
I'm typing dd/mm/yy in the first record
I live in Australia and want it to appear as dd/mm/yy in the next record.

I believe I need to convert the date to a number with CLng but I've
tried all sorts of ways and cant get it to happen.



I think all you need to do is set the text box's Format
property to:
dd/mm/yy
No, sorry Marshall, that didn't make any difference.
The format property was Short Date and I have now changed it dd/mm/yy

In both cases, entering 12/06/05 (the 12th day of June, 2005)in the
first record displays as 06/12/05 in the second.

Bob
 
R

RoyVidar

Bob Wickham wrote in message said:
Marshall said:
Bob said:
I have a form where the user enters a date in each record.
In many records the date stays the same so I have written the following
code that inserts the previous records value into the current record.

Private Sub PaymentDate_AfterUpdate()

Me![PaymentDate].DefaultValue = "#" & Me![PaymentDate] & "#"

End Sub

Works OK, except when the value is inserted it displays as US date
mm/dd/yy.
I'm typing dd/mm/yy in the first record
I live in Australia and want it to appear as dd/mm/yy in the next record.

I believe I need to convert the date to a number with CLng but I've tried
all sorts of ways and cant get it to happen.



I think all you need to do is set the text box's Format
property to:
dd/mm/yy
No, sorry Marshall, that didn't make any difference.
The format property was Short Date and I have now changed it dd/mm/yy

In both cases, entering 12/06/05 (the 12th day of June, 2005)in the first
record displays as 06/12/05 in the second.

Bob

You need even one step further (watch for linebreaks)

Me![PaymentDate].DefaultValue = "#" & format$(Me![PaymentDate],
"yyyy-mm-dd") & "#"

or check out this page by Allen Browne
http://allenbrowne.com/ser-36.html
for more information
 
B

Bob Wickham

RoyVidar said:
Bob Wickham wrote in message said:
Marshall said:
Bob Wickham wrote:

I have a form where the user enters a date in each record.
In many records the date stays the same so I have written the
following code that inserts the previous records value into the
current record.

Private Sub PaymentDate_AfterUpdate()

Me![PaymentDate].DefaultValue = "#" & Me![PaymentDate] & "#"

End Sub

Works OK, except when the value is inserted it displays as US date
mm/dd/yy.
I'm typing dd/mm/yy in the first record
I live in Australia and want it to appear as dd/mm/yy in the next
record.

I believe I need to convert the date to a number with CLng but I've
tried all sorts of ways and cant get it to happen.




I think all you need to do is set the text box's Format
property to:
dd/mm/yy
No, sorry Marshall, that didn't make any difference.
The format property was Short Date and I have now changed it dd/mm/yy

In both cases, entering 12/06/05 (the 12th day of June, 2005)in the
first record displays as 06/12/05 in the second.

Bob


You need even one step further (watch for linebreaks)

Me![PaymentDate].DefaultValue = "#" & format$(Me![PaymentDate],
"yyyy-mm-dd") & "#"

or check out this page by Allen Browne
http://allenbrowne.com/ser-36.html
for more information
Thanks Roy and Marshall,

That fixed it.
Info for anybody else confused about all this.
This code: (without line breaks)
Me![PaymentDate].DefaultValue = "#" & Format$(Me![PaymentDate],
"yyyy-mm-dd") & "#"
will display dates according to the text boxes Format property, as
Marshall suggested.
In my case:
dd/mm/yy will display 12/06/05 (12th day of June, 05)
Short Date will display according to your regional settings eg
dd/mm/yyyy (12/06/2005) (12th day of June, 2005)

Yes, it works, but, I have read several posts (notably Steve Schapel)
suggesting that dates be converted using CLng so as to eliminate any
confusion about all the different date formats.
Can anyone explain how I would use CLng with my current problem.

Bob
 
R

RoyVidar

Bob Wickham wrote in message said:
Thanks Roy and Marshall,

That fixed it.
Info for anybody else confused about all this.
This code: (without line breaks)
Me![PaymentDate].DefaultValue = "#" & Format$(Me![PaymentDate],
"yyyy-mm-dd") & "#"
will display dates according to the text boxes Format property, as Marshall
suggested.
In my case:
dd/mm/yy will display 12/06/05 (12th day of June, 05)
Short Date will display according to your regional settings eg dd/mm/yyyy
(12/06/2005) (12th day of June, 2005)

Yes, it works, but, I have read several posts (notably Steve Schapel)
suggesting that dates be converted using CLng so as to eliminate any
confusion about all the different date formats.
Can anyone explain how I would use CLng with my current problem.

Bob

Number formatting scheme - well, I'm not the best to explain that,
since
I don't like using that technique. I'm not sure formatting to long will
work in this scenario, and I'm not sure how it would work against other
databases, for instance SQL server or Oracle ...

I'm happy enough with the knowledge that whenever passing information
as
a string - and that's probably the crucial point - as a string - one
need to pass it in an unambiguous format. I will prefer the suggested
format (or use methods that utilize the parameters collection of the
method one uses to execute queries;-)).
 
B

Bob Wickham

RoyVidar said:
Bob Wickham wrote in message said:
Thanks Roy and Marshall,

That fixed it.
Info for anybody else confused about all this.
This code: (without line breaks)
Me![PaymentDate].DefaultValue = "#" & Format$(Me![PaymentDate],
"yyyy-mm-dd") & "#"
will display dates according to the text boxes Format property, as
Marshall suggested.
In my case:
dd/mm/yy will display 12/06/05 (12th day of June, 05)
Short Date will display according to your regional settings eg
dd/mm/yyyy (12/06/2005) (12th day of June, 2005)

Yes, it works, but, I have read several posts (notably Steve Schapel)
suggesting that dates be converted using CLng so as to eliminate any
confusion about all the different date formats.
Can anyone explain how I would use CLng with my current problem.

Bob


Number formatting scheme - well, I'm not the best to explain that, since
I don't like using that technique. I'm not sure formatting to long will
work in this scenario, and I'm not sure how it would work against other
databases, for instance SQL server or Oracle ...

I'm happy enough with the knowledge that whenever passing information as
a string - and that's probably the crucial point - as a string - one
need to pass it in an unambiguous format. I will prefer the suggested
format (or use methods that utilize the parameters collection of the
method one uses to execute queries;-)).
Do you think that dates converted with CLng may not be recognised if I
was to upsize my Access db to SQL Server
 
R

RoyVidar

Bob Wickham wrote in message said:
RoyVidar said:
Bob Wickham wrote in message said:
Thanks Roy and Marshall,

That fixed it.
Info for anybody else confused about all this.
This code: (without line breaks)
Me![PaymentDate].DefaultValue = "#" & Format$(Me![PaymentDate],
"yyyy-mm-dd") & "#"
will display dates according to the text boxes Format property, as
Marshall suggested.
In my case:
dd/mm/yy will display 12/06/05 (12th day of June, 05)
Short Date will display according to your regional settings eg dd/mm/yyyy
(12/06/2005) (12th day of June, 2005)

Yes, it works, but, I have read several posts (notably Steve Schapel)
suggesting that dates be converted using CLng so as to eliminate any
confusion about all the different date formats.
Can anyone explain how I would use CLng with my current problem.

Bob


Number formatting scheme - well, I'm not the best to explain that, since
I don't like using that technique. I'm not sure formatting to long will
work in this scenario, and I'm not sure how it would work against other
databases, for instance SQL server or Oracle ...

I'm happy enough with the knowledge that whenever passing information as
a string - and that's probably the crucial point - as a string - one
need to pass it in an unambiguous format. I will prefer the suggested
format (or use methods that utilize the parameters collection of the
method one uses to execute queries;-)).
Do you think that dates converted with CLng may not be recognised if I was to
upsize my Access db to SQL Server

I think that might depend on whether you executed it on linked tables
(where it would proably give correct date) or on a connection to SQL
server (where it might be a day or two off)
 
B

Bob Wickham

RoyVidar said:
Bob Wickham wrote in message said:
RoyVidar said:
Bob Wickham wrote in message <[email protected]> :

Thanks Roy and Marshall,

That fixed it.
Info for anybody else confused about all this.
This code: (without line breaks)
Me![PaymentDate].DefaultValue = "#" & Format$(Me![PaymentDate],
"yyyy-mm-dd") & "#"
will display dates according to the text boxes Format property, as
Marshall suggested.
In my case:
dd/mm/yy will display 12/06/05 (12th day of June, 05)
Short Date will display according to your regional settings eg
dd/mm/yyyy (12/06/2005) (12th day of June, 2005)

Yes, it works, but, I have read several posts (notably Steve
Schapel) suggesting that dates be converted using CLng so as to
eliminate any confusion about all the different date formats.
Can anyone explain how I would use CLng with my current problem.

Bob



Number formatting scheme - well, I'm not the best to explain that, since
I don't like using that technique. I'm not sure formatting to long will
work in this scenario, and I'm not sure how it would work against other
databases, for instance SQL server or Oracle ...

I'm happy enough with the knowledge that whenever passing information as
a string - and that's probably the crucial point - as a string - one
need to pass it in an unambiguous format. I will prefer the suggested
format (or use methods that utilize the parameters collection of the
method one uses to execute queries;-)).
Do you think that dates converted with CLng may not be recognised if I
was to upsize my Access db to SQL Server


I think that might depend on whether you executed it on linked tables
(where it would proably give correct date) or on a connection to SQL
server (where it might be a day or two off)
Thanks Roy,
There is a chance I will have to upsize my db sometime in the future, so
I'll see how I go.

Bob Wickham
 
M

Marshall Barton

Bob said:
RoyVidar said:
Bob Wickham wrote in message said:
Thanks Roy and Marshall,

That fixed it.
Info for anybody else confused about all this.
This code: (without line breaks)
Me![PaymentDate].DefaultValue = "#" & Format$(Me![PaymentDate],
"yyyy-mm-dd") & "#"
will display dates according to the text boxes Format property, as
Marshall suggested.
In my case:
dd/mm/yy will display 12/06/05 (12th day of June, 05)
Short Date will display according to your regional settings eg
dd/mm/yyyy (12/06/2005) (12th day of June, 2005)

Yes, it works, but, I have read several posts (notably Steve Schapel)
suggesting that dates be converted using CLng so as to eliminate any
confusion about all the different date formats.
Can anyone explain how I would use CLng with my current problem.


Number formatting scheme - well, I'm not the best to explain that, since
I don't like using that technique. I'm not sure formatting to long will
work in this scenario, and I'm not sure how it would work against other
databases, for instance SQL server or Oracle ...

I'm happy enough with the knowledge that whenever passing information as
a string - and that's probably the crucial point - as a string - one
need to pass it in an unambiguous format. I will prefer the suggested
format (or use methods that utilize the parameters collection of the
method one uses to execute queries;-)).
Do you think that dates converted with CLng may not be recognised if I
was to upsize my Access db to SQL Server


I don't know the context where Steve recommended using CLng,
but the only reason I can think of is to drop the Time part
of a date value. Not to say Steve didn't have something
clever in mind, but I do not see how it can help in a
situation inolving the Default Value.

Generally, you are much safer using standard date functions
to manipulate date values. For string conversions, as Roy
stated, the Format function can convert a date value to a
string just about any way you might want. If you have a
string that looks like a date, CDate will convert it using
your regional settings unless you append the # signs. This
is also true of implicit conversions such as when a user
enters a date in a text box.
 
D

Douglas J. Steele

Marshall Barton said:
Bob Wickham wrote:

I don't know the context where Steve recommended using CLng,
but the only reason I can think of is to drop the Time part
of a date value. Not to say Steve didn't have something
clever in mind, but I do not see how it can help in a
situation inolving the Default Value.

I believe what Steve may have been suggesting is that you can use:

Me![PaymentDate].DefaultValue = CLng(Me![PaymentDate])

rather than

Me![PaymentDate].DefaultValue = "#" & Format$(Me![PaymentDate],
"yyyy-mm-dd") & "#"
 
B

Bob Wickham

Douglas said:
Bob Wickham wrote:

I don't know the context where Steve recommended using CLng,
but the only reason I can think of is to drop the Time part
of a date value. Not to say Steve didn't have something
clever in mind, but I do not see how it can help in a
situation inolving the Default Value.


I believe what Steve may have been suggesting is that you can use:

Me![PaymentDate].DefaultValue = CLng(Me![PaymentDate])

rather than

Me![PaymentDate].DefaultValue = "#" & Format$(Me![PaymentDate],
"yyyy-mm-dd") & "#"
Doug,
The "Steve" suggestion I was referring to was in response to a question
asked by you back in October (Steves reply below).

I am wondering if there is a right/wrong or good/bad way to do it or is
like the "discussions" I have with my father-in-law about the pros and
cons of PC's and Mac. ie. It depends on what you need.

Bob Wickham

Steve Schapel
Oct 27 2005, 2:32 am show options
Newsgroups: microsoft.public.access.formscoding
From: Steve Schapel <[email protected]> - Find messages by this author
Date: Thu, 27 Oct 2005 04:32:00 +1300
Subject: Re: Insert Into and automatic record entry
Reply to Author | Forward | Print | Individual Message | Show original |
Report Abuse

Thanks, Doug. No, NewDate is a number. That's why I *always* use
CLng() to handle dates in code, because it will *always* be right,
regardless of formats and regional settings. If you're using an Append
Query to insert 38652 into a date field, then you must get today's date
whether you're in Brazil or Timbuktu.

Common example: A couple of unbound textboxes for entry of a date range
for query criteria. My SQL in code will always be like this...
"WHERE MyDateField Between " & CLng(Nz(Me.Start, Me.End)) & " And " &
CLng(Me.End)
So, the entries in the unbound textboxes would (for me) normally be in
dd-mm-yy format, and similarly the date in the field. The above
approach always gets it right. Silly, probably, but I somehow don't
trust the Format() approach to always get it right... and even if it
did, I find the syntax more difficult to use.

Of course, I was also just trying to make it simpler for Jeff, who is
probably now more confused than ever :)
 
M

Marshall Barton

Douglas said:
Bob Wickham wrote:

I don't know the context where Steve recommended using CLng,
but the only reason I can think of is to drop the Time part
of a date value. Not to say Steve didn't have something
clever in mind, but I do not see how it can help in a
situation inolving the Default Value.

I believe what Steve may have been suggesting is that you can use:

Me![PaymentDate].DefaultValue = CLng(Me![PaymentDate])

rather than

Me![PaymentDate].DefaultValue = "#" & Format$(Me![PaymentDate],
"yyyy-mm-dd") & "#"


I never thought of that. It is clever, but it does rely on
Access to convert from a long to a date. While I don't
really have a problem with that, it is not in the slightest
portable to other contexts that don't provide the same
implicit conversions.

Based on that thinking, I wouldn't trust it in SQL Server
SQL statements without seeing some definitive documentation
to that effect.
 
D

Douglas J. Steele

Marshall Barton said:
Douglas said:
Bob Wickham wrote:

I don't know the context where Steve recommended using CLng,
but the only reason I can think of is to drop the Time part
of a date value. Not to say Steve didn't have something
clever in mind, but I do not see how it can help in a
situation inolving the Default Value.

I believe what Steve may have been suggesting is that you can use:

Me![PaymentDate].DefaultValue = CLng(Me![PaymentDate])

rather than

Me![PaymentDate].DefaultValue = "#" & Format$(Me![PaymentDate],
"yyyy-mm-dd") & "#"


I never thought of that. It is clever, but it does rely on
Access to convert from a long to a date. While I don't
really have a problem with that, it is not in the slightest
portable to other contexts that don't provide the same
implicit conversions.

I don't really understand this comment, Marsh.

I don't see any reliance on converting from a long to a date. Dates ARE
numbers, after all. You can use the numeric equivalent of the date anywhere.
Today (22 Jan, 2006) is 38739 days since 30 Dec, 1899, so CLng(Date) will
give you 38739:

?CLng(Date)
38739

You can add, say, 31 to that to get 38970 and then format that as a date,
and you'll get 22 Feb, 2006:

?38739 + 31
38770
?Format(38770, "dd mmm yyyy")
22 Feb 2006

I don't feel it even relies on knowing that dates start at 30 Dec, 1899: 0
could represent any date, and this approach would still work in Access.

Based on that thinking, I wouldn't trust it in SQL Server
SQL statements without seeing some definitive documentation
to that effect.

I don't disagree with this statement: I believe SQL Server uses a different
start date (so that 0 isn't 30 Dec, 1899, like it is in Access and Excel),
but I'm not certain about that. However, even if your front-end is linked to
SQL Server, there's no reason why you can't use this "trick" to set the
default value for a field. If Access is doing translations when
communicating with SQL Server, it'll do the same translations in either
case.
 
D

Douglas J. Steele

Bob Wickham said:
The "Steve" suggestion I was referring to was in response to a question
asked by you back in October (Steves reply below).

I am wondering if there is a right/wrong or good/bad way to do it or is
like the "discussions" I have with my father-in-law about the pros and
cons of PC's and Mac. ie. It depends on what you need.

Whichever way gives you the correct answer is the "right" way. <g>

Take a look at the discussion I had with Marsh elsewhere in this thread. I
don't see that one approach is better than the other. Yes, the CLng approach
assumes that dates are stored as numbers, and if that ever changed then this
wouldn't work, but I can't see such a fundamental thing changing.

Personally, I always use the Format approach (Format(MyDate,
"\#mm\/dd\/yyyy\#") rather than the CLng approach, but I would think that
one function call is just as "expensive" as the other. It's possible that
the CLng approach is actually slight more efficient, since Access has to
convert the string version of the date returned by the Format function to a
date.
 
M

Marshall Barton

Douglas said:
Douglas said:
Bob Wickham wrote:

I don't know the context where Steve recommended using CLng,
but the only reason I can think of is to drop the Time part
of a date value. Not to say Steve didn't have something
clever in mind, but I do not see how it can help in a
situation inolving the Default Value.

I believe what Steve may have been suggesting is that you can use:

Me![PaymentDate].DefaultValue = CLng(Me![PaymentDate])

rather than

Me![PaymentDate].DefaultValue = "#" & Format$(Me![PaymentDate],
"yyyy-mm-dd") & "#"


I never thought of that. It is clever, but it does rely on
Access to convert from a long to a date. While I don't
really have a problem with that, it is not in the slightest
portable to other contexts that don't provide the same
implicit conversions.

I don't really understand this comment, Marsh.

I don't see any reliance on converting from a long to a date. Dates ARE
numbers, after all. You can use the numeric equivalent of the date anywhere.
Today (22 Jan, 2006) is 38739 days since 30 Dec, 1899, so CLng(Date) will
give you 38739:

?CLng(Date)
38739

You can add, say, 31 to that to get 38970 and then format that as a date,
and you'll get 22 Feb, 2006:

?38739 + 31
38770
?Format(38770, "dd mmm yyyy")
22 Feb 2006

I don't feel it even relies on knowing that dates start at 30 Dec, 1899: 0
could represent any date, and this approach would still work in Access.

Based on that thinking, I wouldn't trust it in SQL Server
SQL statements without seeing some definitive documentation
to that effect.

I don't disagree with this statement: I believe SQL Server uses a different
start date (so that 0 isn't 30 Dec, 1899, like it is in Access and Excel),
but I'm not certain about that. However, even if your front-end is linked to
SQL Server, there's no reason why you can't use this "trick" to set the
default value for a field. If Access is doing translations when
communicating with SQL Server, it'll do the same translations in either
case.


I agree that this will work as long as the conversions are
done in Access.

What I am questioning is a situation where the long value is
saved to a table and then processed by another system, e.g.
Excel and SQL Server. If/When the long values is then used
in that context, it could generate wrong dates or require
the programmer to be aware of the situation and to program
Access's zero date in the other system. As long as the
programmer is totally aware of the issues with dates, the
Long value dates would not be stored to tables and the issue
would not arise.

I would not have brought up this extrapolation of Bob's
situation, if Bob had not stated repeatedly that he expected
to have to upsize his application.
 
R

RoyVidar

Marshall Barton wrote in message
Douglas said:
Douglas J. Steele wrote:

Bob Wickham wrote:

I don't know the context where Steve recommended using CLng,
but the only reason I can think of is to drop the Time part
of a date value. Not to say Steve didn't have something
clever in mind, but I do not see how it can help in a
situation inolving the Default Value.

I believe what Steve may have been suggesting is that you can use:

Me![PaymentDate].DefaultValue = CLng(Me![PaymentDate])

rather than

Me![PaymentDate].DefaultValue = "#" & Format$(Me![PaymentDate],
"yyyy-mm-dd") & "#"


I never thought of that. It is clever, but it does rely on
Access to convert from a long to a date. While I don't
really have a problem with that, it is not in the slightest
portable to other contexts that don't provide the same
implicit conversions.

I don't really understand this comment, Marsh.

I don't see any reliance on converting from a long to a date. Dates ARE
numbers, after all. You can use the numeric equivalent of the date anywhere.
Today (22 Jan, 2006) is 38739 days since 30 Dec, 1899, so CLng(Date) will
give you 38739:

?CLng(Date)
38739

You can add, say, 31 to that to get 38970 and then format that as a date,
and you'll get 22 Feb, 2006:

?38739 + 31
38770
?Format(38770, "dd mmm yyyy")
22 Feb 2006

I don't feel it even relies on knowing that dates start at 30 Dec, 1899: 0
could represent any date, and this approach would still work in Access.

Based on that thinking, I wouldn't trust it in SQL Server
SQL statements without seeing some definitive documentation
to that effect.

I don't disagree with this statement: I believe SQL Server uses a different
start date (so that 0 isn't 30 Dec, 1899, like it is in Access and Excel),
but I'm not certain about that. However, even if your front-end is linked to
SQL Server, there's no reason why you can't use this "trick" to set the
default value for a field. If Access is doing translations when
communicating with SQL Server, it'll do the same translations in either
case.


I agree that this will work as long as the conversions are
done in Access.

What I am questioning is a situation where the long value is
saved to a table and then processed by another system, e.g.
Excel and SQL Server. If/When the long values is then used
in that context, it could generate wrong dates or require
the programmer to be aware of the situation and to program
Access's zero date in the other system. As long as the
programmer is totally aware of the issues with dates, the
Long value dates would not be stored to tables and the issue
would not arise.

I would not have brought up this extrapolation of Bob's
situation, if Bob had not stated repeatedly that he expected
to have to upsize his application.

It's probably not very nice to have a go at a suggestion made by an NG
participant who hasn't participated here (yet) - but - their advice is
given in a completely different context, and in this context, it has
been mentioned a possible upsize to SQL server and questions about the
clng method when concatenating a dynamic SQL string.

For those who bother, and/or have a possibility of testing, here's a
small sample

Create a SQL server table (dbo.DateTest), with fields
ID, Int, Identity PK
myText, Varchar(50)
myDate, DateTime

Then try to insert a record with date the "clng way" both on the linked
table, and on a connection

dim cn as adodb.connection
set cn = new adodb.connection
cn.Open "Provider=sqloledb;" & _
"Data Source=myServerName;" & _
"Initial Catalog=myDatabaseName;" & _
"Integrated Security=SSPI"
' connection string from here (watch for linebreaks)
http://www.carlprothman.net/Default.aspx?tabid=87#OLEDBProviderForSQLServer

' execute on connection to the SQL server
cn.execute "insert into dbo.DateTest (myText, myDate) " & _
"Values ('connection', " & clng(date) & ")"

' execute on "current db" -> linked table
currentproject.connection.execute "
"insert into dbo_DateTest (myText, myDate) " & _
"Values ('linked', " & clng(date) & ")"

on my setup, that gives two different dates (see below).

SQL server - select convert(datetime, 0) -> 1/1/1900
VBA format$(0, "m/d/yyyy") -> 12/30/1899

?date -> 1/22/2006
?format$(date, 0) -> 38739
select convert(datetime, 38739) -> 1/24/2006

so - at least on my setups, there's a two day difference between these
two samples, which makes me be blunt enought to say: be careful with
this approach if there's a possibility of

1 upsize to SQL server, and
2 executing action queries with date parameters on a separate
connection
using dynamic SQL

Anyone else verify?

I think I will continue to recommend using the format function when
assigning date parameters to dynamic SQL strings. Btw - when converting
a date to a long, wouldn't it also create an implicit conversion/cast
of
it to string when concatenating whith the dynamic SQL string?

I'm thinking that most problems I've seen when the format function has
been used, is when one has forgotten to escape the date separators (as
in for instance using "mm/dd/yyyy" in stead of "mm\/dd\/yyyy". The
first
of these formats, would barf on my settings, but again "yyyy-mm-dd"
should work everywhere).

The ISO-8601 date format is supposed to work with most recent
databases,
so I'll keep using it, should I work with date paramters in dynamic SQL
strings.

But why not use the Clng for GUI and pure/native Jet/Access stuff?
Speaking for myself, I think I'd get into troubles using different
methods for practically the same, I'd probably not remember when and
where to use what, but that's me ;-)
 
B

Bob Wickham

RoyVidar said:
Marshall Barton wrote in message
Douglas said:
Douglas J. Steele wrote:


Bob Wickham wrote:

I don't know the context where Steve recommended using CLng,
but the only reason I can think of is to drop the Time part
of a date value. Not to say Steve didn't have something
clever in mind, but I do not see how it can help in a
situation inolving the Default Value.


I believe what Steve may have been suggesting is that you can use:

Me![PaymentDate].DefaultValue = CLng(Me![PaymentDate])

rather than

Me![PaymentDate].DefaultValue = "#" & Format$(Me![PaymentDate],
"yyyy-mm-dd") & "#"



I never thought of that. It is clever, but it does rely on
Access to convert from a long to a date. While I don't
really have a problem with that, it is not in the slightest
portable to other contexts that don't provide the same
implicit conversions.


I don't really understand this comment, Marsh.

I don't see any reliance on converting from a long to a date. Dates
ARE numbers, after all. You can use the numeric equivalent of the
date anywhere. Today (22 Jan, 2006) is 38739 days since 30 Dec, 1899,
so CLng(Date) will give you 38739:

?CLng(Date)
38739

You can add, say, 31 to that to get 38970 and then format that as a
date, and you'll get 22 Feb, 2006:

?38739 + 31
38770
?Format(38770, "dd mmm yyyy")
22 Feb 2006

I don't feel it even relies on knowing that dates start at 30 Dec,
1899: 0 could represent any date, and this approach would still work
in Access.


Based on that thinking, I wouldn't trust it in SQL Server
SQL statements without seeing some definitive documentation
to that effect.


I don't disagree with this statement: I believe SQL Server uses a
different start date (so that 0 isn't 30 Dec, 1899, like it is in
Access and Excel), but I'm not certain about that. However, even if
your front-end is linked to SQL Server, there's no reason why you
can't use this "trick" to set the default value for a field. If
Access is doing translations when communicating with SQL Server,
it'll do the same translations in either case.



I agree that this will work as long as the conversions are
done in Access.

What I am questioning is a situation where the long value is
saved to a table and then processed by another system, e.g.
Excel and SQL Server. If/When the long values is then used
in that context, it could generate wrong dates or require
the programmer to be aware of the situation and to program
Access's zero date in the other system. As long as the
programmer is totally aware of the issues with dates, the
Long value dates would not be stored to tables and the issue
would not arise.

I would not have brought up this extrapolation of Bob's
situation, if Bob had not stated repeatedly that he expected
to have to upsize his application.


It's probably not very nice to have a go at a suggestion made by an NG
participant who hasn't participated here (yet) - but - their advice is
given in a completely different context, and in this context, it has
been mentioned a possible upsize to SQL server and questions about the
clng method when concatenating a dynamic SQL string.

For those who bother, and/or have a possibility of testing, here's a
small sample

Create a SQL server table (dbo.DateTest), with fields
ID, Int, Identity PK
myText, Varchar(50)
myDate, DateTime

Then try to insert a record with date the "clng way" both on the linked
table, and on a connection

dim cn as adodb.connection
set cn = new adodb.connection
cn.Open "Provider=sqloledb;" & _
"Data Source=myServerName;" & _
"Initial Catalog=myDatabaseName;" & _
"Integrated Security=SSPI"
' connection string from here (watch for linebreaks)
http://www.carlprothman.net/Default.aspx?tabid=87#OLEDBProviderForSQLServer

' execute on connection to the SQL server
cn.execute "insert into dbo.DateTest (myText, myDate) " & _
"Values ('connection', " & clng(date) & ")"

' execute on "current db" -> linked table
currentproject.connection.execute "
"insert into dbo_DateTest (myText, myDate) " & _
"Values ('linked', " & clng(date) & ")"

on my setup, that gives two different dates (see below).

SQL server - select convert(datetime, 0) -> 1/1/1900
VBA format$(0, "m/d/yyyy") -> 12/30/1899

?date -> 1/22/2006
?format$(date, 0) -> 38739
select convert(datetime, 38739) -> 1/24/2006

so - at least on my setups, there's a two day difference between these
two samples, which makes me be blunt enought to say: be careful with
this approach if there's a possibility of

1 upsize to SQL server, and
2 executing action queries with date parameters on a separate connection
using dynamic SQL

Anyone else verify?

I think I will continue to recommend using the format function when
assigning date parameters to dynamic SQL strings. Btw - when converting
a date to a long, wouldn't it also create an implicit conversion/cast of
it to string when concatenating whith the dynamic SQL string?

I'm thinking that most problems I've seen when the format function has
been used, is when one has forgotten to escape the date separators (as
in for instance using "mm/dd/yyyy" in stead of "mm\/dd\/yyyy". The first
of these formats, would barf on my settings, but again "yyyy-mm-dd"
should work everywhere).

The ISO-8601 date format is supposed to work with most recent databases,
so I'll keep using it, should I work with date paramters in dynamic SQL
strings.

But why not use the Clng for GUI and pure/native Jet/Access stuff?
Speaking for myself, I think I'd get into troubles using different
methods for practically the same, I'd probably not remember when and
where to use what, but that's me ;-)
Whether upsizing to SQL Server is likely in the future is something
worth keeping in mind when designing any serious database.
Whether it happens or not, in my case, will depend on the success of my
clients business. Hopefully my database will make his business so damn
efficient, success will be a mere formality.

Thanks for the lively debate.

Bob Wickham
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top