?
Solved

Change a month of a date on a recordset

Posted on 2011-02-10
6
Medium Priority
?
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 2

Accepted Solution

by:
wobblynut earned 2000 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 2000 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 51

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
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.
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…
Suggested Courses

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