Selasa, 29 November 2011

[MS_AccessPros] VBA question: my date variables are defaulting to 12/30/1899 and I can't figure out why!

 

I'm not sure if I have something screwy going on with my installation of
Access or if I found a bug, but any help working around this would be great!

Running Access 2010 (v14.0.6112.5000 (32-bit)) - I don't see any Office
Service Packs, but I'm not in control of that. .... and if it makes a
difference, the OS is WinXP Pro SP 3 (32-bit)

my debug statements show ....

? whatsy()
The time is now ... 2011-11-29 14:35:12
Year: 1899 Month: 12 Day: 30
WHEN: 1899-12-30 00:00:00
7/1/1899

the function is ....

~~~~~~~~~~~~~~~~

Public Function WhatSY(Optional ByVal dtDate As Date) As Date
Dim dtWhen As Date
Dim nY As Integer
Dim nM As Integer
Dim nD As Integer

Debug.Print "The time is now ... " & Format(Now(), "yyyy-mm-dd hh:mm:ss")

If IsMissing(dtDate) Then ' Or IsNull(dtDate) Or Len(dtDate) < 1 Then
nY = Year(Now())
nM = Month(Now())
nD = Day(Now())
Else
nY = Year(dtDate)
nM = Month(dtDate)
nD = Day(dtDate)
End If

Debug.Print "Year: " & nY & " Month: " & nM & " Day: " & nD

dtWhen = DateSerial(nY, nM, nD)

Debug.Print "WHEN: " & Format(dtWhen, "yyyy-mm-dd hh:mm:ss")

Select Case Month(dtWhen)
Case Month(dtWhen) >= 7
WhatSY = DateSerial(Year(dtWhen) - 1, 7, 1)
Case Month(dtWhen) < 3
WhatSY = DateSerial(Year(dtWhen) - 2, 7, 1)
Case Else
WhatSY = DateSerial(Year(dtWhen), 7, 1)
End Select

End Function

Note: the purpose of the function is to figure out which year is currently
being reported on, where the fiscal year starts July 1st, but the reporting
period doesn't start until after February, and ends the following February.
Example: fiscal year 10/11 starts reporting on 3/1/11 and ends on 2/28/12.
At some point, I'll probably set up a global variable for when the
reporting period starts and use that instead of the hard-coded February

~*~*~*~*~*~*~
Shay Bella Holmes
~*~*~*~*~*~*~

[Non-text portions of this message have been removed]

__._,_.___
Recent Activity:
MARKETPLACE

Stay on top of your group activity without leaving the page you're on - Get the Yahoo! Toolbar now.

.

__,_._,___

Tidak ada komentar:

Posting Komentar