I have created a booking system for a set of resources for schools. Most schools have a membership which entitles them to 2 free sets. I have a booking form with a membership subform (membership table), and a booking details subform (kitloan table).
Once a school is selected on the main form, the membership subform shows the most recent record for that school based on schoolID
I want to display the number of sets they have already had within their membership period (can start at any time of the year, and lasts for 1 year) on the membership subform, so we know how many free ones they have left.
I therefore need to count the number of KitBkID (ID of the booking) in the Kitloan table where SchoolID = the SchoolID displayed on the membership subform, and the DateOut (booking date on kitloan table) is between the DateJoined and DateRenewal displayed on the membership subform (from membership table).
I can do this with a query which works when run and provided with the parameters SchoolID, DateJoined, and DateRenewal.
SELECT Count(Kitloan.KitBkID) AS CountOfKitBkID, Kitloan.SchoolID, Kitloan.DateOut
FROM Kitloan INNER JOIN Membership ON Kitloan.SchoolID = Membership.SCHOOLID
GROUP BY Kitloan.SchoolID, Kitloan.DateOut
HAVING (((Kitloan.SchoolID)=[Me].[SCHOOLID]) AND ((Kitloan.DateOut) Between [Me].[DateJoined] And [Me].[DateRenewal]));
What I can't do is get it to run on the form and take those values from the form.
From the searching I've done, I'm thinking a DCount should be the way to go, but I cannot get the criteria right. I created a query (KitloanCountQry) so that criteria could come from both the kitloan and membership tables.
SELECT Kitloan.KitBkID, Kitloan.SchoolID, Membership.DateJoined, Membership.SCHOOLID, Kitloan.DateOut
FROM Kitloan INNER JOIN Membership ON Kitloan.SchoolID = Membership.SCHOOLID;
I have put the DCount as the control source for a textbox on the Membership subform (but have tried it in VBA too):
=DCount("KitBkID","KitloanCountQry")
This works but obviously gives me the total for all bookings.
This works, but obviously I need to be able to change the ID:
=DCount("KitBkID","KitloanCountQry","SchoolID = 107")
This is what I thought it should be
=DCount("[KitBkID]","KitloanCountQry","[SchoolID] = " & [Forms]![MakeaKitloanBooking]![MembershipSubform].[Form]![SCHOOLID] & " And [DateOut] > #"& [Forms]![MakeaKitloanBooking]![MembershipSubform].[Form]![DateJoined] &"# And [DateOut] <# "& [Forms]![MakeaKitloanBooking]![MembershipSubform].[Form]![DateRenewal] &"# )
Although I have to admit to getting lost in the syntax. This produces #Error.
Sorry for the long winded explanation but hopefully you will understand what I am trying to do.
Any help gratefully received.
Once a school is selected on the main form, the membership subform shows the most recent record for that school based on schoolID
I want to display the number of sets they have already had within their membership period (can start at any time of the year, and lasts for 1 year) on the membership subform, so we know how many free ones they have left.
I therefore need to count the number of KitBkID (ID of the booking) in the Kitloan table where SchoolID = the SchoolID displayed on the membership subform, and the DateOut (booking date on kitloan table) is between the DateJoined and DateRenewal displayed on the membership subform (from membership table).
I can do this with a query which works when run and provided with the parameters SchoolID, DateJoined, and DateRenewal.
SELECT Count(Kitloan.KitBkID) AS CountOfKitBkID, Kitloan.SchoolID, Kitloan.DateOut
FROM Kitloan INNER JOIN Membership ON Kitloan.SchoolID = Membership.SCHOOLID
GROUP BY Kitloan.SchoolID, Kitloan.DateOut
HAVING (((Kitloan.SchoolID)=[Me].[SCHOOLID]) AND ((Kitloan.DateOut) Between [Me].[DateJoined] And [Me].[DateRenewal]));
What I can't do is get it to run on the form and take those values from the form.
From the searching I've done, I'm thinking a DCount should be the way to go, but I cannot get the criteria right. I created a query (KitloanCountQry) so that criteria could come from both the kitloan and membership tables.
SELECT Kitloan.KitBkID, Kitloan.SchoolID, Membership.DateJoined, Membership.SCHOOLID, Kitloan.DateOut
FROM Kitloan INNER JOIN Membership ON Kitloan.SchoolID = Membership.SCHOOLID;
I have put the DCount as the control source for a textbox on the Membership subform (but have tried it in VBA too):
=DCount("KitBkID","KitloanCountQry")
This works but obviously gives me the total for all bookings.
This works, but obviously I need to be able to change the ID:
=DCount("KitBkID","KitloanCountQry","SchoolID = 107")
This is what I thought it should be
=DCount("[KitBkID]","KitloanCountQry","[SchoolID] = " & [Forms]![MakeaKitloanBooking]![MembershipSubform].[Form]![SCHOOLID] & " And [DateOut] > #"& [Forms]![MakeaKitloanBooking]![MembershipSubform].[Form]![DateJoined] &"# And [DateOut] <# "& [Forms]![MakeaKitloanBooking]![MembershipSubform].[Form]![DateRenewal] &"# )
Although I have to admit to getting lost in the syntax. This produces #Error.
Sorry for the long winded explanation but hopefully you will understand what I am trying to do.
Any help gratefully received.