Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

complicated data retrieval process

Status
Not open for further replies.

THWatson

Technical User
Apr 25, 2000
2,601
0
0
CA
Using Access 2003 (2000 format)

I have a report "rptPerfectInRange" that determines the members of a club who have achieved Perfect Attendance within dates selected by the user on a form.

The record source for this report is "qryRangePerfect"
Code:
SELECT qryPerfectStreak.MemberID, qryPerfectStreak.FullName, qryPerfectStreak.StreakStartMonth, qryPerfectStreak.StreakLength, qryPerfectStreak.EndMonth, qryPerfectStreak.LastName, qryPerfectStreak.PreferredName, qryPerfectStreak.Status, qryPerfectStreak.LastPerfectAttendance
FROM qryPerfectStreak
WHERE (((qryPerfectStreak.Status)='Active' Or (qryPerfectStreak.Status)='Senior' Or (qryPerfectStreak.Status)='Leave of Absence'))
ORDER BY qryPerfectStreak.LastName, qryPerfectStreak.PreferredName;

This query pulls from "qryPerfectStreak"
Code:
SELECT tblMembers.MemberID, tblMembers.LastName, getStreakStart([MemberID]) AS StreakStartMonth, DateDiff("m",[StreakStartMonth],[EndMonth])+1 AS StreakLength, getEndDate() AS EndMonth, [LastName] & ", " & [PreferredName] AS FullName, tblMembers.PreferredName, tblMembers.Status, tblMembers.LastPerfectAttendance
FROM tblMembers
WHERE (((getStreakStart([MemberID]))<=getStartDate()) AND ((tblMembers.Status)="Active" Or (tblMembers.Status)="Senior" Or (tblMembers.Status)="Leave of Absence"))
ORDER BY getStreakStart([MemberID]), tblMembers.LastName, tblMembers.PreferredName;

The user selects a Start Date, "txtStartDate", and an End Date, "txtEndDate" on a form.

The parameters in the query are filled from a function
Code:
Option Compare Database
Option Explicit
Public glblEndDate
Public glblStartDate
Public Function getStreakStart(memID As Long) As Date
  Dim currentMonth As Date
  Dim numMeetings As Integer
  Dim numAttended As Integer
  Dim numMakeUps As Integer
  Dim creditedMeetings
  Dim perfectMonth As Boolean
  Dim dtmEndDate As Variant
  
  dtmEndDate = getEndDate()
  If IsNull(dtmEndDate) Or dtmEndDate = 0 Or Not IsDate(dtmEndDate) Then
    dtmEndDate = DateSerial(Year(Date), Month(Date), 1)
  End If
  
  currentMonth = DateSerial(Year(dtmEndDate), Month(dtmEndDate), 1)
  getStreakStart = currentMonth
  perfectMonth = True
Do Until Not (perfectMonth)
  perfectMonth = False
  numMeetings = getNumberMeetings(currentMonth)
  numAttended = getNumberMeetingsAttended(memID, currentMonth)
  numMakeUps = getNumberMakeUps(memID, currentMonth)
  If numMakeUps > 5 Then
    numMakeUps = 4
  End If
  If numMakeUps + numAttended >= numMeetings Then
    perfectMonth = True
    getStreakStart = currentMonth
    currentMonth = DateSerial(Year(currentMonth), Month(currentMonth) - 1, 1)
  End If
 Loop
End Function

Public Function getNumberMeetings(dtmDate As Date) As Integer
  getNumberMeetings = DLookup("Required", "tblMonths", "MonthYear = " & getSQLDate(dtmDate))
End Function
Public Function getNumberMeetingsAttended(memID As Long, currentMonth As Date) As Integer
  Dim monthStart As Date
  Dim monthEnd As Date
  monthStart = getFirstOfMonth(currentMonth)
  monthEnd = getEndOfMonth(currentMonth)
  getNumberMeetingsAttended = DCount("MemberID", "qryMeetingsAttended", "MeetingDate >= " & getSQLDate(monthStart) & " AND MeetingDate <= " & getSQLDate(monthEnd) & " AND MemberID = " & memID)
End Function
Public Function getNumberMakeUps(memID As Long, currentMonth As Date) As Integer
  Dim monthStart As Date
  Dim monthEnd As Date
  monthStart = getFirstOfMonth(currentMonth)
  monthEnd = getEndOfMonth(currentMonth)
  getNumberMakeUps = DCount("MemberID", "qryMakeUps", "MeetingDate >= " & getSQLDate(monthStart) & " AND MeetingDate <= " & getSQLDate(monthEnd) & " AND MemberID = " & memID)
End Function

Function getSQLDate(varDate As Variant) As String
    'Purpose:    Return a delimited string in the date format used natively by JET SQL.
    'Argument:   A date/time value.
    'Note:       Returns just the date format if the argument has no time component,
    '                or a date/time format if it does.
    'Author:     Allen Browne. allen@allenbrowne.com, June 2006.
    If IsDate(varDate) Then
        If DateValue(varDate) = varDate Then
            getSQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
        Else
            getSQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
        End If
    End If
End Function

Public Function getFirstOfMonth(dtmDate As Date) As Date
  getFirstOfMonth = DateSerial(Year(dtmDate), Month(dtmDate), 1)
End Function
Public Function getEndOfMonth(dtmDate As Date) As Date
  getEndOfMonth = DateSerial(Year(dtmDate), Month(dtmDate) + 1, 0)
End Function

Public Function getMin(mkups As Variant, mkupsallowed As Variant) As Integer
  If IsNull(mkups) Then mkups = 0
  If mkups > mkupsallowed Then
    getMin = mkupsallowed
  Else
    getMin = mkups
  End If
End Function

Public Function getEndDate() As Variant
  ' you can pull this off a form
  'getEndDate = forms("yourForm").yourControlname
   getEndDate = Forms("frmPerfectAttendance").txtEndDate
End Function
Public Function getStartDate() As Variant
  ' you can pull this off a form
  'getStartDate = forms("yourForm").yourControlname
  'or return a global variable
   getStartDate = CDate(Forms("frmPerfectAttendance").txtStartDate)
End Function

All of this worked fine until I designed an Archive process which archives off Attendance data which is prior to October of 2 years ago. And in fact it still works fine if the dates the user selects is within the time frame from October of 2007 until now.

What I need to be able to do is retrieve the data from both the current data in tblAttendance and the archived data in tblAttendanceArchive.

That, I assume, means modifying the module's function so that the streak calculates on all available data but I have tinkered around with a number of possibilities and can't find the key to it.

Any push in the right direction would be appreciated.

Tom


 
Change whatever you can to be UNION queries, which union the two tables (current and history) together.

For example, in the function

getNumberMeetingsAttended

it counts number of meetings in query "qryMeetingsAttended". So try just changing qryMeetingsAttended (or maybe a query that it is based on?) to be a union query.

same for function "getNumberMakeUps" and its query "qryMakeUps".


Another thing you could do is to tweak the dcount statements in these functions, which would mean writing similar queries as the two i've mentioned but based on the 'history' table:

getNumberMeetingsAttended = DCount("MemberID", "qryMeetingsAttended", "MeetingDate >= " & getSQLDate(monthStart) & " AND MeetingDate <= " & getSQLDate(monthEnd) & " AND MemberID = " & memID) + DCount("MemberID", "qryMeetingsAttendedHistory", "MeetingDate >= " & getSQLDate(monthStart) & " AND MeetingDate <= " & getSQLDate(monthEnd) & " AND MemberID = " & memID)

I think you just have to poke around and figure out where the least amount of pain would be.

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244. Basics at
 
GingerR
We're on the same page. I printed out a copy of the module and after reviewing it on paper I woke up this morning thinking the same thing as you have pointed out.

So I made Union queries of the data, and changed the references in the 2 lines. They now become...
Piece of code 1
Code:
getNumberMeetingsAttended = DCount("MemberID", "qunMeetingsAttended(IncludesARCHIVE)", "MeetingDate >= " & getSQLDate(monthStart) & " AND MeetingDate <= " & getSQLDate(monthEnd) & " AND MemberID = " & memID)

Code:
getNumberMakeUps = DCount("MemberID", "qunMakeUps(IncludesARCHIVE)", "MeetingDate >= " & getSQLDate(monthStart) & " AND MeetingDate <= " & getSQLDate(monthEnd) & " AND MemberID = " & memID)

The interesting thing now is that when I try to produce a report things work fine until I get back to September, 2006 and then I get a persistent bunch of Error 94 - Invalid use of null in... Piece of code 1. The error message occurs and occurs and occurs, and the only way out is to shut down the database.

I can branch out of the error, but I'd sooner not take that approach because the data is all there.

The current data is from October 1, 2007 to present. The archived data runs from October 1, 2005 to September 30, 2007. And the data is all there in the Union query.

I have an Access Errors table, and it shows no error description for Error 94.

So I'm a bit puzzled.

Tom
 
Hi--well, you are going to have to pick it all apart to find the error. Obviously you are trying to do a calculation on something that is missing.

Just start taking stuff out until it works:

getNumberMeetingsAttended = DCount("MemberID", "qunMeetingsAttended(IncludesARCHIVE)", "MemberID = " & memID)

does that work?

does it work if you put in actual dates instead of getSQLDate()? Sorry but there's no easy solution, you just have to troubleshoot it yourself. What is the qunMeetingsAttended(IncludesArchive) look like? is it doing any calculations that could be on blank data?

Anyhow, forge ahead, let us know if you get stuck...

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244. Basics at
 
GingerR
Thanks. I'm forging (one step ahead, one back).

It would seem that something's missing...trouble is all the data is there in the queries, so i can't quite figure out what's missing.

getNumberMeetingsAttended = DCount("MemberID", "qunMeetingsAttended(IncludesARCHIVE)", "MemberID = " & memID)
doesn't work.

Plugging in actual dates rather than SQL dates doesn't seem to matter.

Tom
 
What doesn't work about it?

you are just going to have to pick thru each part of the queries to find the problem. Does hardcoding in a memberID work?

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244. Basics at
 
I get Error 94 and it locks up and the database has to be shut down through the Task Manager to get out of it.

In other circumstances I get Error 2001 and the database locks up.

I hard coded MemberID = 12 in the module. The query ran but pulled all the records from the query, and the results were incorrect.

I am going to pull together a stripped down version of the database and put it on a public site, so you (or anyone else) can have a look at it.

Tom
 
when you say the results are incorrect, what do you mean exactly?

It would help if you showed us what happens, what you expect to happen, and what is "wrong" with your results.

Thanks!

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244. Basics at
 
Ginger
There are records in the database from October 2005 through to the early dates of July, 2009.

The piece I am working with has to do with determining the # of consecutive months in which members have had Perfect Attendance. Perfect Attendance is deemed to mean meeting a very complicated set of criteria for attendance at Regular weekly meetings + Makeup Activities undertaken.

I hardcoded in MemberID = 12. When I say the results are incorrect I know that this member had 17 months of Perfect Attendance in the range ending in January 2009. However when I hardcoded in his number I received results for all members, and his record showed 1 month rather than 17.

Can't think of anything more to say at this moment, but in fairness I'm not thinking normally at this moment because my wife's brother just died a few hours ago. So if I'm still not being clear please send questions back. Otherwise, just leave it for a bit, as I will take a bit of time to get back to it.

Thanks.

Tom
 
Hi Tom--sorry for your loss. Take a break and get back to it when you can. If you can post the db some place, or compact it and send it to me some time, let me know. datachick10 at hotmail.

g

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244. Basics at
 
Ginger
I tried to put zipped copies of the db on 4Shared.com but with using slow dial-up (which I am compelled to work with here at my trailer where I spend my summer months) the files wouldn't post.

So I am taking your alternate suggested route.

Tom
 
It seems that if I add September 2005 to tblMonths then the functions work properly and I can remove the Nz from the DLookup line and leave it as
Code:
getNumberMeetings = DLookup("Required", "tblMonths", "MonthYear = " & getSQLDate(dtmDate))
and it runs without encountering error 94.

Not sure why that makes a difference but it appears that it does.

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top