Solved

Change a month of a date on a recordset

Posted on 2011-02-10
6
297 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
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
Comment Utility
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
Comment Utility
after retesting the suggestions these 2 did the trick.

Thanks for the input.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now