473,396 Members | 1,894 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,396 software developers and data experts.

Access 2003: Frustrating date/time formatting switch in VBA update query

49
I am writing code to import assessment information about a building from an Excel file (populated by a mobile interviewer on a laptop) into an Access database. With much trial and error, I've hit upon a way of importing all the necessary information, but have run into one last problem when establishing my fully-prepared temporary table to use for appending to the permanent assessment data table.

One of the fields in the form, AImportDate, is a date/time field without a specified format. I have obtained the value of the assessment date and stored it as a variable dtDate, Dim'd as Date. For my test file, dtDate properly returns '05/03/2010' (5 March). I then run the following SQL command:

Expand|Select|Wrap|Line Numbers
  1.     strSQL = "UPDATE " & strWriteTable & " " _
  2.     & "SET AImportPropIndex = " & intPropIndex & ", " _
  3.     & "AImportDate = " & dtDate & ";"
  4.  
  5.     'DoCmd.SetWarnings False
  6.     DoCmd.RunSQL strSQL
  7.     DoCmd.SetWarnings True
Bizarrely, this causes AImportDate for all of the records in the table to be updated with a value of '00:01:12', not a date at all, and certainly not the right one (if I change the format to Short Date in the table design, they show as '30/12/1899'). Yet if I manually type the date into the table, even after the update in the code, it displays correctly.

I had created the table with this SQL statement earlier in the module:

Expand|Select|Wrap|Line Numbers
  1.     strSQL = "CREATE TABLE " & strWriteTable & " " _
  2.     & "(AImportIndex INT, " _
  3.     & "AssessmentItemIndex INT NOT NULL, " _
  4.     & "AssessmentRShort INT, " _
  5.     & "AssessmentRFreeText TEXT(255), " _
  6.     & "AssessmentRComment TEXT(255), " _
  7.     & "AImportPropIndex INT, " _
  8.     & "AImportDate DATETIME, " _
  9.     & "PRIMARY KEY (AImportIndex));"
  10.  
  11.     DoCmd.RunSQL strSQL
Does anyone know why this problem would be occurring? And the best way to circumvent it? Thanks.
Mar 17 '10 #1

✓ answered by TheSmileyCoder

While the variable dtDate is a date, your parsing it into a sql string. That converts the date into a string. To preserve the date literal, you need to enclose it in # on both sides. Try looking at this article:

Literal datetimes and their delimiters

I also believe I can tell you why using a double works. This bit is dragged up from memory so if not 100% accurate don't shoot me :)
Dates are stored as doubles, with the part before the decimal , seperator (or . seperator depending on where your from) being number of dates after a specific date (which i think is 31 of december 1899 or something) the part after the , being the time fraction.

9 5253
JeremyI
49
Right, tinkered a bit more and resolved the fundamental issue: I changed the variable type to Double (dblDate, heh heh). Now everything is fine.

Still happy to give a best answer to anyone who can explain why this happens...
Mar 17 '10 #2
TheSmileyCoder
2,322 Expert Mod 2GB
While the variable dtDate is a date, your parsing it into a sql string. That converts the date into a string. To preserve the date literal, you need to enclose it in # on both sides. Try looking at this article:

Literal datetimes and their delimiters

I also believe I can tell you why using a double works. This bit is dragged up from memory so if not 100% accurate don't shoot me :)
Dates are stored as doubles, with the part before the decimal , seperator (or . seperator depending on where your from) being number of dates after a specific date (which i think is 31 of december 1899 or something) the part after the , being the time fraction.
Mar 17 '10 #3
JeremyI
49
I see! Yes, I recall seeing the # signs elsewhere but previously didn't know the proper use of them.

Exactly as predicted in your link, Access has muddled up the British-format date I passed and changed it to 3 May 2010. I think I'll stick with the Double format for this application.

Thanks very much.
Mar 17 '10 #4
NeoPa
32,556 Expert Mod 16PB
@JeremyI
This is a misunderstanding of what's going on here.

Jet SQL (not Access as such) has correctly interpreted your muddled up date as 3rd May 2010. Just as it correctly interpreted your 5 ÷ 3 ÷ 2010 as a fraction which, when displayed as a fraction of a day, came out as 1 minute and 12 seconds.

It's all about presenting the data in the correct format. If you tell it to treat it as a number sequence it will. If you pass the 5 as the first portion of a date literal, it will treat it as May. That is the SQL standard. It is behaving perfectly predicatably (as we would all hope).
Mar 17 '10 #5
JeremyI
49
OK... I thought I was starting to understand, but now I'm re-confused, I'm afraid. If the cell in Excel from which the date value is obtained is formatted as dd/mm/yyyy (OS-dependent), what is happening with the date variable?

At what point are the delimiters supposed to be used?
Mar 18 '10 #6
NeoPa
32,556 Expert Mod 16PB
That's a good question. It's covered by the paragraph titled Date Variables in the article, but it is nevertheless very difficult to get. Especially for those new to the whole concept.

The important idea is whether or not the SQL itself contains characters that point to where the date value can be found (variables of some form), or whether it contains ASCII characters representing the value itself. I'll include an example to illustrate. One that many get confused over :
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT " & Date() & " AS [CurrentDate],"
This is quite wrong!

The reference to the function Date() would normally be covered by the reference rule, which would mean it doesn't need the "#" characters. It doesn't appear to be a literal after all. In this case though, the reference is only within the context of the VBA. What is eventually processed as SQL is the strSQL string, which will simply contain (for 5th March 2010) :
Expand|Select|Wrap|Line Numbers
  1. SELECT 05/03/2010 AS [CurrentDate],
The USA version would be equally incorrect as :
Expand|Select|Wrap|Line Numbers
  1. SELECT 03/05/2010 AS [CurrentDate],
With no delimiters in either, they are both treated as number equations (5 ÷ 3 ÷ 2010 & 3 ÷ 5 ÷ 2010).

As Date() is a function also available to the SQL interpreter this could have been written with a valid reference as :
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT Date() AS [CurrentDate],"
Which would resolve to :
Expand|Select|Wrap|Line Numbers
  1. SELECT Date() AS [CurrentDate],
What you're really looking for here though is a literal date value. While Date() may be ok to reference from within the SQL, many other worked out (calculated) date values may not be, hence the requirement for properly formatted date literals :
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT #" & Format(Date(), "m/d/yyyy") & "# AS [CurrentDate],"
This resolves to (correct no matter where you are in the world) :
Expand|Select|Wrap|Line Numbers
  1. SELECT #3/5/2010# AS [CurrentDate],
Mar 18 '10 #7
JeremyI
49
Sweet! Will come back to this thread as the need arises.
Mar 18 '10 #8
NeoPa
32,556 Expert Mod 16PB
@JeremyI
I interpret that as All fully understood. Good news :)
Mar 18 '10 #9
JeremyI
49
Absolutely. :-) Thanks again.
Mar 22 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB...
13
by: Peter James | last post by:
Access 97 If I select New on the Query tab of the db window, and go staight to sql view and type in the following for example: INSERT INTO tblMyTable ( dtDate, txtAny) VALUES (#2003-09-03#,...
7
by: Bob | last post by:
Currently I am using this statement to translate 3 fields in my db thru Visual Basic. I import the data from one table to another then call the IFF statements and the NewDate to translate the...
23
by: JustMe | last post by:
I don't know if this has anything to do with AccessXP running on Terminal Services with Access97/2000 also installed, but here is one example of a query that does not work any longer: SELECT...
2
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data...
5
by: m_t_hill | last post by:
Running MS Access 2000 MS Windows XP Pro This has caused me a lot of hair loss in the last few days so would appreciate any help. I am running code to append/update a local access database...
7
by: Andy Davis | last post by:
I have a table of data in Access 2002 which is used as the source table for a mail merge document using Word 2002 on my clients PC. The data is transferred OK but I've noticed that any dates which...
7
by: aaron.kempf | last post by:
team so i have a nice little list in sharepoint.. about 15k items i've got the simplest little join statement in access; and im trying to UPDATE the sharepoint list via an access query. so...
3
by: Jim in Arizona | last post by:
I have a gridview that's being populated from an access db query. The problem I'm having is that the date/time fields in access that are populating the gridview are showing both date and time, when...
3
by: Sheldon | last post by:
I have the following query expression - Like Format((!!)) & "/*/" & (! !) which would translate to e.g. 04/*/2007 if someone is running a report for last month. The above expression is part of a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...

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.