Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# Function not returning correct year in date

Posted on 2010-11-15
Medium Priority
337 Views
I have the function attached, but for thelife of me, I cannot figure out whyit keeps returning 1899 no matter what I do.
``````Public Function OffsetsYearEnds(strPeriodRangeType As String) As Date
'Determines what the correct year end dates should be for annual reporting
Dim strYearEnd As String
Dim intMonth As Integer

intMonth = Month(Date)

Select Case strYearEnd
Case "YRENDDEC"
If intMonth > 1 Then
OffsetsYearEnds = CDate("12/31/" & (Year(Date) - 1))
Else
OffsetsYearEnds = CDate("12/31/" & (Year(Date) - 2))
End If
Case "YRENDNOV"
If intMonth > 11 Then
OffsetsYearEnds = CDate("11/30" & Year(Date))
Else
OffsetsYearEnds = CDate("11/30/" & (Year(Date) - 1))
End If
Case "YRENDOCT"
If intMonth > 10 Then
OffsetsYearEnds = CDate("10/31" & Year(Date))
Else
OffsetsYearEnds = CDate("10/31/" & (Year(Date) - 1))
End If
Case "YRENDSEP"
If intMonth > 9 Then
OffsetsYearEnds = CDate("9/30" & Year(Date))
Else
OffsetsYearEnds = CDate("7/30/" & (Year(Date) - 1))
End If
Case "YRENDAUG"
If intMonth > 8 Then
OffsetsYearEnds = CDate("8/31" & Year(Date))
Else
OffsetsYearEnds = CDate("8/31/" & (Year(Date) - 1))
End If
Case "YRENDJUL"
If intMonth > 7 Then
OffsetsYearEnds = CDate("7/31" & Year(Date))
Else
OffsetsYearEnds = CDate("7/31/" & (Year(Date) - 1))
End If
Case "YRENDJUN"
If intMonth > 6 Then
OffsetsYearEnds = CDate("6/30" & Year(Date))
Else
OffsetsYearEnds = CDate("6/30/" & (Year(Date) - 1))
End If
Case "YRENDMAY"
If intMonth > 5 Then
OffsetsYearEnds = "5/31" & Year(Date)
Else
OffsetsYearEnds = "5/31/" & (Year(Date) - 1)
End If
Case "YRENDAPR"
If intMonth > 4 Then
OffsetsYearEnds = CDate("4/30" & Year(Date))
Else
OffsetsYearEnds = CDate("4/30/" & (Year(Date) - 1))
End If
Case "YRENDMAR"
If intMonth > 3 Then
OffsetsYearEnds = CDate("3/31" & Year(Date))
Else
OffsetsYearEnds = CDate("3/31/" & (Year(Date) - 1))
End If
Case "YRENDFEB"
If intMonth > 2 Then
OffsetsYearEnds = CDate("3/1" & Year(Date)) - 1  'Avoids leap year problem simply to take March 1st less one day
Else
OffsetsYearEnds = CDate("3/1" & Year(Date) - 1) - 1
End If
Case "YRENDJAN"
If intMonth > 1 Then
OffsetsYearEnds = CDate("1/31" & Year(Date))
Debug.Print OffsetsYearEnds
Else
OffsetsYearEnds = CDate("1/31/" & (Year(Date) - 1))
End If
Case "YRENDFEB"
If intMonth > 2 Then
OffsetsYearEnds = CDate("3/1" & Year(Date)) - 1  'Avoids leap year problem simply to take March 1st less one day
Else
OffsetsYearEnds = CDate("3/1" & Year(Date) - 1) - 1
End If
End Select

Debug.Print FormatDateTime(OffsetsYearEnds, vbShortDate)
End Function
``````
0
Question by:ssmith94015
• 10
• 7
• 2
• +1

Author Comment

ID: 34140034
Ok, I found a typo in that not all the data had the closing "/". But even when I fixed that, it still returns 12/30/1899 for all dates
0

LVL 75

Accepted Solution

DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 1800 total points
ID: 34140073
You have

Dim strYearEnd As String

but you forgot to do this:

strYearEnd=strPeriodRangeType

mx
0

LVL 75

Expert Comment

ID: 34140077
So, none of the Case statements were triggering !

mx
0

Author Comment

ID: 34140080
Ok, found second error in that the Select Case statement needed to use the passed-in variable, strPeriodRangeType.  Now I am getting some correct dates and some still in the 12/30/1899 date.
0

LVL 46

Expert Comment

ID: 34140141
Please post the current version of the routine that is partially working for you.
0

LVL 5

Assisted Solution

Kenneth Brown earned 200 total points
ID: 34140147
Theres probably another typo.
More haste less speed. Read through what youve done as if youve never seen it before, & check it would do what you wanted it to do....
0

Author Comment

ID: 34140167
Attached is the code.  Yes, I am going too fast.
``````Public Function OffsetsYearEnds(strPeriodRangeType As String) As Date
'Determines what the correct year end dates should be for annual reporting
Dim strYearEnd As String
Dim intMonth As Integer

intMonth = Month(Date)

Select Case strPeriodRangeType
Case "YRENDDEC"
If intMonth > 1 Then
OffsetsYearEnds = CDate("12/31/" & (Year(Date) - 1))
Else
OffsetsYearEnds = CDate("12/31/" & (Year(Date) - 2))
End If
Case "YRENDNOV"
If intMonth > 11 Then
OffsetsYearEnds = CDate("11/30/" & Year(Date))
Else
OffsetsYearEnds = CDate("11/30/" & (Year(Date) - 1))
End If
Case "YRENDOCT"
If intMonth > 10 Then
OffsetsYearEnds = CDate("10/31/" & Year(Date))
Else
OffsetsYearEnds = CDate("10/31/" & (Year(Date) - 1))
End If
Case "YRENDSEP"
If intMonth > 9 Then
OffsetsYearEnds = CDate("9/30/" & Year(Date))
Else
OffsetsYearEnds = CDate("9/30/" & (Year(Date) - 1))
End If
Case "YRENDAUG"
If intMonth > 8 Then
OffsetsYearEnds = CDate("8/31/" & Year(Date))
Else
OffsetsYearEnds = CDate("8/31/" & (Year(Date) - 1))
End If
Case "YRENDJUL"
If intMonth > 7 Then
OffsetsYearEnds = CDate("7/31/" & Year(Date))
Else
OffsetsYearEnds = CDate("7/31/" & (Year(Date) - 1))
End If
Case "YRENDJUN"
If intMonth > 6 Then
OffsetsYearEnds = CDate("6/30/" & Year(Date))
Else
OffsetsYearEnds = CDate("6/30/" & (Year(Date) - 1))
End If
Case "YRENDMAY"
If intMonth > 5 Then
OffsetsYearEnds = "5/31/" & Year(Date)
Else
OffsetsYearEnds = "5/31/" & (Year(Date) - 1)
End If
Case "YRENDAPR"
If intMonth > 4 Then
OffsetsYearEnds = CDate("4/30/" & Year(Date))
Else
OffsetsYearEnds = CDate("4/30/" & (Year(Date) - 1))
End If
Case "YRENDMAR"
If intMonth > 3 Then
OffsetsYearEnds = CDate("3/31/" & Year(Date))
Else
OffsetsYearEnds = CDate("3/31/" & (Year(Date) - 1))
End If
Case "YRENDFEB"
If intMonth > 2 Then
OffsetsYearEnds = CDate("3/1/" & Year(Date)) - 1  'Avoids leap year problem simply to take March 1st less one day
Else
OffsetsYearEnds = CDate("3/1/" & Year(Date) - 1) - 1
End If
Case "YRENDJAN"
If intMonth > 1 Then
OffsetsYearEnds = CDate("1/31/" & Year(Date))
Else
OffsetsYearEnds = CDate("1/31/" & (Year(Date) - 1))
End If
End Select

Debug.Print FormatDateTime(OffsetsYearEnds, vbShortDate)
End Function
``````
0

Author Comment

ID: 34140188
Ok, found the error is NOT in my code, it is in the underlying data source I was given!  Ok, yes, made a few but now that I have cleaned up the supposedly "clean" data - it seems to be working fine.
0

LVL 75

Expert Comment

ID: 34140202
All the cases seem to be working now ... where are you getting 1899 ?

mx
0

LVL 75

Expert Comment

ID: 34140223
Ahh ... I posted the correct solution @ http:#a34140073

With all due respect, the Accepted and Assisted answers are not really solutions or the cause of the problem ...

mx
0

LVL 5

Expert Comment

ID: 34148280
Thanks, glad to be of some help...
0

Author Comment

ID: 34148877
DatabaseMX, you are correct but is there a timing issue to when answers are posted?  This is the first I have seen yours the only two were the ones I split the points to.
0

Author Comment

ID: 34148906
I am looking at the time sequence and DatabaseMX was in the first group.  I honestly do not know why DatabaseMX's responses were not updated to my viewing as he is correct and spotted the very first error I had.  I think I need to contact the moderator about this.
0

LVL 75

Expert Comment

ID: 34149083
I already contacted the mods ... thx.

mx
0

Author Comment

ID: 34149174
MOderator, have discussed issue with your Chat area and would like to re-distribute points as follows:

400 DatabaseMS
50 to KenBrown for suggesting I slow down (good point)
50 to aikiamark for also trying to help - I feel too guilty not to award points as this point but DatabaseMX is correct and probably diserves all - but I am a coward and don't want to penalize anyone for my system not refreshing properly!
0

LVL 75

Expert Comment

ID: 34149223
thx.  Really wasn't about the points, but what someone in the future will see as the actual answer/solution.

mx
0

Author Comment

ID: 34149307
Honestly, sometimes I simply do not know what to do.  But I am going to say, you (along with one or two others) consistently gve me answers the solve the issue quickly, clairify something or point me in the right direction.  Your tag is among one of the first I look for when I pose a question as well as when I am searching for an answer that may already exist.  Don't mean to sound saccarin but simply stating the way it is and I have appreciated, over these many years, all the help you have given.
0

LVL 75

Expert Comment

ID: 34149362
Well, I certainly appreciate that feedback and it's always a pleasure to help.

Thank you for using Experts Exchange.

mx
0

Author Comment

ID: 34157167
alias99, thank you.
0

Author Closing Comment

ID: 34157231
Ok, have re-assigned points.  Based on the moderator's advice, just going to assign to the two suggestions that helped (I really DO need to slow down and be reminded of that).    I still appreciate the attention by all.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance andâ€¦
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if youâ€™re going to set referential integrity: Decide if you want cascade updaâ€¦
If youâ€™ve ever visited a web page and noticed a cool font that you really liked the look of, but couldnâ€™t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yoâ€¦
###### Suggested Courses
Course of the Month11 days, 18 hours left to enroll