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 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....
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 Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
|
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...
|
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.
|
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.
|
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...
| |
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...
|
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 .
|
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...
|
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
|
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...
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| | |