Solved

Change a month of a date on a recordset

Posted on 2011-02-10
6
298 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 49

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

777 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