Link to home
Start Free TrialLog in
Avatar of Sandra Smith
Sandra SmithFlag for United States of America

asked on

Define a date in a variable with changing year part

When I first did this and hard-coded 12/31/2008 in the if staement, it worked fine.  But I need this to update for 12/31 and a new year.  So, how do I tell the variable that add the year from my form?  I need either the Year(maxDate) for 12/31 & FORMS!frmSalesPayroll.txtForecastYear.  I think it has something to do with the pounds but I am not sure as everything I tried still does not work.
Public Sub AppendCalendarDate()
On Error GoTo ErrorHandler
Dim addDate As Date, maxDate As Date, i As Integer, strAppendDate As String, dteEOY As Date
 
maxDate = DMax("CalDate", "tblSalesDaily445_and_Calendar")
dteEOY = #12/31/Year(maxDate)#
If maxDate < dteEOY Then
    i = DatePart("d", maxDate) + 1
Debug.Print maxDate
Debug.Print i
Debug.Print dteEOY
 
 
For i = i To 31
strAppendDate = "INSERT INTO tblSalesDaily445_and_Calendar ( ForecastYear, CalDate, CalMonth ) " & _
    "SELECT DISTINCT Year([CalDate]) AS " & _
    "ForecastYear, #12/" & i & "/" & Forms!frmSalesPayroll.txtForecastYear & "# AS CalDate, " & _
    "MonthName(Month([CalDate]),True) AS CalMonth " & _
    "FROM tblSalesDaily445_and_Calendar "
    
DoCmd.RunSQL strAppendDate
Next i
End If
Exit_ErrorHandler:
Exit Sub
ErrorHandler:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_ErrorHandler
    
End Sub

Open in new window

Avatar of RDWaibel
RDWaibel
Flag of United States of America image

Dim sYear as string
sYear = Year(maxDate)
dteEOY = cdate("12/31/" & sYear)

there ya go!
Avatar of peter57r
dteEOY = Dateserial(Year(maxDate),12,31)
ASKER CERTIFIED SOLUTION
Avatar of Rick_Rickards
Rick_Rickards
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sandra Smith

ASKER

Rick had the simplest, thank you all however.
My thanks as well ssmith.  Just glad you liked the approach.  As for anyone who read this in the future though it is worth pointing out that everyone offered a valid and accurate solution, there was simply a multitude of ways that this issue could be solved.