PC Review


Reply
Thread Tools Rate Thread

Problems with MS Access reading dates as US, not British formats.

 
 
Will Flanagan
Guest
Posts: n/a
 
      16th Apr 2010
Hi Folks,

I downloaded a database off the MSDN website (web
address:http://msdn.microsoft.com/en-us/library/dd327646.aspx). It
calculates the number of working days and takes off any holidays which are
stored in a separate table. It works fine, but the only problem is that
when I enter dates in the holiday table, it reads it as the US format of
m/d/yy instead of UK d/m/yy. I have set the regional settings to UK
time/date so my dates in the Holidays table are displayed as UK, but I have
to remember to enter the dates in US order, for the system to work. (I know
this will cause confusion for users later on).

The problematic code is:

Public Function Workdays(ByRef startDate As Date, _
ByRef endDate As Date, _
Optional ByRef strHolidays As String = "Holidays" _
) As Integer
' Returns the number of workdays between startDate
' and endDate inclusive. Workdays excludes weekends and
' holidays. Optionally, pass this function the name of a table
' or query as the third argument. If you don't the default
' is "Holidays".
On Error GoTo Workdays_Error
Dim nWeekdays As Integer
Dim nHolidays As Integer
Dim strWhere As String

' DateValue returns the date part only.
startDate = DateValue(startDate)
endDate = DateValue(endDate)

nWeekdays = Weekdays(startDate, endDate)
If nWeekdays = -1 Then
Workdays = -1
GoTo Workdays_Exit
End If

strWhere = "[Holiday] >= #" & startDate _
& "# AND [Holiday] <= #" & endDate & "#"

' Count the number of holidays.
nHolidays = DCount(Expr:="[Holiday]", _
Domain:=strHolidays, _
Criteria:=strWhere)

Workdays = nWeekdays - nHolidays

Workdays_Exit:
Exit Function

Workdays_Error:
Workdays = -1
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Workdays"
Resume Workdays_Exit

End Function

The dates are stored in the [Holiday] field of the Holidays table.

I am sure that there is something really simple I need to do in this Visual
Basic code to make sure it reads all dates as UK, but I am completely stuck!!

I didn't know if anyone would have any ideas?

Many thanks,

Will.
 
Reply With Quote
 
 
 
 
Alex Dybenko
Guest
Posts: n/a
 
      16th Apr 2010
Hi,
first - check that you get correct dates into function, then you have to
change this line:

strWhere = "[Holiday] >= #" & format(startDate,"mm\/dd\/yyyy") _
& "# AND [Holiday] <= #" & Format(endDate,"mm\/dd\/yyyy") & "#"

Jet correctly understand dates, passed in mm/dd/yyyy format


--
Best regards,
___________
Alex Dybenko (MVP)
http://accessblog.net
http://www.PointLtd.com

"Will Flanagan" <(E-Mail Removed)> wrote in message
news:8E2ADB9A-440F-4A1D-80AB-(E-Mail Removed)...
> Hi Folks,
>
> I downloaded a database off the MSDN website (web
> address:http://msdn.microsoft.com/en-us/library/dd327646.aspx). It
> calculates the number of working days and takes off any holidays which are
> stored in a separate table. It works fine, but the only problem is that
> when I enter dates in the holiday table, it reads it as the US format of
> m/d/yy instead of UK d/m/yy. I have set the regional settings to UK
> time/date so my dates in the Holidays table are displayed as UK, but I
> have
> to remember to enter the dates in US order, for the system to work. (I
> know
> this will cause confusion for users later on).
>
> The problematic code is:
>
> Public Function Workdays(ByRef startDate As Date, _
> ByRef endDate As Date, _
> Optional ByRef strHolidays As String = "Holidays" _
> ) As Integer
> ' Returns the number of workdays between startDate
> ' and endDate inclusive. Workdays excludes weekends and
> ' holidays. Optionally, pass this function the name of a table
> ' or query as the third argument. If you don't the default
> ' is "Holidays".
> On Error GoTo Workdays_Error
> Dim nWeekdays As Integer
> Dim nHolidays As Integer
> Dim strWhere As String
>
> ' DateValue returns the date part only.
> startDate = DateValue(startDate)
> endDate = DateValue(endDate)
>
> nWeekdays = Weekdays(startDate, endDate)
> If nWeekdays = -1 Then
> Workdays = -1
> GoTo Workdays_Exit
> End If
>
> strWhere = "[Holiday] >= #" & startDate _
> & "# AND [Holiday] <= #" & endDate & "#"
>
> ' Count the number of holidays.
> nHolidays = DCount(Expr:="[Holiday]", _
> Domain:=strHolidays, _
> Criteria:=strWhere)
>
> Workdays = nWeekdays - nHolidays
>
> Workdays_Exit:
> Exit Function
>
> Workdays_Error:
> Workdays = -1
> MsgBox "Error " & Err.Number & ": " & Err.Description, _
> vbCritical, "Workdays"
> Resume Workdays_Exit
>
> End Function
>
> The dates are stored in the [Holiday] field of the Holidays table.
>
> I am sure that there is something really simple I need to do in this
> Visual
> Basic code to make sure it reads all dates as UK, but I am completely
> stuck!!
>
> I didn't know if anyone would have any ideas?
>
> Many thanks,
>
> Will.


 
Reply With Quote
 
 
 
 
rocco
Guest
Posts: n/a
 
      16th Apr 2010
Acces will always refer to US date format when using date variable in code.
You can SEE Uk format but you should USE US format in code or SQL statements
(all the use the US format for date, is an international convention on any
RDBMS and more general in programming).
Use the format property for the table field to show the date in your desired
format but use the format function in code to have date in US format:
Format(myDate, "\#mm\/dd\/yyyy\#")

rocco


"Will Flanagan" wrote:

> Hi Folks,
>
> I downloaded a database off the MSDN website (web
> address:http://msdn.microsoft.com/en-us/library/dd327646.aspx). It
> calculates the number of working days and takes off any holidays which are
> stored in a separate table. It works fine, but the only problem is that
> when I enter dates in the holiday table, it reads it as the US format of
> m/d/yy instead of UK d/m/yy. I have set the regional settings to UK
> time/date so my dates in the Holidays table are displayed as UK, but I have
> to remember to enter the dates in US order, for the system to work. (I know
> this will cause confusion for users later on).
>
> The problematic code is:
>
> Public Function Workdays(ByRef startDate As Date, _
> ByRef endDate As Date, _
> Optional ByRef strHolidays As String = "Holidays" _
> ) As Integer
> ' Returns the number of workdays between startDate
> ' and endDate inclusive. Workdays excludes weekends and
> ' holidays. Optionally, pass this function the name of a table
> ' or query as the third argument. If you don't the default
> ' is "Holidays".
> On Error GoTo Workdays_Error
> Dim nWeekdays As Integer
> Dim nHolidays As Integer
> Dim strWhere As String
>
> ' DateValue returns the date part only.
> startDate = DateValue(startDate)
> endDate = DateValue(endDate)
>
> nWeekdays = Weekdays(startDate, endDate)
> If nWeekdays = -1 Then
> Workdays = -1
> GoTo Workdays_Exit
> End If
>
> strWhere = "[Holiday] >= #" & startDate _
> & "# AND [Holiday] <= #" & endDate & "#"
>
> ' Count the number of holidays.
> nHolidays = DCount(Expr:="[Holiday]", _
> Domain:=strHolidays, _
> Criteria:=strWhere)
>
> Workdays = nWeekdays - nHolidays
>
> Workdays_Exit:
> Exit Function
>
> Workdays_Error:
> Workdays = -1
> MsgBox "Error " & Err.Number & ": " & Err.Description, _
> vbCritical, "Workdays"
> Resume Workdays_Exit
>
> End Function
>
> The dates are stored in the [Holiday] field of the Holidays table.
>
> I am sure that there is something really simple I need to do in this Visual
> Basic code to make sure it reads all dates as UK, but I am completely stuck!!
>
> I didn't know if anyone would have any ideas?
>
> Many thanks,
>
> Will.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
US - British Dates Howard31 Microsoft Excel Programming 1 21st Dec 2008 11:38 PM
Using DCount and British dates mlm198@lycos.co.uk Microsoft Access 3 8th Mar 2007 12:31 PM
British Keyboard and British Windows XP =?Utf-8?B?SkQ=?= Windows XP General 6 21st Feb 2006 06:11 AM
Dates dates dates dates... SQL and ASP.NET David Lozzi Microsoft ASP .NET 1 30th Sep 2005 03:18 PM
British Pounds Symbol "£" not rendering correctly Brian Manchester Windows XP Internet Explorer 2 22nd Apr 2004 04:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:36 PM.