Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date function gives an error 13 1

Status
Not open for further replies.

vba317

Programmer
Joined
Mar 5, 2009
Messages
708
Location
US
I am using access 2003. I am trying to calculate a report period based on todays date. My code works for todays date but when I was testing for other dates less than 10 that is when I run into problems. I have attempted a couple of solutions with no luck. This code works but if I change the date to 3/6/2012 that is when I get the error. Any help is appreciated.

Code:
Public Function CurRptMon(pd As Integer)
Dim strCurDate As Date
Dim strCurMon As String
Dim iCurYr As Integer
Dim iCurMon As Integer
Dim iCurPd As Integer
Dim iCurMonPd As Integer
Dim ipdCalc As Integer

strCurDate = #11/6/2013#
iCurYr = CInt(Right(strCurDate, 4))
iCurMon = CInt(Left(strCurDate, 2))
    If iCurMon = 0 Then
        iCurMon = CInt(Left(strCurDate, 1))
    Else
        iCurMon = CInt(Left(strCurDate, 2))
    End If
iCurMonPd = iCurMon - 1

If iCurMonPd = 1 Then strCurMon = "Jan"
If iCurMonPd = 2 Then strCurMon = "Feb"
If iCurMonPd = 3 Then strCurMon = "Mar"
If iCurMonPd = 4 Then strCurMon = "Apr"
If iCurMonPd = 5 Then strCurMon = "May"
If iCurMonPd = 6 Then strCurMon = "Jun"
If iCurMonPd = 7 Then strCurMon = "Jul"
If iCurMonPd = 8 Then strCurMon = "Aug"
If iCurMonPd = 9 Then strCurMon = "Sep"
If iCurMonPd = 10 Then strCurMon = "Oct"
If iCurMonPd = 11 Then strCurMon = "Nov"
If iCurMonPd = 12 Then strCurMon = "Dec"

ipdCalc = 360
If iCurYr = 2013 Then ipdCalc = ipdCalc + 11
If iCurYr = 2014 Then ipdCalc = ipdCalc + 21
If iCurYr = 2015 Then ipdCalc = ipdCalc + 31
If iCurYr = 2016 Then ipdCalc = ipdCalc + 41

pd = ipdCalc + iCurMonPd

End Function
 
First, you can replace all of your If iCurMonPd with just
Code:
strCurMon = MonthName(iCurMoPd,True)

I'm not sure why you are using "str" prefix to create a date memory variable since this is confusing.

You are us a string function "Right()" with a date. You should use:
Code:
iCurYr = Year(strCurDate)
iCurMon = Month(strCurDate)

Duane
Hook'D on Access
MS Access MVP
 
Thank you for your response. You are right so I changed the variables to dt instead of str. I am currently getting an error 5 invalid procedure or call on
strCurMon = MonthName(iCurMonPd, True)

I don't have MonthName declared is this a string or date?

Tom
 
I just realized that MonthName is a function. I changed iCurMonPd to iCurMon and everything works. Thanks

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top