473,652 Members | 3,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Date function, need to return Days/Weeks/Months from a given two dates...

1 New Member
Guys,

I urgently need to create a date function in VBA/Access that will take in two different date params and will return a string value like this...

"1 Months, 2 Weeks, 3 Days"

This will need return the time difference between the two date ranges in the above format...

If any of you guys could give some assistance, that would be great!

Rob.
Aug 6 '12 #1
3 1677
twinnyfo
3,653 Recognized Expert Moderator Specialist
Have you tried any code on this yet? Plesae show us what you have to start and I would be glad to assist....
Aug 7 '12 #2
twinnyfo
3,653 Recognized Expert Moderator Specialist
You could try this:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub cmdCheckDate_Click()
  5. On Error GoTo EH
  6.     If Not (IsNull(Me.txtFromDate) Or IsNull(Me.txtToDate)) And _
  7.         Me.txtFromDate < Me.txtToDate Then
  8.         Dim strDifference As String
  9.         Dim YearFrom As Integer
  10.         Dim YearTo As Integer
  11.         Dim MonthFrom As Integer
  12.         Dim MonthTo As Integer
  13.         Dim WeekFrom As Integer
  14.         Dim WeekTo As Integer
  15.         Dim DayFrom As Integer
  16.         Dim DayTo As Integer
  17.         Dim YearDifference As Integer
  18.         Dim MonthDifference As Integer
  19.         Dim WeekDifference As Integer
  20.         Dim DayDifference As Integer
  21.         Dim dtTemp As Date
  22.         YearFrom = Year(Me.txtFromDate)
  23.         YearTo = Year(Me.txtToDate)
  24.         MonthFrom = Month(Me.txtFromDate)
  25.         MonthTo = Month(Me.txtToDate)
  26.         DayFrom = Day(Me.txtFromDate)
  27.         DayTo = Day(Me.txtToDate)
  28.  
  29.         If YearFrom < YearTo Then
  30.             If MonthFrom < MonthTo Then
  31.                 YearDifference = YearTo - YearFrom
  32.             Else
  33.                 YearDifference = YearTo - YearFrom - 1
  34.             End If
  35.         Else
  36.             strDifference = "0 years, "
  37.         End If
  38.         If MonthFrom < MonthTo Then
  39.             If DayFrom <= DayTo Then
  40.                 MonthDifference = MonthTo - MonthFrom
  41.             Else
  42.                 MonthDifference = MonthTo - MonthFrom - 1
  43.             End If
  44.         Else
  45.             If YearFrom < YearTo Then
  46.                 If DayFrom <= DayTo Then
  47.                     MonthDifference = MonthTo - MonthFrom + 12
  48.                 Else
  49.                     MonthDifference = MonthTo - MonthFrom + 11
  50.                 End If
  51.             Else
  52.                 MonthDifference = 0
  53.             End If
  54.         End If
  55.         dtTemp = DateAdd("yyyy", -YearDifference, Me.txtToDate)
  56.         dtTemp = DateAdd("m", -MonthDifference, dtTemp)
  57.         DayDifference = dtTemp - Me.txtFromDate
  58.         Select Case DayDifference
  59.             Case Is < 7
  60.                 WeekDifference = 0
  61.             Case 7 To 13
  62.                 WeekDifference = 1
  63.                 DayDifference = DayDifference - 7
  64.             Case 14 To 20
  65.                 WeekDifference = 2
  66.                 DayDifference = DayDifference - 14
  67.             Case 21 To 27
  68.                 WeekDifference = 3
  69.                 DayDifference = DayDifference - 21
  70.             Case Is >= 28
  71.                 WeekDifference = 4
  72.                 DayDifference = DayDifference - 28
  73.         End Select
  74.         strDifference = YearDifference & " year" & _
  75.             IIf((YearDifference > 1 Or YearDifference = 0), "s, ", ", ") & _
  76.             MonthDifference & " month" & _
  77.             IIf((MonthDifference > 1 Or MonthDifference = 0), "s, ", ", ") & _
  78.             WeekDifference & " week" & _
  79.             IIf((WeekDifference > 1 Or WeekDifference = 0), "s, ", ", ") & _
  80.             DayDifference & " day" & _
  81.             IIf((DayDifference > 1 Or DayDifference = 0), "s", "")
  82.         Me.TxtDateDifference = strDifference
  83.     End If
  84.     Exit Sub
  85. EH:
  86.     MsgBox Err.Number & " " & Err.Description
  87.     Exit Sub
  88. End Sub
  89.  
Aug 7 '12 #3
zmbd
5,501 Recognized Expert Moderator Expert
@rtiddy
As asked please post your work.

This is almost eactly like a homework question my compsci prof had us do in FORTRAN some two decades ago.
I've got this lying around in an old text file and once you post your work I'll take the time to convert it to VBA, I suspect that it should only take 18 lines... counting the DIM and OPTION statements!

-z
PS:
You can also do this as a series of calculated fields in a query :)

-z
Aug 9 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
3020
by: Dennis M. Marks | last post by:
What is the maximum valid date range for the date(yyyy,mm,dd) function. -- Dennis M. Marks http://www.dcs-chico.com/~denmarks/ Replace domain.invalid with dcsi.net -----= Posted via Newsfeeds.Com, Uncensored Usenet News =----- http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
5
2421
by: Dennis M. Marks | last post by:
After reading section 15.9.1.1 the ECMAScript Language Specifications I see that the date range for the Date function is +/- 100,000,000 days from 01 Jan 1970. This is called an extrapolated Gregorian calendar. Since the Gregorian calendar did not begin until 15 Oct 1582 what is the purpose of dates before that date? Wouldn't any computation prior to that date be meaningless or am I missing something? The reason I ask is that I have...
2
1841
by: JAPIO | last post by:
I have two variables which look like: $begin = "23-08-05" $end = "26-08-05" Now i want also to know the dates between them. 24-08-05 25-08-05 How can i do this? Any help would be appreciated.
3
3345
by: MMFBprez | last post by:
I am trying to compute storage charges by getting the number of months between dates and multiplying it by a rate. I cannot get a correct number of months if the date is greater than a year ago. Here is the formula I am using now to get the number of months: #ofMonths: Month(.-.)-1. It has worked for me until now because it never was greater than a year before. Please help. I am a novice at this.
13
2155
by: Sara | last post by:
I have a query that pulls data for the month, using the Month End Date from a form (user enters) as criteria. It works. I want to use the same query to pull "month to date" data, on a weekly basis. So, for week 1, it would only show the first 7 days; week 2 the first 14 days, etc. If I enter the next month ending date (1/1/05) right now, I get the information for days since the last month end until current date. That's perfect. My...
29
9097
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An example is: 36,525 represents 01/01/1900. The starting point date is considered to be : 00/00/0000. I have looked thru Help and used Google and have not really found an answer. I know that Leap Years need to be accounted for too. Any...
3
265
by: Eduardo78 | last post by:
I am trying to find out any function or code to calculate days between to dates, and another one that I select a date and then select a number of days, weeks, or years from a control, and I will bring me what they It would be after the number of days or weeks selected. Lets say for example I would like to know what date it would be after 20 days from todays date. Thanks for any suggestions .
9
2241
by: Charnondall | last post by:
Hi guys, this is gonna sound very simple but got a slight problem with the critera of one of my database's. Basically i am looking to have a criteria in a Query that returns all the database fields that are 7 days old or less. Before i was using ">Date()-7" but since it turned September it's no longer working at it wont show any of the September database entries only the August ones that are still within the 7 day gap, ie. 29th, 30th and...
3
19460
by: PerumalSamy | last post by:
Hi I am developing a project in asp.net using vb coding. I need to add days/weeks/months to existing date in asp.net. Ex: existing date : 24/10/2007 Add : 10 days or 2 weeks or 3 months
4
3053
by: OzNet | last post by:
I have some functions to calculate the working days in a given period. This includes a table that is queried to calculate the number of public holidays that don’t occur on a weekend. If I test the function using the intermediate window, it works fine. However, when I pass the dates from the code attached to my form, the results are inaccurate. You will notice my dates are in Australian format. Everything works fine using the Australian...
0
8367
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8279
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8589
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7302
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2703
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.