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

Subtract dates in subform

I have a table with the fields -
Income_Date Income.
19/3/07 $320
21/2/07 $200
27/1/07 $300

In a continuous subform, I need to show for each income period the average weekly income on each line. As far as I can figure so far, I need to use the datediff function to calculate the difference between the two dates, and then loop to the next record and repeat. I just have no idea how to achieve this.

Please can somebody help me with the code I need to use, and how to use it? In case it's not already obvious painfully, I'm new to this - any suggestions would be much appreciated before I go nuts.
May 1 '07 #1
31 2641
Rabbit
12,516 Expert Mod 8TB
How do you calculate the weekly average income?
May 1 '07 #2
I'd subtract date 2 from date 1, convert to weeks and divide into the income. So in the example -

Income_Date Income.
19/3/07 $320
21/2/07 $200
27/1/07 $300

19/3/07 - 21/2/07 = 27 days, say 3.8 weeks. Divide $320 by 3.8 for average weekly income. I could do it on a spreadsheet!
May 1 '07 #3
Rabbit
12,516 Expert Mod 8TB
Difficult, you'll need either a subquery or a DMax to pull the last date.
DMax is easier to implement but less efficient.
May 1 '07 #4
Difficult, you'll need either a subquery or a DMax to pull the last date.
DMax is easier to implement but less efficient.
Would you be able to be a bit more specific please, I'm new to this. I thought I'd need to use datediff, but can't figure that out either. I don't know what code to use or how to use it ...
May 1 '07 #5
Rabbit
12,516 Expert Mod 8TB
Something along the lines of
Expand|Select|Wrap|Line Numbers
  1. SELECT DateDiff("d", [Income_Date], DMax("Income_Date", "TableName", "Income_Date<" & [Income_Date])) As Days
  2. FROM TableName;
  3.  
May 1 '07 #6
Thanks heaps Rabbit for your time, I appreciate it. I've put that in as the control source for an unbound field in the subform, and get the following error message:
"the syntax of the subquery is incorrect. Check the subquery's status and enclose the subquery in parenthesis". Do you have any other clues?
Cheers
May 2 '07 #7
Rabbit
12,516 Expert Mod 8TB
Thanks heaps Rabbit for your time, I appreciate it. I've put that in as the control source for an unbound field in the subform, and get the following error message:
"the syntax of the subquery is incorrect. Check the subquery's status and enclose the subquery in parenthesis". Do you have any other clues?
Cheers
I was under the impression you were looking for a query.

You can't use a SQL statement as the control source of a control. You can use them for the row sources and record source of combo/list boxes and forms respectively.

If you're only looking to calculate the value for the one record you're viewing then you only need the formula and can forgo the SQL statement.

In the end, the tables are poorly designed. Ideally you would want each record to have both a start date and an end date.
May 2 '07 #8
I was under the impression you were looking for a query.

You can't use a SQL statement as the control source of a control. You can use them for the row sources and record source of combo/list boxes and forms respectively.

If you're only looking to calculate the value for the one record you're viewing then you only need the formula and can forgo the SQL statement.

In the end, the tables are poorly designed. Ideally you would want each record to have both a start date and an end date.
When the operator is entering the latest collection date, there is no way of knowing the last collection date, aka the start date, without going back and looking it up manually.
The value to be calculated would be on each row of a continuous subform, to give a continuous history of income at a glance.
Thanks again for your input, I'll give it another go.
May 2 '07 #9
Hi again,

I've put in the following code as the control source

=DateDiff("d",[Collection_Date],DMax("Collection_Date","M_Income","Collection_Dat e<" & [Collection_Date]))

and it's returning a blank field.
There is an #Error on the line for a new record, which I suppose is to be expected.
May 2 '07 #10
I was under the impression you were looking for a query.

You can't use a SQL statement as the control source of a control. You can use them for the row sources and record source of combo/list boxes and forms respectively.

If you're only looking to calculate the value for the one record you're viewing then you only need the formula and can forgo the SQL statement.

In the end, the tables are poorly designed. Ideally you would want each record to have both a start date and an end date.
I've tried putting the SQL statement in as a query, and I get the same results, a series of blank fields.
May 2 '07 #11
Rabbit
12,516 Expert Mod 8TB
In the query did you give it an alias?
May 3 '07 #12
In the query did you give it an alias?
No, I didn't. What is that and how do I do it?
May 3 '07 #13
Rabbit
12,516 Expert Mod 8TB
In design view it would be:
Alias: Formula

In SQL it would be:
Formula As Alias

The Alias is to give the field a name. Functions can't be the name of the field.
May 3 '07 #14
In design view it would be:
Alias: Formula

In SQL it would be:
Formula As Alias

The Alias is to give the field a name. Functions can't be the name of the field.
I'm sorry to be so thick, I have no idea what I'm doing here. Could you please be utterly specific, what do I type in, and where? I have the subform, I've put the formula in as the control source for a text box. I don't know what you mean to do with the alias from there.
May 3 '07 #15
Rabbit
12,516 Expert Mod 8TB
I'm sorry to be so thick, I have no idea what I'm doing here. Could you please be utterly specific, what do I type in, and where? I have the subform, I've put the formula in as the control source for a text box. I don't know what you mean to do with the alias from there.
Control source? You do realize we've been talking about a query this whole time right? Go up a few posts.
May 3 '07 #16
Rabbit
12,516 Expert Mod 8TB
After some testing, I think I've figured out why, need to delimit the value as a date using #.

[code]
DateDiff("d", [Income_Date], DMax("Income_Date", "TableName", "Income_Date<#" & [Income_Date] & "#"))
May 3 '07 #17
After some testing, I think I've figured out why, need to delimit the value as a date using #.

[code]
DateDiff("d", [Income_Date], DMax("Income_Date", "TableName", "Income_Date<#" & [Income_Date] & "#"))
Thanks Rabbit, I think that's getting closer ... I get a result now, just not the right one, eg:

Line 1 3.4.07 -32
Line 2 9.2.07 54
Line 3 4.1.07 85

Or

Line 1 30.3.07 -2
Line 2 23.3.07 -4
Line 3 16.3.07 -4
Line 4 2.3.07 -28
Line 5 22.2.07 -2
Line 6 16.2.07 -6
Line 7 9.2.07 54

I thought it would be easy to figure out what the results mean and adjust the formula accordingly, but I can't make any sense of the results.

In the first example, if I change the date from 3.4.07 to 9.2.07 it changes from -32 to 54, when the result should be 0.
May 4 '07 #18
Rabbit
12,516 Expert Mod 8TB
Thanks Rabbit, I think that's getting closer ... I get a result now, just not the right one, eg:

Line 1 3.4.07 -32
Line 2 9.2.07 54
Line 3 4.1.07 85

Or

Line 1 30.3.07 -2
Line 2 23.3.07 -4
Line 3 16.3.07 -4
Line 4 2.3.07 -28
Line 5 22.2.07 -2
Line 6 16.2.07 -6
Line 7 9.2.07 54

I thought it would be easy to figure out what the results mean and adjust the formula accordingly, but I can't make any sense of the results.

In the first example, if I change the date from 3.4.07 to 9.2.07 it changes from -32 to 54, when the result should be 0.
Try flipping the second argument of DateDiff with the third argument.

Basically what it's doing is calculating the difference in days between the date of the current record with the date of the next highest date. It does not necessarily use the next record. If there are going to be records with the same dates then that adds a whole other layer of complexity.
May 4 '07 #19
Try flipping the second argument of DateDiff with the third argument.

Basically what it's doing is calculating the difference in days between the date of the current record with the date of the next highest date. It does not necessarily use the next record. If there are going to be records with the same dates then that adds a whole other layer of complexity.
Did you mean swap "TableName", and "Income_Date<#" & [Income_Date] & "#"))?
That gives a jet error.
May 4 '07 #20
Rabbit
12,516 Expert Mod 8TB
Did you mean swap "TableName", and "Income_Date<#" & [Income_Date] & "#"))?
That gives a jet error.
I said DateDiff, not DMax.
May 4 '07 #21
I said DateDiff, not DMax.
Expand|Select|Wrap|Line Numbers
  1. =DateDiff("d",DMax("M_Income","Income_Date","M_Income","Income_Date<#" & [Income_Date] & "#"),[Income_Date]) 
Sorry, told you I was a bit thick. Is this what you meant - it returns #Error
May 4 '07 #22
Rabbit
12,516 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. =DateDiff("d",DMax("M_Income","Income_Date","M_Income","Income_Date<#" & [Income_Date] & "#"),[Income_Date]) 
Sorry, told you I was a bit thick. Is this what you meant - it returns #Error
You have an extra parameter in your DMax, specifically the first one shouldn't be there.
May 4 '07 #23
You have an extra parameter in your DMax, specifically the first one shouldn't be there.
Oops, must have done copy and past instead of cut and paste - too early in the morning.

That gives a result now, it's still wrong though. Its the same as the previous results posted, but the positives and negatives are reversed.

Basically what it's doing is calculating the difference in days between the date of the current record with the date of the next highest date. It does not necessarily use the next record. If there are going to be records with the same dates then that adds a whole other layer of complexity..
I would have thought there should be no circumstance where there are negative results, no matter which date it's using.
I've added and subtracted and can't find a way to get these same results.
May 5 '07 #24
Rabbit
12,516 Expert Mod 8TB
The only thing I can think of is that your Date field is a text field rather than a date field because I tested it and it works.
May 5 '07 #25
The only thing I can think of is that your Date field is a text field rather than a date field because I tested it and it works.
No, the date field is definitely set as a date field.
May 5 '07 #26
No, the date field is definitely set as a date field.
Hi Rabbit, I've had a play, set up a new database table with some data, and entered your formula. What it does is give me the number of days between the current record, and the first record in the table, not the very next record in the table, eg
1.4.07 90
1.3.07 59
1.2.07 31
1.1.07

What I need is the number of days from the immediately previous record, for that particular site (this control is on a subform in the Sites table, so only the records for that particular site display).

So I guess something unexpected is happening in my live data, because is has multiple sites and multiple dates?
May 6 '07 #27
Rabbit
12,516 Expert Mod 8TB
Hi Rabbit, I've had a play, set up a new database table with some data, and entered your formula. What it does is give me the number of days between the current record, and the first record in the table, not the very next record in the table, eg
1.4.07 90
1.3.07 59
1.2.07 31
1.1.07

What I need is the number of days from the immediately previous record, for that particular site (this control is on a subform in the Sites table, so only the records for that particular site display).

So I guess something unexpected is happening in my live data, because is has multiple sites and multiple dates?
It's possible, I tested it with just a table with a date field and it worked fine. It takes the date of the record it's currently on, gets the next highest date after the current record and finds the difference in days. Like I said before, it's not necessarily going to get the next record because it skips all records that are the same date.

Also, the fact that you need it by site brings up more complications which you should have stated at the beginning, it changes things.
May 6 '07 #28
It's possible, I tested it with just a table with a date field and it worked fine. It takes the date of the record it's currently on, gets the next highest date after the current record and finds the difference in days. Like I said before, it's not necessarily going to get the next record because it skips all records that are the same date.

Also, the fact that you need it by site brings up more complications which you should have stated at the beginning, it changes things.
Sorry, I thought I did mention that when I said I was using it in a subform.

Any further suggestions on where to go from here?
May 6 '07 #29
Rabbit
12,516 Expert Mod 8TB
Sorry, I thought I did mention that when I said I was using it in a subform.

Any further suggestions on where to go from here?
The subform doesn't have anything to do with this. I'm talking about site. You're going to have to account for site in the third parameter of the DMax function so that you're only returning dates for the same site.
May 6 '07 #30
The subform doesn't have anything to do with this. I'm talking about site. You're going to have to account for site in the third parameter of the DMax function so that you're only returning dates for the same site.
Ok, I've got this far, but still giving me weird results. Can you see where I'm going wrong with this?

The income and dates are in a table called (M_Income), which is displayed in a continuous subform on a form named (F_Sites) which is updating the table M_Sites. The link field is Site_ID between the M_Sites and M_Income tables.

=IIf(Len([Collection_Date] & "")>0,DateDiff("d",DMax("Collection_Date","M_Incom e","Collection_Date< #" & [Collection_Date] & "# And Site_ID=" & [Site_ID]),[Collection_Date]),Null)

The IIf is so it doesn't give an error on the new record line.
May 6 '07 #31
Rabbit
12,516 Expert Mod 8TB
Ok, I've got this far, but still giving me weird results. Can you see where I'm going wrong with this?

The income and dates are in a table called (M_Income), which is displayed in a continuous subform on a form named (F_Sites) which is updating the table M_Sites. The link field is Site_ID between the M_Sites and M_Income tables.

=IIf(Len([Collection_Date] & "")>0,DateDiff("d",DMax("Collection_Date","M_Incom e","Collection_Date< #" & [Collection_Date] & "# And Site_ID=" & [Site_ID]),[Collection_Date]),Null)

The IIf is so it doesn't give an error on the new record line.
I don't know what that & "" is for, that probably doesn't have to be there. The rest looks OK as long as the Site_ID is of a numeric data type.

But the problem is that you can't use this on a continuous subform. You have to do it through a query and then you can show it in a subform by including the field. The problem is that unbound controls always display the same thing on a continuous subform. They don't have seperate values depending on what record they belong to.
May 7 '07 #32

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...
3
by: Lumpierbritches | last post by:
Can this be done in access by a lay person? Also, are there any tutorials for this type of addition to your applications? I'm trying to use a calendar control to set a date in the future for a to...
7
by: Jimbo | last post by:
Anyone know how I can subtract 12 months from the current date?
8
by: Remington | last post by:
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...
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...
3
Ericks
by: Ericks | last post by:
I have several subforms (datasheet mode), each with a date. The default dates are that of the system but they can be altered at will by the user. I want these dates to remain sorted descending, that...
4
by: jnice814 | last post by:
I have created a frmTimesheet form where auditors will enter their hours for audits that they've worked on. I know how to build a very basic, no-frills database, and what I'm trying to accomplish...
6
by: lptl | last post by:
I know the title is misleading. I am working on a class project where we are trying to setup a notification system written in PHP and using an MySQL database. We are trying to set-up a notification...
2
by: barronmo | last post by:
I'm trying to get the difference in dates using the time module rather than datetime because I need to use strptime() to convert a date and then find out how many weeks and days until that date. ...
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: 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
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
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
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
isladogs
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...

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.