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

Date range query help please.

19
I'm working with mine production data in Access 2007. I'll try to make this make sense and keep it simple (mostly to not confuse myself further).

The range of years operating varies between mines. For example, I have data for one of the mines for the years 1961 through 1991. How can I get a query to report the years of operation, like 1961 - 1991, in a single cell? The Production.Year column of the Production table is formatted as a number and not a year/date. I can't change that(permissions).

The fields should be something like:
ProjectID (from Project.ProjectID)
DepositName (from Project.ProjectName)
Country (from Project.CountryName)
YearsOperating (from Production.Year) This is the one I need.
Apr 14 '09 #1
20 1952
FishVal
2,653 Expert 2GB
Hello, kujito.

Though I didn't understand your question completely, the following thread might give you some clues.
Grouping Query Range

Regards,
Fish
Apr 14 '09 #2
kujito
19
Sorry about the confusion. Take two:
I have a production table which has data for all the years that the mines were/are operating. The Production table (simplified) looks like:
ProductionID(pk), ProjectID(fk, Project table pk), Year, Metal1, Metal2, Produced1, Produced2

Each mine is represented by one record and ProductionID for each year of operation. I'd like to have the range of operating years shown in a single cell, like 1961-1991 for the Climax mine, 1976 - 2006 for the Henderson mine, and so on. I have other data I need in the query, but I have been able to get those.
I'm trying for something like this:
P-ID | Name | Country | YearsOP |
31086 | Climax | USA | 1961-1991 |
28919 | Henderson | USA | 1076-2006
Apr 14 '09 #3
FishVal
2,653 Expert 2GB
Well.

A simple grouping query can do it. Something like the following:
Expand|Select|Wrap|Line Numbers
  1. SELECT ProjectID, Min([Year]) & "-" & Max([Year]) As txtPeriod
  2. FROM Production
  3. GROUP BY ProjectID;
Apr 14 '09 #4
kujito
19
Thanks, I'll give that a whirl.
Apr 14 '09 #5
NeoPa
32,556 Expert Mod 16PB
That link may give you a full answer, but basically, you need a GROUP BY clause to exclude the years. Aggregate functions can be used to return the Min() & Max() of the years to display.
Apr 14 '09 #6
kujito
19
Thanks FishVal! I got it to work with only minor tweeking!
It's been a few days, and now there's a new requirement (of course). I now need an additional field which selects the years for which there was no production.
Continuing with the theme from above:
P-ID | Name | Country | YearsOP | YearsNoMO
31086 | Climax | USA | 1961-1991 | 1983, 1988
28919 | Henderson | USA | 1976-2006 | 1975, 1983
104004 | Endakko | CAN | 1965-2007 | 1999, 2000, 2001, 2002, 2006

The years without production values vary, both in year and number of years, by mine.
Can I select multiple values like that to a single cell? If so, how?
I'm still looking around, but this seems like the place for real answers.
TIA,
Brian
Apr 21 '09 #7
FishVal
2,653 Expert 2GB
Ok, Brian.

Do you have some skills in VBA?
Apr 21 '09 #8
kujito
19
Some, at a novice level. I did take an 'Intro to Visual Basic' class. I've not had to do much with it, but I'm not totally unfamiliar with how the scripts look/work as ArcGIS (just got my BS in GIS) uses VB for some things.
Why, what do you have in mind? I'm willing to try/learn. Is it something that's already part of Access? I don't have Visual Studio here.

I don't have to have this out right away, so I've got some time to make it work.
I really appreciate the help btw.
Apr 21 '09 #9
FishVal
2,653 Expert 2GB
Good.

Access allows to call public functions written in VBA from query. This gives a very suitable method to get calculated field in context of each record fetched by query.
Let us say, in your case it will be function getting [P-ID] and returning comma separated list of "no production years".

The function has to be placed in public code module.
Expand|Select|Wrap|Line Numbers
  1. Public Function YearsOfNoProduction(lngMineID As Long) As String
  2. ........
  3. End Function
  4.  
Then it will be invoked in query:
Expand|Select|Wrap|Line Numbers
  1. SELECT ....., YearsOfNoProduction([P-ID]) AS YearsNoMO FROM .....
  2.  
The function code do the following:
  • opens a recordset from your source table filtered by [P-ID] and ordered by year
  • iterates the recordset
  • if next record year is not current record year + 1 then add to output string sequential years until it is
Apr 21 '09 #10
NeoPa
32,556 Expert Mod 16PB
I'm not sure, but could
Producing a List from Multiple Records be helpful?
Apr 21 '09 #11
FishVal
2,653 Expert 2GB
@NeoPa
This requires non-existing year/mine combinations to be fetched as records.
It could be achieved by holding auxiliary table with sequential years list. This table is joined to produce cartesian product with distinct list of mines, and thus obtained dataset is filtered with outer join with OP's table. And it is only a half of the story. ;)
Apr 21 '09 #12
kujito
19
Ya, I looked at the help menu stuff about multivalued cells. It got me nowhere.

A little fuzzy on the VBA stuff you posted FishVal.
Where in Access would I do this? Not sure of the syntax either.
Apr 21 '09 #13
NeoPa
32,556 Expert Mod 16PB
@FishVal
OK Fish. I'll leave this in your capables then :)
Apr 21 '09 #14
kujito
19
Found the 'VB' window in the 'Macro' tab. Need to go home and brush up on my vb syntax tonight. Hopefully I can figure this out, I'll be back asking questions again tomorrow if not.
Any hints are welcome in the mean time. :)
Apr 21 '09 #15
FishVal
2,653 Expert 2GB
@kujito
Add the following function to code module.

Expand|Select|Wrap|Line Numbers
  1. Public Function YearsNoMO(lngProjectID As Variant) As String
  2. 'lngProjectID is expected to be a Long integer, but since query field
  3. 'could be Null it is declared as Variant
  4.  
  5.     Dim rs As New ADODB.Recordset
  6.     Dim intYear As Integer 'variable to store previous record year
  7.  
  8.     If IsNull(lngProjectID) Then Exit Function
  9.  
  10.     With rs
  11.  
  12.         'set properties of Recordset object
  13.         .ActiveConnection = CurrentProject.Connection
  14.         .CursorType = adOpenForwardOnly
  15.         .LockType = adLockReadOnly
  16.         'open source table filtered by ProjectID and ordered by Year
  17.         .Open "SELECT intYear FROM tbl WHERE lngProjectID=" & _
  18.             lngProjectID & " ORDER BY intYear;"
  19.  
  20.         'if recordset is not empty then start computing
  21.         If Not (.EOF And .BOF) Then intYear = !intYear
  22.  
  23.         'iterate recordset
  24.         While Not .EOF
  25.             'add non-existing years to output
  26.             While intYear < !intYear - 1
  27.                 intYear = intYear + 1
  28.                 YearsNoMO = YearsNoMO & intYear & ", "
  29.             Wend
  30.             intYear = !intYear
  31.             .MoveNext
  32.         Wend
  33.  
  34.         'close recordset
  35.         .Close
  36.  
  37.     End With
  38.  
  39.     Set rs = Nothing
  40.  
  41.     'remove trailing comma and space
  42.     If Len(YearsNoMO) <> 0 Then _
  43.         YearsNoMO = Left(YearsNoMO, Len(YearsNoMO) - 2)
  44.  
  45. End Function
  46.  
And use it in your query.
Apr 21 '09 #16
kujito
19
Wow! Thanks again! If you weren't so far away, I'd take you out for some beers (if you're ever in/around Denver, CO, USA ...).
I probably wouldn't have been able to come up with all that in a decent amount of time. I'll have to play around with that code and see what I can do.

Thanks again! He doesn't know it yet, but you just made my boss very happy!
Apr 22 '09 #17
kujito
19
Sorry for the length on this one.
OK, so I finally got to it.
All I changed from the code you gave me is from this:
.Open "SELECT intYear FROM tbl WHERE lngProjectID=" & _
lngProjectID & " ORDER BY intYear;"
The bold 'tbl' was changed to Production (the table containing the year data, ProjectID is fk). I also tried it with Project (the table with ProjectID as pk).

My query looks like this:
SELECT Project.ProjectID, Project.ProjectName, YearsNoMO([Production.ProjectID]) AS NoMO
FROM Project INNER JOIN Production ON Project.ProjectID = Production.ProjectID
WHERE Project.PrimaryMO = 1;
The 'PrimaryMO' simply restricts it to Primary MO producers. Once I get this straight, I will be running one for 'PrimaryMO = 2'; bi-product producers.

The query results in an error message:
Run-time error '-2147217904 (80040e10)'
No value given for one or more required parameters.
Clicking "Debug" results in this bit being highlighted:
.Open "SELECT intYear FROM Production WHERE lngProjectID=" & _ lngProjectID & " ORDER BY intYear;"

This is where I changed the original 'tbl' to 'Production' or 'Project' (tried both).
Not sure what to do now. The debugger seems to have locked me out of/frozen everything else. When I try to close the VB window, I get a message that doing this will stop the debugger. Click 'OK' and I get the same error message as above.
Apr 22 '09 #18
FishVal
2,653 Expert 2GB
Well.
Example worth thousand words.
Attached Files
File Type: zip kujito.zip (13.6 KB, 76 views)
Apr 22 '09 #19
kujito
19
Ah, see, my Production table is not MISSING the years where there is no MO produced. The no production years have a 0 (zero) entry for Production.Produced1 (for Project.PrimaryMO = 1, it would be a 0 for Production.Produced2 for Project.PrimaryMO = 2)

I've added my (trimmed down) Project and Production tables to the dbse you sent me. I've also queried out the Projects and Productions I'm concerned with at the moment. Maybe that will help clear things up? I'll see if I can't figure something out with what you've sent me so far.

There's nothing that's not publicly available in the tables. I did a lot of web a library research to find it, but it's nothing proprietary or any such concerns.

p.s.
I may have to up it from beers to a nice bottle of booze.
Attached Files
File Type: zip kujito2.zip (35.3 KB, 78 views)
Apr 22 '09 #20
FishVal
2,653 Expert 2GB
  • It makes method suggested by NeoPa in post #11 quite feasible.
  • The records with 0 production could be filtered out and the code will work as they never were.
  • Actually, this case the code could be significantly simplified
    Expand|Select|Wrap|Line Numbers
    1. Public Function YearsNoMO(lngProjectID As Variant) As String
    2. 'lngProjectID is expected to be a Long integer, but since query field
    3. 'could be Null it is declared as Variant
    4.  
    5.     Dim rs As New ADODB.Recordset
    6.  
    7.     If IsNull(lngProjectID) Then Exit Function
    8.  
    9.     With rs
    10.  
    11.         'set properties of Recordset object
    12.         .ActiveConnection = CurrentProject.Connection
    13.         .CursorType = adOpenForwardOnly
    14.         .LockType = adLockReadOnly
    15.         'open source table filtered by ProjectID and ordered by Year
    16.         .Open "SELECT intYear FROM tbl WHERE lngProjectID=" & _
    17.             lngProjectID & "AND [Produced]=0;"
    18.  
    19.  
    20.         'iterate recordset
    21.         While Not .EOF
    22.              YearsNoMO = YearsNoMO & intYear & ", "
    23.              .MoveNext
    24.         Wend
    25.  
    26.         'close recordset
    27.         .Close
    28.  
    29.     End With
    30.  
    31.     Set rs = Nothing
    32.  
    33.     'remove trailing comma and space
    34.     If Len(YearsNoMO) <> 0 Then _
    35.         YearsNoMO = Left(YearsNoMO, Len(YearsNoMO) - 2)
    36.  
    37. End Function
    38.  
Apr 23 '09 #21

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

Similar topics

5
by: BlackFireNova | last post by:
I need to write a report in which one part shows a count of how many total records fall within the working days (Monday - Friday) inside of a (prompted) given date range, in a particular...
4
by: Tony | last post by:
Hey guys, I use Google Groups quite a bit as it is an enormous wealth of information, and now I need some help. I have created a query using parameters to capture a range of date, the date is...
12
by: Steve Elliott | last post by:
I have a query set up to gather together data between two specified dates. Shown in the query column as: Between #24/09/2004# And #01/10/2004# Is it possible to enter several different date...
3
by: manning_news | last post by:
Using A2K. I've been asked to modify a report currently requiring only one date parameter to now accept a date range. The main report has 2 subreports and is not bound to a table or query. The...
67
by: PC Datasheet | last post by:
Transaction data is given with date ranges: Beginning End 4/1/06 4/4/06 4/7/06 4/11/06 4/14/06 4/17/06 4/18/06 4/21/06 426/06 ...
9
by: mharrison | last post by:
Hello, I am developing a small java web-based car-pool booking system app which interacts with an access database. I am trying to write 2 queries: The first which will specify whether a given car...
1
by: irfanali | last post by:
Hallo All, This is a Tool i m tryin to develop at work. I will explain how it works and then the Q I download a report from my ERP Tool on a daily basis and upload it into the Access Tool....
2
by: sixdeuce62 | last post by:
Hello, I am trying to create a query that will prompt me to enter the parameter value if beginning date and ending date. I have created everything I need in the query, but I have to manually go...
4
Sandboxer
by: Sandboxer | last post by:
I want to be able to program Access to provide for me, by individual day, what my contract obligations are to my customers. Will Access recognize all the individual days in between a date range...
19
by: phill86 | last post by:
Hi I am re-posting this thread because it has become very confusing and I have got some way to solving the problem so it is a slightly different question from the initial thread. here is the...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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.