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.
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....
twinnyfo 3,653
Recognized Expert Moderator Specialist
You could try this: -
Option Compare Database
-
Option Explicit
-
-
Private Sub cmdCheckDate_Click()
-
On Error GoTo EH
-
If Not (IsNull(Me.txtFromDate) Or IsNull(Me.txtToDate)) And _
-
Me.txtFromDate < Me.txtToDate Then
-
Dim strDifference As String
-
Dim YearFrom As Integer
-
Dim YearTo As Integer
-
Dim MonthFrom As Integer
-
Dim MonthTo As Integer
-
Dim WeekFrom As Integer
-
Dim WeekTo As Integer
-
Dim DayFrom As Integer
-
Dim DayTo As Integer
-
Dim YearDifference As Integer
-
Dim MonthDifference As Integer
-
Dim WeekDifference As Integer
-
Dim DayDifference As Integer
-
Dim dtTemp As Date
-
YearFrom = Year(Me.txtFromDate)
-
YearTo = Year(Me.txtToDate)
-
MonthFrom = Month(Me.txtFromDate)
-
MonthTo = Month(Me.txtToDate)
-
DayFrom = Day(Me.txtFromDate)
-
DayTo = Day(Me.txtToDate)
-
-
If YearFrom < YearTo Then
-
If MonthFrom < MonthTo Then
-
YearDifference = YearTo - YearFrom
-
Else
-
YearDifference = YearTo - YearFrom - 1
-
End If
-
Else
-
strDifference = "0 years, "
-
End If
-
If MonthFrom < MonthTo Then
-
If DayFrom <= DayTo Then
-
MonthDifference = MonthTo - MonthFrom
-
Else
-
MonthDifference = MonthTo - MonthFrom - 1
-
End If
-
Else
-
If YearFrom < YearTo Then
-
If DayFrom <= DayTo Then
-
MonthDifference = MonthTo - MonthFrom + 12
-
Else
-
MonthDifference = MonthTo - MonthFrom + 11
-
End If
-
Else
-
MonthDifference = 0
-
End If
-
End If
-
dtTemp = DateAdd("yyyy", -YearDifference, Me.txtToDate)
-
dtTemp = DateAdd("m", -MonthDifference, dtTemp)
-
DayDifference = dtTemp - Me.txtFromDate
-
Select Case DayDifference
-
Case Is < 7
-
WeekDifference = 0
-
Case 7 To 13
-
WeekDifference = 1
-
DayDifference = DayDifference - 7
-
Case 14 To 20
-
WeekDifference = 2
-
DayDifference = DayDifference - 14
-
Case 21 To 27
-
WeekDifference = 3
-
DayDifference = DayDifference - 21
-
Case Is >= 28
-
WeekDifference = 4
-
DayDifference = DayDifference - 28
-
End Select
-
strDifference = YearDifference & " year" & _
-
IIf((YearDifference > 1 Or YearDifference = 0), "s, ", ", ") & _
-
MonthDifference & " month" & _
-
IIf((MonthDifference > 1 Or MonthDifference = 0), "s, ", ", ") & _
-
WeekDifference & " week" & _
-
IIf((WeekDifference > 1 Or WeekDifference = 0), "s, ", ", ") & _
-
DayDifference & " day" & _
-
IIf((DayDifference > 1 Or DayDifference = 0), "s", "")
-
Me.TxtDateDifference = strDifference
-
End If
-
Exit Sub
-
EH:
-
MsgBox Err.Number & " " & Err.Description
-
Exit Sub
-
End Sub
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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. ...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| | |