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

Date and Time Expression Help with Access

Hi Everyone-

I need to figure out how link up a date and time in one database to a specific value in another database. For example:

I have a radio station ABCD that aired an ad on Monday, Feb. 5th at 2:04pm. We pay out specific rates depending on the time of the airing. The rates for each station will vary depending on the station/day and time. In one database I have the airings that are set up like this: (example of headers from the database)

Station: Date: Time of Airing:


Then I have another database that houses all the rates for each station/day time: (these are the headers for the database). This database has a rate associated to each station/day/time.

Station: Mon-Fri 6am-10am: Mon-Fri 10am-3pm: Mon-Fri 3pm-7pm: Mon-Fri 7pm-12am: Sat 6am-7pm: Sun 6am-7pm:


Based on the time and date of the airing, I would like my querry to pull back the specific rates. How can I do this?

So based on the example information from above, station ABCD aired an ad on a Monday at 2:04pm. Therefore I would need the Mon-Fri 10am-3pm rate to be brought back in a querry. Is this possible?
Feb 5 '07 #1
14 3089
Rabbit
12,516 Expert Mod 8TB
Hi Everyone-

I need to figure out how link up a date and time in one database to a specific value in another database. For example:

I have a radio station ABCD that aired an ad on Monday, Feb. 5th at 2:04pm. We pay out specific rates depending on the time of the airing. The rates for each station will vary depending on the station/day and time. In one database I have the airings that are set up like this: (example of headers from the database)

Station: Date: Time of Airing:


Then I have another database that houses all the rates for each station/day time: (these are the headers for the database). This database has a rate associated to each station/day/time.

Station: Mon-Fri 6am-10am: Mon-Fri 10am-3pm: Mon-Fri 3pm-7pm: Mon-Fri 7pm-12am: Sat 6am-7pm: Sun 6am-7pm:


Based on the time and date of the airing, I would like my querry to pull back the specific rates. How can I do this?

So based on the example information from above, station ABCD aired an ad on a Monday at 2:04pm. Therefore I would need the Mon-Fri 10am-3pm rate to be brought back in a querry. Is this possible?
This is entirely possible.

In the Airings table, you'll need to store the day and time in different fields.
In the Rates table, you'll need to store day, time start, time end in different fields. Be sure this is mutually exclusive, i.e. 3:01 - 4:00, 4:01 - 5:00. Not 3:00 - 4:00, 4:00 - 5:00.

Then, in a query, you link the tables based on day, and include all records from the Airings table where the time is between time start and time end.

The SQL will look like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT tbl_Airing.Station, tbl_Airing.Day, tbl_Airing.Time, tbl_Rates.Rate
  2. FROM tbl_Airing LEFT JOIN tbl_Rates ON tbl_Airing.Day = tbl_Rates.Day
  3. WHERE (((tbl_Airing.Time) Between [tbl_Rates].[Time Start] And [tbl_Rates].[Time End]));
  4.  
[code]
Feb 5 '07 #2
This is entirely possible.

In the Airings table, you'll need to store the day and time in different fields.
In the Rates table, you'll need to store day, time start, time end in different fields. Be sure this is mutually exclusive, i.e. 3:01 - 4:00, 4:01 - 5:00. Not 3:00 - 4:00, 4:00 - 5:00.

Then, in a query, you link the tables based on day, and include all records from the Airings table where the time is between time start and time end.

The SQL will look like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT tbl_Airing.Station, tbl_Airing.Day, tbl_Airing.Time, tbl_Rates.Rate
  2. FROM tbl_Airing LEFT JOIN tbl_Rates ON tbl_Airing.Day = tbl_Rates.Day
  3. WHERE (((tbl_Airing.Time) Between [tbl_Rates].[Time Start] And [tbl_Rates].[Time End]));
  4.  
[code]

Thanks Rabbit! the only problem is that the Rates database is already set up with thousands and thousands of rows of data. It is set up with the headers:

Station: Mon-Fri 6am-10am: Mon-Fri 10am-3pm: Mon-Fri 3pm-7pm: Mon-Fri 7pm-12am: Sat 6am-7pm: Sun 6am-7pm:

The Rates are already tied to the Mon-Fri 6am-10am etc. So how would could I make the time start and end in separate columns without losing the rate information?

The current database for Rates looks like this:

Station MF6am-10am MF10am-3pm etc..
ABCD 100.00 80.00 etc
Feb 5 '07 #3
Rabbit
12,516 Expert Mod 8TB
Okay. Here Goes:

Expand|Select|Wrap|Line Numbers
  1. SELECT tbl_Airing.Station, tbl_Airing.DoA, tbl_Airing.ToA, IIf(Weekday([DoA])=1,DLookUp("U6am7pm","tbl_Rates","Station = '" & [Station] & "'"),IIf(Weekday([DoA])=7,DLookUp("S6am7pm","tbl_Rates","Station = '" & [Station] & "'"),IIf([ToA]>=#12/30/1899 6:0:0# And [ToA]<#12/30/1899 10:0:0#,DLookUp("MF6am10am","tbl_Rates","Station = '" & [Station] & "'"),IIf([ToA]>=#12/30/1899 10:0:0# And [ToA]<#12/30/1899 15:0:0#,DLookUp("MF10am3pm","tbl_Rates","Station = '" & [Station] & "'"),IIf([ToA]>=#12/30/1899 15:0:0# And [ToA]<#12/30/1899 19:0:0#,DLookUp("MF3pm7pm","tbl_Rates","Station = '" & [Station] & "'"),IIf([ToA]>=#12/30/1899 19:0:0# And [ToA]<=#12/30/1899 23:59:0#,DLookUp("MF7pm12am","tbl_Rates","Station = '" & [Station] & "'"),"No Rates")))))) AS Rate
  2. FROM tbl_Airing;
  3.  
This is the SQL for the query.

You will have to name the fields: Station, MF6am10am, MF10am3pm, MF3pm7pm,MF7pm12am, S6am7pm, U6am7pm.

This will work if your Airings table has the date and time as seperate fields. ToA for Time of Airing and DoA for Date of Airing in my SQL.

This query will return "No Rates" only for Times of Airing before 6 AM or after Midnight on Mondays through Fridays. I did not include such a check for Saturday and Sunday because I did not think of it at the time.

I used 11:59 PM rather than 12 AM because I didn't want to deal with what happens when the date changes at midnight.

If either of these are of importance, let me know.
Feb 5 '07 #4
Okay. Here Goes:

Expand|Select|Wrap|Line Numbers
  1. SELECT tbl_Airing.Station, tbl_Airing.DoA, tbl_Airing.ToA, IIf(Weekday([DoA])=1,DLookUp("U6am7pm","tbl_Rates","Station = '" & [Station] & "'"),IIf(Weekday([DoA])=7,DLookUp("S6am7pm","tbl_Rates","Station = '" & [Station] & "'"),IIf([ToA]>=#12/30/1899 6:0:0# And [ToA]<#12/30/1899 10:0:0#,DLookUp("MF6am10am","tbl_Rates","Station = '" & [Station] & "'"),IIf([ToA]>=#12/30/1899 10:0:0# And [ToA]<#12/30/1899 15:0:0#,DLookUp("MF10am3pm","tbl_Rates","Station = '" & [Station] & "'"),IIf([ToA]>=#12/30/1899 15:0:0# And [ToA]<#12/30/1899 19:0:0#,DLookUp("MF3pm7pm","tbl_Rates","Station = '" & [Station] & "'"),IIf([ToA]>=#12/30/1899 19:0:0# And [ToA]<=#12/30/1899 23:59:0#,DLookUp("MF7pm12am","tbl_Rates","Station = '" & [Station] & "'"),"No Rates")))))) AS Rate
  2. FROM tbl_Airing;
  3.  
This is the SQL for the query.

You will have to name the fields: Station, MF6am10am, MF10am3pm, MF3pm7pm,MF7pm12am, S6am7pm, U6am7pm.

This will work if your Airings table has the date and time as seperate fields. ToA for Time of Airing and DoA for Date of Airing in my SQL.

This query will return "No Rates" only for Times of Airing before 6 AM or after Midnight on Mondays through Fridays. I did not include such a check for Saturday and Sunday because I did not think of it at the time.

I used 11:59 PM rather than 12 AM because I didn't want to deal with what happens when the date changes at midnight.

If either of these are of importance, let me know.
Perfect! So I just change the name of the fields and copy SQL into the "criteria" field in my querry? Thank you! Thank you!
Feb 5 '07 #5
Rabbit
12,516 Expert Mod 8TB
Not the criteria field.

There are 2 ways to design a query: Design View and SQL View.

In Design View, if you right-click the title bar, you can select SQL View.

You want to replace what's in there with what I typed.

Here's the problem, in my SQL, the tables are named tbl_Rates and tbl_Airing.
If it won't cause any problems, you can just rename your tables.
If they are linked to other forms, reports, queries, etc. then paste it into Word and use search and replace to change the names of the tables.
Feb 5 '07 #6
Not the criteria field.

There are 2 ways to design a query: Design View and SQL View.

In Design View, if you right-click the title bar, you can select SQL View.

You want to replace what's in there with what I typed.

Here's the problem, in my SQL, the tables are named tbl_Rates and tbl_Airing.
If it won't cause any problems, you can just rename your tables.
If they are linked to other forms, reports, queries, etc. then paste it into Word and use search and replace to change the names of the tables.

YOU ARE A GENIUS! THANK YOU SO MUCH! IT WORKED LIKE A CHARM!
Feb 5 '07 #7
Rabbit
12,516 Expert Mod 8TB
YOU ARE A GENIUS! THANK YOU SO MUCH! IT WORKED LIKE A CHARM!
Not a problem at all! If only everyone was so enthusiastic about getting their answers lol.
Feb 5 '07 #8
Rabbit
12,516 Expert Mod 8TB
Some additional notes of caution on what the query actually returns:

1) It ignores the time at which the airing occurs on Saturday and Sunday.

2) The time cutoffs for Monday through Friday are 6:00 AM - 9:59 AM, 10:00 AM - 2:59 PM, 3:00 PM - 6:59 PM, 7:00 PM - 11:59 PM.

3) The time fields must assume a date of 12/20/1899. If it works now, as long as you don't change anything, the query will continue to work. Just make sure you don't enter any Date information into the Time field.

How the calculated field works:

1) Checks to see if it's a Sunday, if it is, looks up the rate for U6am7pm.

2) If not, checks to see if it's Saturday, if it is, looks up the rate for S6am7pm.

3) If not, checks to see if ToA is between 6:00 AM and 9:59 AM, if it is, looks up rate for MF6am10am.

4) If not, checks to see if ToA is between 10:00 AM and 2:59 PM, if it is, looks up rate for MF10am3pm.

5) If not, checks to see if ToA is between 3:00 PM and 6:59 PM, if it is, looks up rate for MF3pm7pm.

6) If not, checks to see if ToA is between 7:00 PM and 11:59 PM, if it is, looks up rate for MF7pm12am.

7) If not, returns "No Rates".

So basically it's iif() embedded in iif() embedded in iif() and so on.
Feb 5 '07 #9
Some additional notes of caution on what the query actually returns:

1) It ignores the time at which the airing occurs on Saturday and Sunday.

2) The time cutoffs for Monday through Friday are 6:00 AM - 9:59 AM, 10:00 AM - 2:59 PM, 3:00 PM - 6:59 PM, 7:00 PM - 11:59 PM.

3) The time fields must assume a date of 12/20/1899. If it works now, as long as you don't change anything, the query will continue to work. Just make sure you don't enter any Date information into the Time field.

How the calculated field works:

1) Checks to see if it's a Sunday, if it is, looks up the rate for U6am7pm.

2) If not, checks to see if it's Saturday, if it is, looks up the rate for S6am7pm.

3) If not, checks to see if ToA is between 6:00 AM and 9:59 AM, if it is, looks up rate for MF6am10am.

4) If not, checks to see if ToA is between 10:00 AM and 2:59 PM, if it is, looks up rate for MF10am3pm.

5) If not, checks to see if ToA is between 3:00 PM and 6:59 PM, if it is, looks up rate for MF3pm7pm.

6) If not, checks to see if ToA is between 7:00 PM and 11:59 PM, if it is, looks up rate for MF7pm12am.

7) If not, returns "No Rates".

So basically it's iif() embedded in iif() embedded in iif() and so on.

Thanks for explaining that calculation! That helps alot. The only problem I'm having now is that there are several rates showing up for each day (which is correct), is there any way that I can add into the formula something that would just give me the total rate for the entire day's worth of airings?
Feb 7 '07 #10
Rabbit
12,516 Expert Mod 8TB
First I need to know how you calculate the total rate for the entire day. Are you talking total average rate? And what's being included and excluded from these calculations? Total rate for all stations? Or for each station? All of these can be done with a different query that is based off this query to calculate sums. But how you build it will depend on what you want to calculate.
Feb 7 '07 #11
First I need to know how you calculate the total rate for the entire day. Are you talking total average rate? And what's being included and excluded from these calculations? Total rate for all stations? Or for each station? All of these can be done with a different query that is based off this query to calculate sums. But how you build it will depend on what you want to calculate.
Sorry, I should have been more specific. It is the Total Rate(sum) for each station on a daily basis. You have been so much help. Thank you!
Feb 8 '07 #12
Rabbit
12,516 Expert Mod 8TB
You're going to have to create another query that is based on the previous one. Just group by Station, Date of Airing, and then Sum of Rate.

If you need the SQL, let me know.
Feb 8 '07 #13
You're going to have to create another query that is based on the previous one. Just group by Station, Date of Airing, and then Sum of Rate.

If you need the SQL, let me know.

Thanks again!!! You are the best. Have a great weekend.
Feb 9 '07 #14
Rabbit
12,516 Expert Mod 8TB
You're welcome, you have a nice weekend too.
Feb 9 '07 #15

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

Similar topics

2
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
7
by: vnl | last post by:
I'm trying to run a SQL query but can't find any records when trying to select a certain date. Here's the sql: SELECT field 1, field2, date_and_time, FROM table1 WHERE date_and_time =...
4
by: William Bradley | last post by:
I have two cells on a form. One of them is the "Production Date" and the other is the "Expiry Date". The "Expiry Date" is 183 days after the "Production Date." On an Excel spreadsheet, the...
17
by: Lapchien | last post by:
My table has a record called date/time - filled (not surprisingly) with a date and time. I can format the date in one query field, also the time, using format in an expression. My user wants me...
3
by: Taji | last post by:
Can someone please explain this to me. When I run the following query in MS Access, it doesn't return anything. SELECT tbl_vdc.vcd_id, tbl_vdc.vcd_count, tbl_vdc.batch_day FROM tbl_vdc WHERE...
3
by: Reney | last post by:
I am using Access in my project. In one of the forms, I am calling two tables, and two of the columns have date/time type, namely "ClockIn" and "ClockOut". I created a dataset and filled the...
18
by: dfetrow410 | last post by:
Anyone have some code that will do this? Dave
2
by: x | last post by:
hi i am a pilot by profession. i want to create a database of my logbook using ms access 2002. i am facing a problem regarding the format of time field. when i select "Data/Time" data type for my...
1
by: Susan Bricker | last post by:
Greetings. I have a report (actually all of my reports in an MDB) that I want to date/time stamp at the bottom. Previously, I had used the builtin function of Now(). I thought that would give...
4
by: SilentThunderer | last post by:
Hey folks, Let me start out by letting you know what I'm working with. I'm building an application in VB 2005 that is basically a userform that employees can use to "Clock in". The form...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...
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...

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.