DSUM won't let me give it criteria - ms-access

I have a few tables: Order, OrderLine, and Cost being relevant here.
In my Order entry form, I have two subforms, one for OrderLine and one for Cost. At the footer, I have a "subtotal" which I simply want to sum up my extended prices on my orderline sub form. I tried doing something like this:
=Sum([OrderLine subform].[Form]![PriceExtended])
But it gives me an #Error (without ever telling me the error anywhere, that's another annoying problem...) Well, okay that's not a problem we can just go straight to the database with it. So I tried using DSUM like so
=DSum("PriceExtended","OrderLine","OrderUnique=" & [OrderUnique])
And that gave me an error and it just made the box blink... well okay it's not picking up the OrderUnique field, so we'll try hardcoding it
=DSum("PriceExtended","OrderLine","OrderUnique=SHOP1234")
Nope, still giving me the stupid blinking... okay then, let's try no criteria
=DSum("PriceExtended","OrderLine")
And that works just fine, except of course it sums up every order ever and I only want to sum up the lines for this particular order.
So why would my DSum criteria not accept "OrderUnique=SHOP1234"?

Since OrderUnique is defined as a Text field, you need to surround the value with single quotes or double quotes. You want the criteria string to look like:
OrderUnique='SHOP1234'
, so you would hard code that as
=DSum("PriceExtended","OrderLine","OrderUnique='SHOP1234'")
or build it with
=DSum("PriceExtended","OrderLine","OrderUnique='" & [OrderUnique] & "'")

Related

Use IIF and LIKE statement in critera column in queries, MS Access

I have homework from Microsoft Access for school and one of the tasks was to make a search form in query, where you type for example 1. grade, 2. grade or 1st year, etc... But the field contains the name of the classes such as IT1, IT2, E1A, E3C, etc... So I could not just make a search form with [Enter Class:] or Like [Enter Class:]&"*" in the criteria column. So I was thinking, that I can use the IIF statement but it doesn't work how I imagined. I think there is a problem with the LIKE statement. I read on forums, that the IIF statement should be used in the field column but I tried the simple examples in the criteria and it worked just fine. So my question is, how I can make a search form, where I type numerous letters with one certain number in it BUT only the number will be read and returned with the same number in classes. Example: I type to search 1. grade and the value will return classes IT1, E1A, E1B, E1C, E1D. This is the line I used in the criteria column:
Like IIf([Enter the grade:]="*1*";"*1*";IIf("*2*";"*2*";IIf("*3*";"*3*";IIf("*4*";"*4*"))))
Just to qualify, I'm beginner in access and databases overall, so there is big possibility, that I'm missing something.
Thank you for help! Cheers!
Assuming input will always start with a number and you want all values containing that number, consider:
WHERE fieldname LIKE "*" & Val([Enter grade]) & "*"
Also assumes values contain only single digit.

Dlookup on textbox is not working

I have been researching on MS-Access topics around DLookup, but not being lucky on the resolution of my problem.
I have a query that solely returns one value, which is a lumpsum of credits. So I used the clause "AS TOT_CREDIT" on the query to give the unique column a name.
On access report, I learned that you can't directly set the value of a textbox from a query, but also learned the magic is to set the textbox controlsource property to dlookup, like this: dlookup([TOT_CREDIT]; [QUERY THAT CONTAINS TOT_CREDIT]). When I pull the report from access, the textbox still displays the infamous "#Name?", instead of the query value.
Is anything missing here? What else can I do in order to have the textbox display the query result?
Must use quotation marks:
DLookup("[TOT_CREDIT]"; "[QUERY THAT CONTAINS TOT_CREDIT]").

Hiding Rows with Blank Fields in Access Subreport

I have a subreport, ExpenSubrpt, in Access 2010 that’s basically a table with two columns: YearNo, and Amount. These values are taken from another table, Expenditures. Long story short, some records in my database have rows with no YearNo. What I want to do is to hide the rows that don’t have a YearNo, ie. YearNo is blank but there is something in Amount. For example:
YearNo Amount
20
1 50
I want the first row to not be visible. I’ve tried putting
"[YearNo] <> " " AND [YearNo] IS NOT NULL"
into the Data -> Filter tab, but it gives me a syntax error (missing operator). When I removed the space, the error goes away but it doesn't filter, either. I've also tried calling a query by using the OnLoad event.
Private Sub Report_Load()
DoCmd.OpenQuery "Expenditures Query"
End Sub
Query:
SELECT Expenditures.YearNo, Expenditures.Amount
FROM Expenditures
WHERE (((Expenditures.YearNo) Is Not Null));
However, this doesn't work either. I’m not really sure what to do from here, or if there’s a better way to filter this form. Any assistance would be greatly appreciated.
You might want to change your query to have the "Not" before the field name, like this...
SELECT Expenditures.YearNo, Expenditures.Amount
FROM Expenditures
WHERE ((Not(Expenditures.YearNo) Is Null));

Returning specific field from a matching record

I'm still a newbie at Access 2007, but I feel I am missing a understanding of a concept, when it comes to using user input from an unbound text box.
I'm trying to have the user input the record number (i.e. A12) and return another field in the matching record (such as the record status like "Opened")
I'm fiddling with DLookup to see if it will work through that method but no luck yet.
I may look into SELECT - SQL, but I haven't used that function yet and not sure if that will give me the result I'm looking for.
If this is something elementary to access programming (or databases in general), please let me know where I can read up on this.
I am currently using the MSDN website, but examples go much further to play with.
Edit:
My DLookup so far, which happens after update from user on Text12
Me.Text14.Value = DLookup("[RecordStatus]", "Orders", Text12.Value)
Thanks
Look closer at the third option (Criteria) in your DLookup() expression. You gave it only Text12.Value, which I assume is a string value like "A12".
The Criteria parameter should be like a WHERE clause in a query, without the word WHERE. IOW, some field's value = "A12". If that field is named "record_id", try this:
DLookup("RecordStatus", "Orders", "record_id = '" & Me.Text12 & "'")

DSum returning number of rows instead of total value

I have the following code attached to a text box in a form:
=DSum("[subform].Form![POINTS]","ATTENDANCE","[subform].Form![EMPLOYEE NO] = [EMPLOYEE NO]")
Ideally this would yield the total amount of points accrued by the employee we are currently searching for. However, what I am getting is the total sum of rows in my table.
Does anybody have any idea of how I could get the total sum of the values instead of the number of rows?
Thanks
If you want to get the total from a subform, and your subform in in sync with the main one, it will be much more efficient to procede this way:
create txtTotalPoints textbox = sum(Points) in the footer of your subform
refer to that control from your main form: txtMainResult =subform!form!txtTotalPoints
Hide txtTotalPoints (or the footer itself)
That will generally be much faster.
As far as I know, the Domain functions such as DSum, DLookup, DCount etc. are used to lookup and return values from a table. The first argument is the field, the second the table, and the third is the criteria or WHERE statement that makes sure you get the correct set of records. Your first argument refers to a form's field. I think this is incorrect. Your first item in your WHERE statement is also a form field. I this this is also incorrect. You need to try something like this instead:
=DSum("POINTS","ATTENDANCE","[EMPLOYEE NO] = " & [subform].Form![EMPLOYEE NO])