Solved

Help with Date Format

Posted on 2011-09-19
17
349 Views
Last Modified: 2012-08-13
I have an Access 2007 ADP with a SQL 2008 database.  I am trying to insert either the current date or the date from one of my fields with an Update Query/Stored Procedure into a DateTime field with the desired result of mm/dd/yyyy format.  However no matter how I have tried to format the NOW() function or DATE function within VBA I get the result of 12:00:00 AM or some error message.  I have tried Format and Convert, but maybe not the right combinations.  Suggestions?  
0
Comment
Question by:JasBrad
  • 8
  • 6
  • 2
  • +1
17 Comments
 
LVL 5

Expert Comment

by:25112
Comment Utility
SELECT CONVERT(VARCHAR(10), GETDATE(), 101)
0
 
LVL 14

Expert Comment

by:nishant joshi
Comment Utility
0
 
LVL 26

Expert Comment

by:Nick67
Comment Utility
Ultimately in the end, the field stores a double precision number.
0 is 30-Dec-899 12:00:00 AM
Right now is 40805.4862731481

There isn't point in massaging the crud out of what you are trying to put in
Try it for yourself

Function myDate()
MsgBox CDbl(Now())
End Function

If you want to put in just the date of something use
DateValue(WhateverHoldsYourDate)

Now, displaying your date as it comes out is a different problem
There you want a Format(YourFieldOrControl, "mm/dd/yyyy")
Be careful, though, because NOW() DOES store the time.
That can bite you when you build BETWEEN queries
Your chosen format is also bad.
Depending on Regional Settings 01/09/11 can be 9-Jan-11 or 1-Sep-11
In English the format dd-MMM-yyyy is hard to misinterpret
In any language or system ISO 8601 format is always good -- yyyy-mm-dd -- 2011-09-19

Play with these
Function myDate()
MsgBox CDbl(Now())
MsgBox Format(Now(), "mm/dd/yyyy")
MsgBox Format(Now(), "mm/dd/yyyy hh:Mm:Ss AMPM")
MsgBox Format(DateValue(Now), "mm/dd/yyyy hh:Mm:Ss AMPM")
MsgBox Format(Date(), "mm/dd/yyyy")
MsgBox Format(Date(), "mm/dd/yyyy hh:Mm:Ss AMPM")
End Function

0
 

Author Comment

by:JasBrad
Comment Utility
I am getting a strange error from my ADP.  Microsoft Visual Basic, Compile Error: Sub or Function not Defined. It is erroring out on "VarChar".
0
 
LVL 26

Expert Comment

by:Nick67
Comment Utility
You'll need to post some code or maybe a sample--although with an ADP, that's hard
Normally Access will look after most of this stuff if it is coming from a control.
If you are doing it through SQL and VBA the code I posted should help you out.

If it's a passthrough of some sort, SQL Server takes date literals surrounded by single quotes
'19-Sep-11' would be what you'd pass to SQL Server for today's date
If you had to build the string in VBA then chr(39) & Date & chr(39) would be good
ISO 8601 formats are always good too '2011-09-19'
0
 
LVL 14

Expert Comment

by:nishant joshi
Comment Utility
here put the formate in palce of varchar..
0
 

Author Comment

by:JasBrad
Comment Utility
Its just a simple bit of code actually.  The field of HOLADJUST is DateTime.

strSQL = " Update AdjustedSchedule"
    strSQL = strSQL & " Set AdjustedSchedule.HOLADJUST =" & Convert(VarChar(10), GetDate(), 101) & ","
    strSQL = strSQL & " AdjustedSchedule.HOLIDAY = '" & Left(Replace([Forms]![HOLIDAY]![HOLIDAY], "'", "''"), 25) & "',"
    strSQL = strSQL & " AdjustedSchedule.STATUS = 'Holiday';"
    DoCmd.RunSQL (strSQL)

Open in new window

0
 
LVL 26

Expert Comment

by:Nick67
Comment Utility
This should work

strSQL = " Update AdjustedSchedule"
    strSQL = strSQL & " Set AdjustedSchedule.HOLADJUST =#" & Date & "#,"
    strSQL = strSQL & " AdjustedSchedule.HOLIDAY = '" & Left(Replace([Forms]![HOLIDAY]![HOLIDAY], "'", "''"), 25) & "',"
    strSQL = strSQL & " AdjustedSchedule.STATUS = 'Holiday';"
    DoCmd.RunSQL (strSQL)

Since Access VBA seems to be doing the lifting, it should work out sending the date in a way SQL Server would like.
Note the hashes #  It needs those for a date

Let me know if it doesn't work
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:JasBrad
Comment Utility
Maybe I do not have something turned on in the references?  I am getting Microsoft Office Access error of Incorrect syntax near '#'.  Seems odd.
0
 
LVL 26

Expert Comment

by:Nick67
Comment Utility
So let's simplify first

strSQL = " Update AdjustedSchedule"
    strSQL = strSQL & " Set AdjustedSchedule.HOLADJUST =#19-Sep-11#,"
    strSQL = strSQL & " AdjustedSchedule.HOLIDAY = '" & Left(Replace([Forms]![HOLIDAY]![HOLIDAY], "'", "''"), 25) & "',"
    strSQL = strSQL & " AdjustedSchedule.STATUS = 'Holiday';"
    DoCmd.RunSQL (strSQL)

Does it like this?
0
 

Author Comment

by:JasBrad
Comment Utility
Nope.  Same error message.
0
 
LVL 26

Expert Comment

by:Nick67
Comment Utility
Ok, try

strSQL = " Update AdjustedSchedule"
    strSQL = strSQL & " Set AdjustedSchedule.HOLADJUST ='19-Sep-11',"
    strSQL = strSQL & " AdjustedSchedule.HOLIDAY = '" & Left(Replace([Forms]![HOLIDAY]![HOLIDAY], "'", "''"), 25) & "',"
    strSQL = strSQL & " AdjustedSchedule.STATUS = 'Holiday';"
    DoCmd.RunSQL (strSQL)
0
 

Author Comment

by:JasBrad
Comment Utility
That works fine.
0
 
LVL 26

Expert Comment

by:Nick67
Comment Utility
Ok now for dates.
Since this seems to be using SQL Server Syntax lets try

strSQL = " Update AdjustedSchedule"
    strSQL = strSQL & " Set AdjustedSchedule.HOLADJUST = GETDATE(),"
    strSQL = strSQL & " AdjustedSchedule.HOLIDAY = '" & Left(Replace([Forms]![HOLIDAY]![HOLIDAY], "'", "''"), 25) & "',"
    strSQL = strSQL & " AdjustedSchedule.STATUS = 'Holiday';"
    DoCmd.RunSQL (strSQL)

Note that GETDATE alos includes the time.
Is that what you want?
 
0
 
LVL 26

Accepted Solution

by:
Nick67 earned 250 total points
Comment Utility
If that worked and you only wanted the date then

strSQL = " Update AdjustedSchedule"
    strSQL = strSQL & " Set AdjustedSchedule.HOLADJUST = "
    strSQL = strSQL & " CAST(FLOOR( CAST( GETDATE() AS FLOAT ) ) AS DATETIME)"
    strSQL = strSQL & " AdjustedSchedule.HOLIDAY = '" & Left(Replace([Forms]![HOLIDAY]![HOLIDAY], "'", "''"), 25) & "',"
    strSQL = strSQL & " AdjustedSchedule.STATUS = 'Holiday';"
    DoCmd.RunSQL (strSQL)

might be what you need
0
 

Author Comment

by:JasBrad
Comment Utility
Much better.  I could do without the time but can widen the fields.  But can live with it there as all I need is a stamp.  Thank you.
0
 
LVL 26

Expert Comment

by:Nick67
Comment Utility
The reason I ask about storing times is because BETWEEN criteria constructed with date values that are storing times can give unexpected results
We expect that Between 1-Sep-11 and 19-Sep-11 will return everything from 19-Sep-11.
When a time is stored it does not, because what actually occurs is
Between 1-Sep-11 12:00:00 AM and 19-Sep-11 12:00:00 AM.

If you set the time part of a datetime value to nothing before storing it, this isn't a bother since all the values will have the implicit time of 12:00:00 AM
Begin storing time with GETDATE() and you'll need to be careful to craft that between criteria as
Between 1-Sep-11 12:00:00 AM and 19-Sep-11 11:59:59 PM.


Glad it worked
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now