473,503 Members | 3,308 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 1667
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
2998
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...
5
2405
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...
2
1832
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...
3
3330
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. ...
13
2136
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...
29
9028
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...
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...
9
2237
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...
3
19443
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...
4
3036
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...
0
7192
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,...
0
7064
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...
0
7261
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,...
0
7315
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7445
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...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1492
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 ...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.