Ms-Access DLookup - how to I return only records from this year? - ms-access

I think this should be simple, but I've been struggling for too long.
I have a form with a bound text box which I would like to return a value it has calculated. It calls on the table 'Training' which stores the variables Client, Standard and Date. Client is a string, standard is an integer and Date is in the format dd/mm/yyyy. I would like the box to return the sum of the Standard entries for this year.
So, if we've been out to see Client A last week and done one session and been out again today and done two, the table might look like:
Client A - 1 - 01/01/2017 <br/>
Client A - 1 - 12/01/2018 <br/>
Client A - 2 - 17/01/2018 <br/>
Client B - 1 - 15/01/2018
I'd like my box to return the value '3', ignoring Client A's training last year or the training this year of any other client.
I can get the box to correctly distinguish between clients, but not between years using this code:
=DSum("[Standard]","Training","[Client] = '" & [Client] & "'")
That selects the client from the training table, based on the client record currently selected in the form. It sums all the training that client has ever had - so in the example above returns '4'.
What I am struggling with is how to restrict the year. I've tried a number of variations on:
& "Year([TrainingDate])=#" & Year(Date())
but always get either Name? or Error. What am I doing wrong?
With thanks,
Matt

Use:
=DSum("[Standard]","Training","[Client] = '" & [Client] & "' And Year([TrainingDate]) = Year(Date())")

year is an integer -- try Year([TrainingDate])= " & Year(Date())

Related

Access parameter query not returning records with specific value

Yesterday I asked a similar question, and thank you to those who replied. I'm creating this new question as I've worked a few things out and can hopefully provide more/better information, but still running into the same problem. I'm very, very new to Access and any kind of code or related terms so thank you in advance for your patience with me.
I'm building a query to search tbl: Inventory. There are no lookups built into the table. The parameters can be entered via combo boxes on a form by the user. The row sources for the combo boxes are distinct values from the coordinating fields in tbl: Inventory. I want the user to be able to fill in any/all parameters and get results, so I'm using LIKE "*" in my criteria of each parameter. All values in the parameters are text. In tbl: Inventory the fields are also all text (except the PK which is an autonumber). This is my SQL for the query:
SELECT Inventory.InventoryID, Inventory.SetID, Inventory.ItemName, Inventory.Period, Inventory.Color, Inventory.GarmentType, Inventory.GarmentSubType, Inventory.ATCShow
FROM Inventory
WHERE (((Inventory.Period) Like "*" & [Forms]![frmSearchInventory]![cboPeriod] & "*") AND ((Inventory.Color) Like "*" & [Forms]![frmSearchInventory]![cboColor] & "*") AND ((Inventory.GarmentType) Like "*" & [Forms]![frmSearchInventory]![cboType] & "*") AND ((Inventory.GarmentSubType) Like "*" & [Forms]![frmSearchInventory]![cboSubType] & "*") AND ((Inventory.ATCShow) Like "*" & [Forms]![frmSearchInventory]![cboShow] & "*"));
Please excuse the excess brackets, as I said, very new so I'm letting Access do most of the writing for me.
For some reason, no matter what combination of parameters are used, it will not return any records where Inventory.GarmentType = suit coat, and the record also includes a SetID--even if the only parameter entered is "suit coat". For records that do not include a setID and are type=suit coat, the record is returned. If the record includes a setID and is not a suit coat, the record is returned. Not sure if that is related, as SetID is not a parameter, but it's something I noticed.
I've tried changing "*" to "%" and LIKE to ALIKE and no records at all were returned.
Any ideas? Happy to provide any additional information. Thank you!

DSUM in Access query yields Nulls in random records

I have a query (in MS Access 2013) that gives me the Sales for various Items by Date, for each day for the next 12mo. In another table, I have each Item with its known purchasing leadtime, say between 30 and 90 days.
I created a query, and for each Item, I calculated the future date according to the leadtime, using:
FutureDate: DateAdd("d",[Leadtime],Date())
I validated all Items exist in the Sales query, and all FutureDates are within the records that exist in Sales.
I need to calculate the sum of daily Sales between now and the calculated [FutureDate] for each Item, to get the total amount of Sales expected within the unique Leadtime of each item.
I tried function DSUM() with weird results:
The query of daily Sales already excludes any past sales, so my first try was:
TotalSalesInLeadtime: DSUM("DailySales","Sales","[DayOfSale]<=#" & [FutureDate] & "# AND [Item]='" & [SearchedItem] &"'")
For some Items, [TotalSalesInLeadtime] calculated correctly, while others evaluated to Null.
Then I tried:
TotalSalesInLeadtime: DSUM("DailySales","Sales","[DayOfSale] BETWEEN #" Date() "# AND #" & [FutureDate] & " AND [Item]='" & [SearchedItem] &"'")
The results now were reversed. [TotalSalesInLeadtime] values now showed correctly for the items that previously showed Null, and were Null for items that previously evaluated correctly.
I never figured out why DSUM() did this.
To work around the DSUM() glitch, I went with an embedded subquery, which yielded all the values correctly, albeit at a significant performance hit:
SELECT [PurchItem],
(SELECT Sum([DailySales]) AS SumOfSales
FROM [Sales]
WHERE ([Item]=[LeadtimeItems].[PurchItem]) AND ([DayOfSale] Between Date() AND [LeadtimeItems].[FutureDate]))
As TotalSalesInLeadtime
FROM LeadtimeItems
If anyone has a clue why DSUM may behave this way, I'd appreciate the help. DSUM, when it works, certainly seems to go faster.
When "gluing together" SQL statements (or fragments) that include date literals enclosed in hash marks (#), one must bear in mind that Access SQL and VBA will always interpret ambiguous date literals as mm-dd-yyyy regardless of the system-wide date format. So on a machine where Windows has been configured to use dd-mm-yyyy, an unambiguous date like April 30 will work okay
?DateSerial(2013,4,30)
30-04-2013
?"#" & DateSerial(2013,4,30) & "#"
#30-04-2013#
?Eval("#" & DateSerial(2013,4,30) & "#")
30-04-2013
...but for the next day, May 1, things don't work so well
?DateSerial(2013,5,1)
01-05-2013
?"#" & DateSerial(2013,5,1) & "#"
#01-05-2013#
?Eval("#" & DateSerial(2013,5,1) & "#")
05-01-2013
So the lesson is that any time we "glue together" date literals we must ensure that those dates are in an unambiguous format like yyyy-mm-dd. With regard to this particular question, we need to use
TotalSalesInLeadtime: DSUM("DailySales","Sales","[DayOfSale]<=#" & Format([FutureDate], "yyyy-mm-dd") & "# AND [Item]='" & [SearchedItem] &"'")

How to iterate over table in Access 2010

I would like to iterate over the rows in my Payment table. User chose for what month and year has wants to book and I want to check in the each raw if this house was booked for this year and month. I want to compare if
HouseID == chosenHouseID && BookingMonth == chosenBookingMonth &&
BookingYear==chosenBookingYear.
If this is true it should pop out message box with info that house was already booked for this month. Also if user chose more than one month i.e. numMonths would be 3, it should increment value of the month (which is a text) it should go to the next value (if there is no next value then it should do mod 12) and do the checking again. Maybe it will be necessary to switch data type of BookingMonth to numeric?
However I hope I was clear what I want to do. I have experience with Java, C, Python and Visual Basic, but I did not do much in Access so it is quite confusing. I could not find the any useful info how to perform this operation. Please advise me on my issue.
Thank you
Yes, you definitely should store the [BookingMonth] as numeric. Maintaining a "month" column as text will be a nuisance in the long run, since "August"<"January" and "12"<"2". You'd have to do at least a certain amount of juggling to convert the text values to numeric values, so make like easy for yourself and just maintain them as numeric. (Note that you can always format them as text if you want to use them in reports.)
As for your search requirements, if the user supplies a [chosenBookingYear], [chosenBookingMonth], and [numberOfMonthsToBook] then you can use the VBA DateAdd function to derive [endOfBookingYear] and [endOfBookingMonth] safely, accounting for "next year" values...
endOfBookingYear = Year(DateAdd("m", numberOfMonthsToBook - 1, DateSerial(chosenBookingYear, chosenBookingMonth, 1)))
...and...
endOfBookingMonth = Month(DateAdd("m", numberOfMonthsToBook - 1, DateSerial(chosenBookingYear, chosenBookingMonth, 1)))
Finally, to perform the lookup without looping through individual rows you can concatenate [BookingYear] and [BookingMonth] together to create something like "2013/05" using...
BookingYear & "/" & Format(BookingMonth, "00")
...so then you can create a SELECT query something like this:
SELECT * FROM Payment
WHERE HouseID = chosenHouseID AND
(
(BookingYear & "/" & Format(BookingMonth, "00"))
BETWEEN (chosenBookingYear & "/" & Format(chosenBookingMonth, "00"))
AND (endOfBookingYear & "/" & Format(endOfBookingMonth, "00"))
)

Totals on a report summary

I have an Access Report. In the footer section of my report I have a list of different totals. I am looking to create these totals based on a time criteron. For example:
Im looking for a count of the included records. I need to determine a count based on either current- 3 months, 4-6 months, 7-12 Months and 13+ Months.
I have created a DateDiff() expression to determine the amount of months. I have created another expression to assign a letter based on what group the result would belong to. For example:
A = Current - 3months
B = 4-6 Months
C = 7-12 Months
D = 13+ Months
How could I use the assigned letter as a count on my report? Could I make a statement in my control source for my display text box to accomplish this?
I'm not sure where to go next...
In the reports On_Open event write some VBA to store the values from your DateDiff into several different String Variables, and then if your footer is a simple text box just update the values e.g.
txtFooter = "3Mnths - " & str3Mnths & " - 4/6Mnths - " & str46Mnths

ASP AND MS ACCESS

set rs5 = objconn.execute("Select Sing from LeaveEntitlement where MonthEntitle = '"& Month(ttodate)& " ' ")
do until rs5.eof
if rs5("Sing") then
s = rs5("Sing")
Loop
In the Database table LeaveEntitlement, MonthEntitle is a Field Nmae(Text Data type) from January...December.Sing is another Field(Number Data type) with values for each Month.But
S is returning nothing.
Can help for solving this.
Oded, Thanks a lot for solving my issue.
Just taking a stab in the dark it looks like you have an extra space at the end of your sql where you put in the month name. Probably not matching any records.
There are probably no rows in the table corresponding to the month you are searching on.
Have you checked directly against the database that the query is returning any rows?
Edit:
It appears that you are using month names in your database, however the Month function returns a month number. You are enclosing that in ', so you are sending a string with the number in the query - this is why you do not get a type error and why no results are being returned.
You can use MonthName with Month:
where MonthEntitle = '" & MonthName(Month(ttodate)) & "' "