473,387 Members | 1,904 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.

Day Difference in Table

Hi,

I am in need of some help. What i am trying to do is to make a field in the table automatically generate data from a calculation. Below is how the table looks like:

- Date Due = this is a date
- Date Returned = the is a date
- Fine = this is where i need to put the calculation...

The calculation that im trying to do is:

The "Fine" field must always have £3 to begin with (default). It should add £0.25 for every day the game has been overdue. To do this, the field would need to calculate the Day difference between the "Date Returned" and "Date Due" fields. For example, if the day difference is 2 days between those 2 fields then in the "Fine" field, £0.50 should automatically be added.

I would love all the help i can get. Please remember that this is in the table.

Thanks,
Feb 18 '08 #1
9 2188
sierra7
446 Expert 256MB
Hi

If you are working on a bound form then in the After_Update event of the ReturnDate you may need something like this.

Expand|Select|Wrap|Line Numbers
  1.  Dim lngPeriod as long 
  2.  
  3. 'Calculate difference between two dates in days
  4. 'will return positive if first date is before second date
  5. lngPeriod = DateDiff("d", [Date Due], [Date Returned])
  6. If lngPeriod < 0 Then 
  7.    'Negative, so Late, so calculate Fine
  8.    Me!Fine = 3 -(0.25 * lngPeriod)
  9. Else
  10.    'Not late
  11.    Me!Fine = 0
  12. End If
  13.  
S7
Feb 18 '08 #2
MikeTheBike
639 Expert 512MB
Hi,

I am in need of some help. What i am trying to do is to make a field in the table automatically generate data from a calculation. Below is how the table looks like:

- Date Due = this is a date
- Date Returned = the is a date
- Fine = this is where i need to put the calculation...

The calculation that im trying to do is:

The "Fine" field must always have £3 to begin with (default). It should add £0.25 for every day the game has been overdue. To do this, the field would need to calculate the Day difference between the "Date Returned" and "Date Due" fields. For example, if the day difference is 2 days between those 2 fields then in the "Fine" field, £0.50 should automatically be added.

I would love all the help i can get. Please remember that this is in the table.

Thanks,
Hi
You need to enter something like this in the query designer Field

Fine:IIF([Date Returned]>[Date Due],([Date Returned]-[Date Due])*0.25+3,0)

This assumes that both dates are dates and not date/time fields.

This has the fixed fine and day fine elements hard coded.
This is fine if these NEVER change, but if they do you may want to think about what you want to happen if/when they do !?

HTH


MTB
Feb 18 '08 #3
missinglinq
3,532 Expert 2GB
S7, I think from reading the original post that you got these two lines backward:

Expand|Select|Wrap|Line Numbers
  1. If lngPeriod < 0 Then
If the differemce, the game was returned early

Expand|Select|Wrap|Line Numbers
  1. fine = 3 - (0.25 * lngPeriod)
The 0.25 needs to be Added to the 3, not subtracted

After testing, it appears to need to be:
Expand|Select|Wrap|Line Numbers
  1.  Dim lngPeriod as long
  2.  
  3. 'Calculate difference between two dates in days
  4. lngPeriod = DateDiff("d", [Date Due], [Date Returned])
  5. If lngPeriod > 0 Then
  6. 'Positive, so Late, so calculate Fine
  7. Me!Fine = 3 + (0.25 * lngPeriod)
  8. Else
  9. 'Not late
  10. Me!Fine = 0
  11. End If
  12.  
There's something I think needs clarification, Captain! Does the 3 pounds always need to be in this field, even if the game is returned on time, or only if the game is returned late, with the daily fine added?

As you might have guessed from these answers, you cannot do this from a table, nor should you! Tables should never be accessed directly, but always thru forms/queries.

Welcome to TheScripts!

Linq ;0)>
Feb 18 '08 #4
sierra7
446 Expert 256MB
Hi Linq
What I got wrong was the dates the wrong way round and persuaded myself that if the deal was late then lngPeriod would be negative!
I knew it did not look 'intuitive' .
I must go to bed earlier!
S7
Feb 20 '08 #5
missinglinq
3,532 Expert 2GB
You're a programmer, and you actually sleep?

Linq ;0)>
Feb 20 '08 #6
S7, I think from reading the original post that you got these two lines backward:

Expand|Select|Wrap|Line Numbers
  1. If lngPeriod < 0 Then
If the differemce, the game was returned early

Expand|Select|Wrap|Line Numbers
  1. fine = 3 - (0.25 * lngPeriod)
The 0.25 needs to be Added to the 3, not subtracted

After testing, it appears to need to be:
Expand|Select|Wrap|Line Numbers
  1.  Dim lngPeriod as long
  2.  
  3. 'Calculate difference between two dates in days
  4. lngPeriod = DateDiff("d", [Date Due], [Date Returned])
  5. If lngPeriod > 0 Then
  6. 'Positive, so Late, so calculate Fine
  7. Me!Fine = 3 + (0.25 * lngPeriod)
  8. Else
  9. 'Not late
  10. Me!Fine = 0
  11. End If
  12.  
There's something I think needs clarification, Captain! Does the 3 pounds always need to be in this field, even if the game is returned on time, or only if the game is returned late, with the daily fine added?

As you might have guessed from these answers, you cannot do this from a table, nor should you! Tables should never be accessed directly, but always thru forms/queries.

Welcome to TheScripts!

Linq ;0)>
Yes, It would need to always be £3 even if it is returned on time. I am currently trying to get this to work but im having a few problems since nothing seems to be happening...

What i did was make a form for the loans table, and added in the vb code you posted / fixed to the "Fine" field in the form. Please let me know if i have done anything wrong.

Thanks, and I appreciate all of the help Im getting from everyone.
Feb 20 '08 #7
Scott Price
1,384 Expert 1GB
One very important point to make is that calculated fields do not belong in tables!

You store data in tables, not calculations. Queries are for calculations.

You will be able to store historic data derived from a calculation in a table, but it is not good practice to store current fine data in the table for retrieval later. The reason for this: Say today you begin the overdue fine period and require your database to calculate and store today's fine. Tomorrow you need to do the same. Next day the same... etc etc. This is a very resource intensive way of structuring the fine system.

A better way: test for the overdue period and tell the database to turn the overdue item red, or popup a form letting you know of the situation. Then when the person returns the item, do your calculation in a query that returns once, and once only, the amount of the fine. You can do what you like with this fine amount then, store it in a table, etc...

Getting the idea?

Regards,
Scott
Feb 20 '08 #8
missinglinq
3,532 Expert 2GB
Say today you begin the overdue fine period and require your database to calculate and store today's fine. Tomorrow you need to do the same. Next day the same... etc etc. This is a very resource intensive way of structuring the fine system.
I'm confused by this, Scott. Nothing has been said about calculating the overdo fine each day; the fine is only calculated when the game is returned and the Date Returned is entered.

This code should do the job. The code goes in the AfterUpdate event for the Date Returned field. It's been adjusted so that the 3 pounds will appear even if the book is returned on time/early.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Date_Returned_AfterUpdate()
  2. Dim lngPeriod As Long
  3.  
  4. 'Calculate difference between two dates in days
  5. lngPeriod = DateDiff("d", Me.Due_Date, Me.Date_Returned)
  6.   If lngPeriod > 0 Then
  7.    'Positive, so Late, so calculate Fine
  8.    Me.Fine = 3 + (0.25 * lngPeriod)
  9.   Else
  10.    'Not late
  11.    Me.Fine = 3
  12.   End If
  13. End Sub
Scott's advice about storing the calculated amount is spot on; it shouldn't be stored but rther calculated whenever needed.

Linq ;0)>
Feb 20 '08 #9
sierra7
446 Expert 256MB
Yes, It would need to always be £3 even if it is returned on time. I am currently trying to get this to work but im having a few problems since nothing seems to be happening...

What i did was make a form for the loans table, and added in the vb code you posted / fixed to the "Fine" field in the form. Please let me know if i have done anything wrong.

Thanks, and I appreciate all of the help Im getting from everyone.
Ahoy Capt'n !
This is starting to make a nonesense of things.
You shouldn't call things by names they are not.

A Fine is what you get when after an infringement (like when you go past those little yellow cameras too fast in the UK) Linq just called me a Programmer and look how misleading that was!

It sound like you should have two fields on you database, one called StandardCharge or whatever and the other called Fine. As MikeTheBike has already said having values hard coded into your program is not a good idea. Right now you seem to be charging a flat-rate £3 for hiring a game. If you want to charge £3.25 next month or £4 for a different game or gendre, then your program won't work.

You could build thes costs into a separate look-up table and select the appropriate cost as you were issuing the game.

As Scott has said it is pointless calculating the 'Fine' and storing it in the database until the game is returned. If you wanted an on-screen display of the current fine you could include an unbound control and set its Control Source property to;-
Expand|Select|Wrap|Line Numbers
  1.  =IIf(Date>[Date Due],(Date-[Date Due])*0.25,0)
  2.  
'Date' is a function that returns Today's date so doesn't need square brackets; [Date Due] is a field name so should have square brackets, but do yourself a favour and get rid of spaces in field names.

The above is similar to MikeTheBikes advice. If you see odd values (none multiples of 0.25) it will mean that [Due Date] may have a Time element to it. (dates are measured in whole days so 0.5 is 12.00 noon, 0.001 would be a 1/1000 of a day (1.44 minutes) after midnight) To avoid this you would have to use the DateDiff() function.

S7
Feb 20 '08 #10

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

Similar topics

0
by: Luc Martineau | last post by:
Hello When we create a table, what is the difference between the REFERENCE constraint on column and the FOREIGN KEY constraint on table ? I guess that the FOREIGN KEY constraint assures us...
5
by: Neil Rutherford | last post by:
During testing of an application, i noticed a difference between SQL 2000 and SQL 7, both with identical config. In a nutshell: A table has a trigger for UPDATE and DELETE. When a column in the...
8
by: Paulo Jan | last post by:
Hi all: I have here a table with the following schema: Table "todocinetv" Column | Type | Modifiers...
5
by: aj | last post by:
DB2 WSE 8.1 FP5 Red Hat AS 2.1 What is the difference between adding a unique constraint like: ALTER TABLE <SCHEMA>.<TABLE> ADD CONSTRAINT CC1131378283225 UNIQUE ( <COL1>) ; and adding a...
1
by: Beffmans | last post by:
Hi I work with .net 1.1 and i wanted to know the difference between a html table and a <asp:table>? Which one is better for a consistent layout of your asp.net pages? ch Beffmans
4
by: dmisen | last post by:
I have data on air pollution coming from sources across the country, and control options for reducing pollution from each source. The example below shows data for: - 2 sources (plant_ID "001" and...
9
by: HC | last post by:
Hello, all, I started out thinking my problems were elsewhere but as I have worked through this I have isolated my problem, currently, as a difference between MSDE and SQL Express 2005 (I'll just...
3
by: bbawa1 | last post by:
Hi, I have a table which has a field ItemsReceived of type datetime. I have a grid view which has two columns. In first column i have to show the data from field ItemsReceived and in second...
3
by: cheguashwini | last post by:
Hi I have a problem. I have two tables...table1 and table2. table1 has the column latitude and sno(serial number) and table2 has the column lat and sno(serial number) . i have to find the...
3
by: Steve | last post by:
I am trying to calculate elapsed travel times for flights. My plan is to enter the local departure time, the departure city and the local arrival time and city. These times would be standardised...
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
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
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,...

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.