Solved

Define a date in a variable with changing year part

Posted on 2007-11-30
5
317 Views
Last Modified: 2013-11-25
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

0
Comment
Question by:ssmith94015
5 Comments
 
LVL 14

Expert Comment

by:RDWaibel
ID: 20383887
Dim sYear as string
sYear = Year(maxDate)
dteEOY = cdate("12/31/" & sYear)

there ya go!
0
 
LVL 77

Expert Comment

by:peter57r
ID: 20383894
dteEOY = Dateserial(Year(maxDate),12,31)
0
 
LVL 16

Accepted Solution

by:
Rick_Rickards earned 500 total points
ID: 20383906
How about...
dteEOY = CDate("12/31/" & Year(maxDate))

Open in new window

0
 

Author Comment

by:ssmith94015
ID: 20383939
Rick had the simplest, thank you all however.
0
 
LVL 16

Expert Comment

by:Rick_Rickards
ID: 20384039
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.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question