473,387 Members | 1,575 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,387 software developers and data experts.

Calculating days!

14
Hi guys, good to be here... I am struggling to calculated days in invoice system...Ok...The dates fields are of two kinds...One is the invoice start date and end date... The other one is the contract start date and end date...I have to calculate days of charging according to the dates involved...
Suppose INVOICE start date: 01-01-07; INVOICE end date: 31-01-07

Conditions are:

1. IF the contract start date is BEFORE(<=) the invoice start date AND contract end date is AFTER(>=) or equal to invoice end date THEN use the invoice start and end dates for calculation.

2. IF contract start date is AFTER(>) the invoice start date AND contract end date is BEFORE (<) the invoice end date THEN use contract start date and contract end date for calculation.

3. IF contract start date is AFTER(>) the invoice start date AND contract start date is BEFORE (<) the invoice end date THEN use contract start date and invoice end date for calculation.

4. IF contract start date is BEFORE(<) the invoice start date AND contract end date is also BEFORE (<) the invoice end date THEN use Invoice start date and contract end date for calculation.

5. IF contract start date is AFTER (>) the invoice end date THEN do not show the record.

6. IF contract end date is BEFORE (<) the invoice start date THEN do not show the records.

I know it’s a bit of nuisance, to me it is :P... Well it’s been 3 days working on it and I am not that much familiar with Access... Any kind of help will be appreciated... Thanking you guys in advance...
Jul 12 '07 #1
13 2381
ADezii
8,834 Expert 8TB
Hi guys, good to be here... I am struggling to calculated days in invoice system...Ok...The dates fields are of two kinds...One is the invoice start date and end date... The other one is the contract start date and end date...I have to calculate days of charging according to the dates involved...
Suppose INVOICE start date: 01-01-07; INVOICE end date: 31-01-07

Conditions are:

1. IF the contract start date is BEFORE(<=) the invoice start date AND contract end date is AFTER(>=) or equal to invoice end date THEN use the invoice start and end dates for calculation.

2. IF contract start date is AFTER(>) the invoice start date AND contract end date is BEFORE (<) the invoice end date THEN use contract start date and contract end date for calculation.

3. IF contract start date is AFTER(>) the invoice start date AND contract start date is BEFORE (<) the invoice end date THEN use contract start date and invoice end date for calculation.

4. IF contract start date is BEFORE(<) the invoice start date AND contract end date is also BEFORE (<) the invoice end date THEN use Invoice start date and contract end date for calculation.

5. IF contract start date is AFTER (>) the invoice end date THEN do not show the record.

6. IF contract end date is BEFORE (<) the invoice start date THEN do not show the records.

I know it’s a bit of nuisance, to me it is :P... Well it’s been 3 days working on it and I am not that much familiar with Access... Any kind of help will be appreciated... Thanking you guys in advance...
I will post the solution in a day or two, as soon as I get the opportunity to work on it.
Jul 12 '07 #2
MikeTheBike
639 Expert 512MB
Hi

I think this might be something like what tou want, if I understant it correctly

Expand|Select|Wrap|Line Numbers
  1. SELECT  
  2.     Field1, Field2, ....,
  3.     IIF(ConStartDate<= InvStartDate, InvStartDate,ConStartDate)-IIF(ConEndDate>InvEndDate,InvEndDate,ConEndDate) As NumDays
  4. FROM YourTable  
  5. WHERE ConStartDate<=InvEndDate AND ConEndDate >= InvStatDate
Does that do it??


MTB
Jul 12 '07 #3
kepston
97 Expert
Hi

I think this might be something like what tou want, if I understant it correctly

Expand|Select|Wrap|Line Numbers
  1. SELECT  
  2.     Field1, Field2, ....,
  3.     IIF(ConStartDate<= InvStartDate, InvStartDate,ConStartDate)-IIF(ConEndDate>InvEndDate,InvEndDate,ConEndDate) As NumDays
  4. FROM YourTable  
  5. WHERE ConStartDate<=InvEndDate AND ConEndDate >= InvStatDate
Does that do it??


MTB
Maybe. But you'll get a negative number taking the end date from the start date.
Try
Expand|Select|Wrap|Line Numbers
  1. SELECT  
  2.     Field1, Field2, ....,
  3.     IIF(ConEndDate>InvEndDate,InvEndDate,ConEndDate) - IIF(ConStartDate<= InvStartDate, InvStartDate,ConStartDate) As NumDays
  4. FROM YourTable  
  5. WHERE ConStartDate<=InvEndDate AND ConEndDate >= InvStartDate
Jul 12 '07 #4
drago
14
Guys thank your for your prompt replies...u ppl are amazing...let me try what MTB and Kepston have suggested...I will get back to you soon. If you ask me I can see the hurdle here... Now each client have 1 or more than one contracts and for that I am using a subform in invoice form... so at the moment it is bringing all the contract records related to the selected client by using the invoice ID...the calculation is done on the sub forms.... Anyway let me try this first...

Thank you once again guys...you are life savers....
Jul 12 '07 #5
ADezii
8,834 Expert 8TB
Guys thank your for your prompt replies...u ppl are amazing...let me try what MTB and Kepston have suggested...I will get back to you soon. If you ask me I can see the hurdle here... Now each client have 1 or more than one contracts and for that I am using a subform in invoice form... so at the moment it is bringing all the contract records related to the selected client by using the invoice ID...the calculation is done on the sub forms.... Anyway let me try this first...

Thank you once again guys...you are life savers....
  1. Assumptions
    1. Table Name: tblInvoice
    2. Field Name: Contract Start Date
    3. Field Name: Contract End Date
    4. Field Name: Invoice Start Date
    5. Field Name: Invoice End Date
  2. Query ==> New ==> Design View ==> SQL View
  3. Paste the following SQL String into the SQL View Window
    Expand|Select|Wrap|Line Numbers
    1. SELECT [Contract Start Date], [Contract End Date], [Invoice Start Date], [Invoice End Date], 
    2. fCalculateDays([Contract Start Date],[Contract End Date],[Invoice Start Date],[Invoice End Date]) AS Days_Differential
    3. FROM tblInvoice
    4. WHERE fCalculateDays([Contract Start Date],[Contract End Date],[Invoice Start Date],[Invoice End Date])<>999999;
  4. This Query consists of the 4 Date Fields and a Calculated Field named [Days_Differential] which calls the fCalculateDays() Function. This Function is passed all 4 Dates as Arguments and returns the appropriate Response including a Value of -32767 for any generated Errors in the Routine. On those conditions where no Records are to be returned, (conditions 5 and 6), this Function returns 999999. Criteria specified in the Query itself, namely <> 999999, will now allow these Records to be shown. All Logic and Data Comparisons are performed within the Function itself with the Return Value populating the Calculated Field, (Days_Differential). The code is basically self-documenting, but if you are confused on anything, please feel free to ask. You must, of course, Copy and Paste the Function Procedure below to a Standard Code Module. This Function must be declared as Public in order to be functional. It has been debugged and is fully operational, I just hope it provides the answer to your dilemma. Let me know how you make out.
    Expand|Select|Wrap|Line Numbers
    1. Public Function fCalculateDays(dteContractStart As Date, dteContractEnd As Date, dteInvoiceStart As Date, dteInvoiceEnd As Date)
    2. On Error GoTo Err_fCalculateDays
    3.  
    4. 'Satifies Condition #1 in outline
    5. If (dteContractStart <= dteInvoiceStart) And (dteContractEnd >= dteInvoiceEnd) Then
    6.   fCalculateDays = DateDiff("d", dteInvoiceStart, dteInvoiceEnd)
    7. 'Satifies Condition #2 in outline
    8. ElseIf (dteContractStart > dteInvoiceStart) And (dteContractEnd < dteInvoiceEnd) Then
    9.   fCalculateDays = DateDiff("d", dteContractStart, dteContractEnd)
    10. 'Satifies Condition #3 in outline
    11. ElseIf (dteContractStart > dteInvoiceStart) And (dteContractStart < dteInvoiceEnd) Then
    12.   fCalculateDays = DateDiff("d", dteContractStart, dteInvoiceEnd)
    13. 'Satifies Condition #4 in outline
    14. ElseIf (dteContractStart < dteInvoiceStart) And (dteContractEnd < dteInvoiceEnd) Then
    15.   fCalculateDays = DateDiff("d", dteInvoiceStart, dteContractEnd)
    16. Else
    17.   fCalculateDays = 999999       'Doesn't fit in any Category - DON'T SHOW? ------------|
    18. End If                          '                                                      |
    19.                                 '                                                      |
    20.                                 '                                                      |
    21. 'Satifies Conditions 5 and 6 in outline - Higher Priority then Items 1 thru 4          |
    22. If (dteContractStart > dteInvoiceEnd) Or (dteContractEnd < dteInvoiceStart) Then  '    |
    23.   fCalculateDays = 999999       'DON'T SHOW (filtered in the Query) -------------------|
    24. End If
    25.  
    26. Exit_fCalculateDays:
    27.   Exit Function
    28.  
    29. Err_fCalculateDays:
    30.   fCalculateDays = -32767       'Error indicator
    31.   Resume Exit_fCalculateDays
    32. End Function
Jul 12 '07 #6
MikeTheBike
639 Expert 512MB
Maybe. But you'll get a negative number taking the end date from the start date
OOPS!

Close but no cigar.

MTB
Jul 13 '07 #7
drago
14
Hi Adezii thank you very much for your help. I have created queries now i will try to create the logic which you have written... I will send you the feedback once its working or working...
Thanks again :)
Jul 13 '07 #8
ADezii
8,834 Expert 8TB
Hi Adezii thank you very much for your help. I have created queries now i will try to create the logic which you have written... I will send you the feedback once its working or working...
Thanks again :)
We're here whenever you need us.
Jul 13 '07 #9
drago
14
We're here whenever you need us.
Hi fellows! Me back. I tried the later code, when i run it, it says "Data type mismatch in criteria expression". Iam sorry, I should have told you guys earlier about my Access , so it Ms Access 07 using vista 07.

I have few queries which run fine. They are:

1. The result of this query will allow invoice system to use invoice start date and end date.

SQL code:
SELECT Invoice.invoice_from_date, Invoice.invoice_to_date, Contract_Elements.c_e_start_date, Contract_Elements.c_e_end_date, Contract_Elements.c_e_total_days
FROM Invoice LEFT JOIN Contract_Elements ON Invoice.invoice_ID = Contract_Elements.invoice_ID
WHERE (((Contract_Elements.c_e_start_date)<=([Invoice].[invoice_from_date])) AND ((Contract_Elements.c_e_end_date)>=([Invoice].[invoice_to_date])));

2. The result of this give you all contracts between invoicing date. So the contract start and end date will be used to calculate No. of days

SELECT Invoice.invoice_from_date, Invoice.invoice_to_date, Contract_Elements.c_e_start_date, Contract_Elements.c_e_end_date, Contract_Elements.c_e_total_days
FROM Invoice LEFT JOIN Contract_Elements ON Invoice.invoice_ID = Contract_Elements.invoice_ID
WHERE (((Contract_Elements.c_e_start_date)>([Invoice].[invoice_from_date])) AND ((Contract_Elements.c_e_end_date)<([Invoice].[invoice_to_date])));

3. This query should later on use values of contract start date and the invoice end date

SELECT Invoice.invoice_from_date, Invoice.invoice_to_date, Contract_Elements.c_e_start_date, Contract_Elements.c_e_end_date, Contract_Elements.c_e_total_days
FROM Invoice LEFT JOIN Contract_Elements ON Invoice.invoice_ID = Contract_Elements.invoice_ID
WHERE ( (Contract_Elements.c_e_start_date)>([Invoice].[invoice_from_date]) AND (Contract_Elements.c_e_end_date)> ([Invoice].[invoice_to_date]) AND (Contract_Elements.c_e_start_date) < ([Invoice].[invoice_to_date]) );

4. This is opposite of the above. should use values of invoice start date and contract end date

SELECT Invoice.invoice_from_date, Invoice.invoice_to_date, Contract_Elements.c_e_start_date, Contract_Elements.c_e_end_date, Contract_Elements.c_e_total_days
FROM Invoice LEFT JOIN Contract_Elements ON Invoice.invoice_ID = Contract_Elements.invoice_ID
WHERE ( (Contract_Elements.c_e_end_date)<([Invoice].[invoice_to_date]) AND (Contract_Elements.c_e_start_date)< ([Invoice].[invoice_from_date]) AND (Contract_Elements.c_e_end_date) > ([Invoice].[invoice_from_date]) );

5. This should not be excluded from the invoice. Old dates less than the contract start

SELECT Invoice.invoice_from_date, Invoice.invoice_to_date, Contract_Elements.c_e_start_date, Contract_Elements.c_e_end_date, Contract_Elements.c_e_total_days
FROM Invoice LEFT JOIN Contract_Elements ON Invoice.invoice_ID = Contract_Elements.invoice_ID
WHERE ( (Contract_Elements.c_e_end_date)<([Invoice].[invoice_from_date]) );


6. This should not be excluded from the invoice. Future dates greater than the contract end date

SELECT Invoice.invoice_from_date, Invoice.invoice_to_date, Contract_Elements.c_e_start_date, Contract_Elements.c_e_end_date, Contract_Elements.c_e_total_days
FROM Invoice LEFT JOIN Contract_Elements ON Invoice.invoice_ID = Contract_Elements.invoice_ID
WHERE ( (Contract_Elements.c_e_start_date)>([Invoice].[invoice_to_date]) );


Now, can I use these queries in a vba to run appropriate query and to perform certain calculations on selected fields and save it in days column. All this will be done in subform.

My brain is totally out of order. Isnt it...Hope you are not confused...
Jul 16 '07 #10
drago
14
(whispering)..Hello?
Jul 19 '07 #11
ADezii
8,834 Expert 8TB
Hi fellows! Me back. I tried the later code, when i run it, it says "Data type mismatch in criteria expression". Iam sorry, I should have told you guys earlier about my Access , so it Ms Access 07 using vista 07.

I have few queries which run fine. They are:

1. The result of this query will allow invoice system to use invoice start date and end date.

SQL code:
SELECT Invoice.invoice_from_date, Invoice.invoice_to_date, Contract_Elements.c_e_start_date, Contract_Elements.c_e_end_date, Contract_Elements.c_e_total_days
FROM Invoice LEFT JOIN Contract_Elements ON Invoice.invoice_ID = Contract_Elements.invoice_ID
WHERE (((Contract_Elements.c_e_start_date)<=([Invoice].[invoice_from_date])) AND ((Contract_Elements.c_e_end_date)>=([Invoice].[invoice_to_date])));

2. The result of this give you all contracts between invoicing date. So the contract start and end date will be used to calculate No. of days

SELECT Invoice.invoice_from_date, Invoice.invoice_to_date, Contract_Elements.c_e_start_date, Contract_Elements.c_e_end_date, Contract_Elements.c_e_total_days
FROM Invoice LEFT JOIN Contract_Elements ON Invoice.invoice_ID = Contract_Elements.invoice_ID
WHERE (((Contract_Elements.c_e_start_date)>([Invoice].[invoice_from_date])) AND ((Contract_Elements.c_e_end_date)<([Invoice].[invoice_to_date])));

3. This query should later on use values of contract start date and the invoice end date

SELECT Invoice.invoice_from_date, Invoice.invoice_to_date, Contract_Elements.c_e_start_date, Contract_Elements.c_e_end_date, Contract_Elements.c_e_total_days
FROM Invoice LEFT JOIN Contract_Elements ON Invoice.invoice_ID = Contract_Elements.invoice_ID
WHERE ( (Contract_Elements.c_e_start_date)>([Invoice].[invoice_from_date]) AND (Contract_Elements.c_e_end_date)> ([Invoice].[invoice_to_date]) AND (Contract_Elements.c_e_start_date) < ([Invoice].[invoice_to_date]) );

4. This is opposite of the above. should use values of invoice start date and contract end date

SELECT Invoice.invoice_from_date, Invoice.invoice_to_date, Contract_Elements.c_e_start_date, Contract_Elements.c_e_end_date, Contract_Elements.c_e_total_days
FROM Invoice LEFT JOIN Contract_Elements ON Invoice.invoice_ID = Contract_Elements.invoice_ID
WHERE ( (Contract_Elements.c_e_end_date)<([Invoice].[invoice_to_date]) AND (Contract_Elements.c_e_start_date)< ([Invoice].[invoice_from_date]) AND (Contract_Elements.c_e_end_date) > ([Invoice].[invoice_from_date]) );

5. This should not be excluded from the invoice. Old dates less than the contract start

SELECT Invoice.invoice_from_date, Invoice.invoice_to_date, Contract_Elements.c_e_start_date, Contract_Elements.c_e_end_date, Contract_Elements.c_e_total_days
FROM Invoice LEFT JOIN Contract_Elements ON Invoice.invoice_ID = Contract_Elements.invoice_ID
WHERE ( (Contract_Elements.c_e_end_date)<([Invoice].[invoice_from_date]) );


6. This should not be excluded from the invoice. Future dates greater than the contract end date

SELECT Invoice.invoice_from_date, Invoice.invoice_to_date, Contract_Elements.c_e_start_date, Contract_Elements.c_e_end_date, Contract_Elements.c_e_total_days
FROM Invoice LEFT JOIN Contract_Elements ON Invoice.invoice_ID = Contract_Elements.invoice_ID
WHERE ( (Contract_Elements.c_e_start_date)>([Invoice].[invoice_to_date]) );


Now, can I use these queries in a vba to run appropriate query and to perform certain calculations on selected fields and save it in days column. All this will be done in subform.

My brain is totally out of order. Isnt it...Hope you are not confused...
Sorry, but I am totally confused!
Jul 19 '07 #12
drago
14
Sorry, but I am totally confused!
Hi ADezii, sorry about that... its just iam new to access and trying to work out what i can... Sorry about that :)
Jul 20 '07 #13
If you have a datatype mismatch you are trying to reference to seperate things. I would pay close attention to the coding.
Jul 20 '07 #14

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

Similar topics

4
by: Hans Gruber | last post by:
Hi all, I have been struggling with a problem all day, I have been unable to come up with a working solution. I want to write a function which takes 2 unix timestamps and calculates the...
4
by: John | last post by:
hey all..... alright, I am frusterated to the point of throwing my machine out the window (this board went down, trying to find stuff on google, this has been a nightmare) so I hope you guys can...
12
by: Anthony Robinson | last post by:
Is anyone aware of a function (system or user defined) that will calculate business days? For instance: I have a column in as table called DATE. I want to be able to add five business days to that...
7
by: JLM | last post by:
I have a table that has fieldA, fieldB, fieldC. I want fieldC=fieldA-fieldB. simple enough. the next record I want to be able to do the same on the new value of fieldC. I can do this with SAP...
1
by: jlm | last post by:
I have a form which feeds table (TblEmpLeave) of Employee Leave Time (time taken off for Administrative, Annual, Sick, Compensation leave). I have EmpID, LeaveDate, LeaveType, LeaveHours fields on...
2
by: celsius | last post by:
Hi folks, Al Bowers wrote this program on comp.lang.c Date: 2001-07-09 13:41:58 PST #include <stdio.h> int isleap (unsigned yr); static unsigned months_to_days (unsigned month); static long...
3
by: Ron Vecchi | last post by:
I need to calculate the age of a person based on a DateTime BirthDate I was thinking TimeSpan ts = DateTime.Now - BirthDate; //I can get the days but not years. // I could check each...
4
realin
by: realin | last post by:
hi guys.. I have made a function which counts the numbers of days or hours or minutes from the current datetime to the give datetime.. but i am confused while displaying number of days along with...
8
by: =?Utf-8?B?QWw=?= | last post by:
I am working in vb2005. how can I calculate business days (not including holidays and weekends) between 2 dates? thanks Al
5
by: jjkeeper | last post by:
G'day, I'm currently working on an annual leave database for the company, so far so good, till they want the database to be able to identify public holidays and prevent reducing the employee's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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 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.