473,385 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using VBA. Trying to subtract dates from one another.

I am using windows 2000pro with access 2000.

I am trying to make a database for our HR department, that would allow our HR Director to type in an employee's ID number into a form and then select the dates the employee took off from work.

(I have the calander add-in 8.0 setup to easily select the dates already) There are two Date Fields, "Leave Date", and "Return Date" where the calander selections are stored. They are in the 12/25/2006 Format.

There then is a combo box field for the "Type of Absence" the employee took. Options such as "Paid Time Off", "Unpaid Absence", "Paid Vacation",and "Volunteer Hours" are the choices in the combo box.

The HR Director asked if I could have the "Return Date" field value, subtracted from the "Leave Date" field value. Thus giving me the total number of days taken off.

So, Step 1 would be to have the dates subtracted from on another

Step 2. Is to have the total number of days taken off, broke down into hours.

Step3. Depends on wether or not "Paid Vacation" is selected for the "Type of Absence field". Because Paid Vacation is saved up, I plan to make another table for those hours to be manually stored in.

Step 4 will Idealy subtract the hours the employee has taken off from the stored vacation hours. That is all assuming that Paid Vacation is selected.
If "Unpaid Absence" or "Volunteer hours" is selected, they just need totaled up in a table.

Hopein you got all that, my main question, is;

What steps will it take codewise to make my Form subtract the two calendar dates from one another? i.e. "Leave Date" = 12/20/2006 and "Return Date" = 12/23/2006. Subtracting them, should give me 3 days off. Not sure how to start that one.

From there, I have the program down in theory...3 days * 8 hrs a day, so 24 hours which is then subtracted from the X amount of hours stored in the employee's Paid Vacation field.

I am just learning VBA and I am still just a bit uneasy with things. Input is greatly appreciated.
Jan 3 '07 #1
8 12804
MMcCarthy
14,534 Expert Mod 8TB
I am using windows 2000pro with access 2000.

I am trying to make a database for our HR department, that would allow our HR Director to type in an employee's ID number into a form and then select the dates the employee took off from work.

(I have the calander add-in 8.0 setup to easily select the dates already) There are two Date Fields, "Leave Date", and "Return Date" where the calander selections are stored. They are in the 12/25/2006 Format.

There then is a combo box field for the "Type of Absence" the employee took. Options such as "Paid Time Off", "Unpaid Absence", "Paid Vacation",and "Volunteer Hours" are the choices in the combo box.

The HR Director asked if I could have the "Return Date" field value, subtracted from the "Leave Date" field value. Thus giving me the total number of days taken off.

So, Step 1 would be to have the dates subtracted from on another

Step 2. Is to have the total number of days taken off, broke down into hours.

Step3. Depends on wether or not "Paid Vacation" is selected for the "Type of Absence field". Because Paid Vacation is saved up, I plan to make another table for those hours to be manually stored in.

Step 4 will Idealy subtract the hours the employee has taken off from the stored vacation hours. That is all assuming that Paid Vacation is selected.
If "Unpaid Absence" or "Volunteer hours" is selected, they just need totaled up in a table.

Hopein you got all that, my main question, is;

What steps will it take codewise to make my Form subtract the two calendar dates from one another? i.e. "Leave Date" = 12/20/2006 and "Return Date" = 12/23/2006. Subtracting them, should give me 3 days off. Not sure how to start that one.

From there, I have the program down in theory...3 days * 8 hrs a day, so 24 hours which is then subtracted from the X amount of hours stored in the employee's Paid Vacation field.

I am just learning VBA and I am still just a bit uneasy with things. Input is greatly appreciated.
Expand|Select|Wrap|Line Numbers
  1. DateDiff ('d', Format([Leave Date], '\#dd/mm/yyyy\#'), Format([Return Date], '\#dd/mm/yyyy\#'))
This should give you the three days required.

Mary
Jan 4 '07 #2
NeoPa
32,556 Expert Mod 16PB
Expand|Select|Wrap|Line Numbers
  1. DateDiff ('d', Format([Leave Date], '\#dd/mm/yyyy\#'), Format([Return Date], '\#dd/mm/yyyy\#'))
This should give you the three days required.

Mary
Nice to see the delimiters in there nicely Mary. If I'm being picky (Moi? - Jamais!) the date format should be m/d rather than d/m in SQL. Otherwise it's perfect.
In fact, in this case, literals are probably not required though. That means you should get away with simply :
Expand|Select|Wrap|Line Numbers
  1. DateDiff ('d', [Leave Date], [Return Date])
This code can be used even within your SQL.

One thing I would point out is that this makes no allowance for weekends or holidays. That is a whole new can of worms and frequently requires special VBA code to handle.
Jan 4 '07 #3
Thanks for the help, though I am still missing something. I have my "LeaveDate" and "ReturnDate" fields, and the calendar coded as so;

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Dim nlbOriginator As ComboBox
  4.  
  5. Private Sub LeaveDate_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  6.     Set nlbOriginator = LeaveDate
  7.     Calendar.Visible = True
  8.     Calendar.SetFocus
  9.     If Not IsNull(nlbOriginator) Then
  10.    Calendar.Value = nlbOriginator.Value
  11. Else
  12.    Calendar.Value = Date
  13. End If
  14.  
  15. End Sub
  16.  
  17. Private Sub ReturnDate_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  18.     Set nlbOriginator = ReturnDate
  19.     Calendar.Visible = True
  20.     Calendar.SetFocus
  21.     If Not IsNull(nlbOriginator) Then
  22.         Calendar.Value = nlbOriginator.Value
  23.     Else
  24.         Calendar.Value = Date
  25.     End If
  26. End Sub
  27.  
  28. Private Sub Calendar_Click()
  29.     nlbOriginator.Value = Calendar.Value
  30.     nlbOriginator.SetFocus
  31.     Calendar.Visible = False
  32.     Set nlbOriginator = Nothing
  33. End Sub
The text box, I have set to recieve the calculated number of days gone, is called "DaysGone". I assume I need to set another global value, to hold the value of the two dates being subtracted? Then I make another sub procedure, for the calendar or for the "DaysGone" field? (It would be there that I insert your listed code?)

I could be way off, any thoughts?

Thanks,
Remington
Jan 4 '07 #4
MMcCarthy
14,534 Expert Mod 8TB
Thanks for the help, though I am still missing something. I have my "LeaveDate" and "ReturnDate" fields, and the calendar coded as so;

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Dim nlbOriginator As ComboBox
  4.  
  5. Private Sub LeaveDate_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  6.     Set nlbOriginator = LeaveDate
  7.     Calendar.Visible = True
  8.     Calendar.SetFocus
  9.     If Not IsNull(nlbOriginator) Then
  10.    Calendar.Value = nlbOriginator.Value
  11. Else
  12.    Calendar.Value = Date
  13. End If
  14.  
  15. End Sub
  16.  
  17. Private Sub ReturnDate_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  18.     Set nlbOriginator = ReturnDate
  19.     Calendar.Visible = True
  20.     Calendar.SetFocus
  21.     If Not IsNull(nlbOriginator) Then
  22.         Calendar.Value = nlbOriginator.Value
  23.     Else
  24.         Calendar.Value = Date
  25.     End If
  26. End Sub
  27.  
  28. Private Sub Calendar_Click()
  29.     nlbOriginator.Value = Calendar.Value
  30.     nlbOriginator.SetFocus
  31.     Calendar.Visible = False
  32.     Set nlbOriginator = Nothing
  33. End Sub
The text box, I have set to recieve the calculated number of days gone, is called "DaysGone". I assume I need to set another global value, to hold the value of the two dates being subtracted? Then I make another sub procedure, for the calendar or for the "DaysGone" field? (It would be there that I insert your listed code?)

I could be way off, any thoughts?

Thanks,
Remington
If you have the field "DaysGone" on your form. Then you just have to decide when you want to trigger this event. You could put the code behind a command button and just code as follows.

Expand|Select|Wrap|Line Numbers
  1. Private Sub CommandXX_Click()
  2.    Me.DaysGone = DateDiff ('d', [Leave Date], [Return Date])
  3. End Sub
  4.  
Mary
Jan 5 '07 #5
Expand|Select|Wrap|Line Numbers
  1. Me.DaysGone = DateDiff ('d', [Leave Date], [Return Date])

I am not having any luck getting this to work. I keep getting an error, wich takes me to the (' just before the d',[Leave Date]

Other questions: What is the Me. ??

Sorry to be a pain ;)
Jan 17 '07 #6
MMcCarthy
14,534 Expert Mod 8TB
Me.DaysGone = DateDiff ('d', [Leave Date], [Return Date])


I am not having any luck getting this to work. I keep getting an error, wich takes me to the (' just before the d',[Leave Date]

Other questions: What is the Me. ??

Sorry to be a pain ;)
Not a problem.

Me. is just a way of referring to the current form.

Sorry this was my error. Try this instead ...

Expand|Select|Wrap|Line Numbers
  1. Me.DaysGone = DateDiff ("d", [Leave Date], [Return Date])
Mary
Jan 17 '07 #7
Awesome! Thanks Mary, your an angel.

It works great now, much thanks.
Jan 17 '07 #8
MMcCarthy
14,534 Expert Mod 8TB
Awesome! Thanks Mary, your an angel.

It works great now, much thanks.
You're welcome!
Jan 17 '07 #9

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

Similar topics

3
by: Ken | last post by:
$expirationdate = 4/15/2005 $startdate = 3/1/2005 I would like to determine the number of days between the above dates. My approach: $startdate = strtotime($startdate); // change to a string...
1
by: sylvian stone | last post by:
Hi, I've used standard date functions in the past, but need to create something a little different, as I am working on an investment calculator. What I need to do is validate two dates, and...
16
by: John Hanley | last post by:
I created a function that breaks down a date into broken down time, I subtract a certain number of seconds from that, then use mktime() to recompute the calendar time. It works basically except...
4
by: Russell | last post by:
I have an assignment that I have to complete. I have to write a windows app in C#. Here is the spec: 1/ Date Comparison Build a program that will find the number of days between two dates. You...
16
by: Atley | last post by:
I am trying to get a overall difference on two dates, I can get the difference in Years, Months, Weeks, Days, Hours, Minutes, Seconds, no problems... What I cannot seem to figure out is how to...
5
by: Larry Bird | last post by:
I want to use TimeSpan to determine the differences between two date to include time. My input data is in the following format: 12/25/2004 12:23:00 AM or 01/05/2005 11:59:00 PM How do I get...
6
by: Brandon | last post by:
I'm using PHP with MySQL 4.x and was having trouble converting a datetime from MySQL into a formatted string until I ran across this solution that converts a YYYY-MM-DD HH:MM:SS string into a...
10
by: dan | last post by:
Am i breaking any rules when I loop dates like // Determine Memorial Day intFlag = 0; memDayHol = new Date (currentYear, 4, 31); while (intFlag == 0) { if (memDayHol.getDay() == 1) {intFlag...
1
by: learning | last post by:
Hi how can I instaltiate a class and call its method. the class has non default constructor. all examples i see only with class of defatul constructor. I am trying to pull the unit test out from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...

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.