Solved

Change a month of a date on a recordset

Posted on 2011-02-10
6
300 Views
Last Modified: 2012-05-11
I need to create a running 60 month forecast end date, so if the current month changes I need for the forecast end date to reflect the month change.  However, if the current forecast end date has a hard coded date that equals the current year I do not want to change the date.

I am looking for function that will accomplish this task>

Karen

The code below was my first attempt, however, it returned the wrong dates.  Then I got to thinking there may be a simpler approach.

 ie. current ForecastEnddate = 1/1/2016 and the Current month is February the new ForecastEndDate = 2/1/2016, unless
 ForecastEnddate's year = current year then nothing happens.

Public Function UpdateForecastMth()
' Procedure: UpdateForecastEndDate
' DateTime: 2/10/2011 2:21:53 PM
' Author: Karen F. Schaefer
' Description: Verifies the Maximum ForecastEndDate and adds 1 month if current Forecast date <= current month
'  ie. current ForecastEnddate = 1/1/2016 and the Current month is February the new ForecastEndDate = 2/1/2016, unless
'      ForecastEnddate's year = current year then nothing happens.
'--
Dim pMth As Date
Dim pYear As Date
Dim strSql As String
Dim rs As DAO.Recordset
Dim db As Database
Set db = CurrentDb()

strSql = "Select ForecastEndDate from tblEmp_ForecastStatusing"
Set rs = db.OpenRecordset(strSql)
    Do Until rs.EOF
        pMth = Month(rs.Fields("ForecastEndDate").value)
        pYear = Year(rs.Fields("ForecastEndDate").value)
        
        If pMth < Month(Date) And pYear > Year(Date) Then
            rs.Edit
            pMth = pMth + 1
            rs.Fields("ForecastEndDate") = Format(pMth & "/1/" & pYear, "mm/dd/yyyy")
            rs.Update
        End If
        rs.MoveNext
    Loop
End Function

Open in new window

0
Comment
Question by:Karen Schaefer
  • 3
  • 2
6 Comments
 
LVL 2

Accepted Solution

by:
wobblynut earned 500 total points
ID: 34866989
Try replacing lines 19-27 with  

If rs.Fields("ForecastEndDate").value>date+366 then
            rs.Edit
            rs.Fields("ForecastEndDate") =  dateserial(year(date)+5,month(date), 1)
            rs.Update
        End If
       
0
 

Author Comment

by:Karen Schaefer
ID: 34867095
thanks that did the trick partly, I still need to check if the date in Forecast end date contains the current year - meaning the date was manually entered than I do not want to change that date.  Got any suggestions on how to look for current year in this field and if so skip it.

K
0
 
LVL 2

Assisted Solution

by:wobblynut
wobblynut earned 500 total points
ID: 34868829
What that above does is to look for anything within the next year rather than the current calendar year.  Else when you get to December, anything that you've given a January date will be overwritten.

If you definitely want t only leave the current calandar year then replace first line of previous solution with ....

If year(rs.Fields("ForecastEndDate").value)>year(date) then

hope that helps.
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 50

Expert Comment

by:Gustav Brock
ID: 34871508
You could and should operate with dates only:
Dim datForecast As Date
' ...
    Do Until rs.EOF
        datForecast = rs.Fields("ForecastEndDate").Value
        pYear = Year(datForecast)
        If pYear > Year(Date) Then
          rs.Edit
            rs.Fields("ForecastEndDate") = DateAdd("m", 1, datForecast)
          rs.Update
        End If
        rs.MoveNext
    Loop

Open in new window


/gustav
0
 

Author Comment

by:Karen Schaefer
ID: 34873564
Gustav,

This doesn't give me a running date = Current date (month and Year)  plus 60 months.  the date in question will always be the first of the month.

K
0
 

Author Closing Comment

by:Karen Schaefer
ID: 34873677
after retesting the suggestions these 2 did the trick.

Thanks for the input.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

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…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

679 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