Hiding Rows with Blank Fields in Access Subreport - ms-access

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));

Related

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]").

Syntax In Access 2010 IIF Query

I am a beginning user of Access, and am trying to build a query inside of a form.
I am working on my first IIF query, and am having trouble getting the desired behavior.
I am trying to pull results based on 4 entries, called [band], [band2], [band3] and [band4].
Here is where I'm at:
Like IIf(IsNull([forms]![financial_filter]![band]),"*",[forms]![financial_filter]![band]) Or Like [forms]![financial_filter]![band2] Or Like [forms]![financial_filter]![band3] Or Like [forms]![financial_filter]![band4]
This means that if the [band] field is empty, it will pull all results for all bands. But if the [band] and/or [band2-4] fields are filled in, it will pull results from only those fields. This is all working.
BUT, if a user leaves the [band] field empty and only enters something in the [band 2-4] fields, all results show up, not just those related to the data in the [band 2-4] fields. Please advise how to modify my query so that if a user chooses one or more bands but does not put anything into the [band] field, only the selected results will populate.
Thanks!
Mike
IIF has nothing to do with Query.
The IIF statement is a shortcut for a IF .. THEN .. ELSE .. END IF statement.
MyResult =IIF (Condition, Make1, Make2) means
IF Condition THEN
MyResult=Make1
ELSE
MyResult= Make2
END IF
You can nest IIF statements, but the result will be difficult to read and also to maintain.
So, in your case I would suggest to write a couple of lines with your business logic (IF THEN ELSE END IF) or (IF THEN ELSEIF ... END IF) in the OnCurrent Event of your form, improving future maintenance and readability of your code.

DSUM won't let me give it criteria

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] & "'")

Efficient use of calculations in MS Access

This is the kind of thing I feel I should already know, but don't...
In MS Access, if a query consists of n calculated fields (say calc_1, calc_2, .... calc_n) but I only want to use a subset of them in a particular form or report - say calc_x, calc_y and calc_z - would Access calculate all n calculations when running SELECT calc_x, calc_y, calc_z FROM myquery and then return the ones I want, or would it be smart enough to only calculate calc_x, calc_y and calc_z?
In my case I'm using Access 2003 but presumably the answer is the same for all versions.
Access is smart and don't calculate not-visible fields.
Tried with Access 2003
Like #David, I did not believe #Andreas's answer at first. So I tested it myself as follows. I created the following function:
Function Watch(Val, Optional CalledFrom As String = "")
Debug.Print Val, CalledFrom
Watch = Val
End Function
Then I created a table named "Dummy" with a single field named "ID". I created a form and used the following as the form's RecordSource:
SELECT Watch([ID],"ShowInForm") AS ShowInForm,
Watch([ID],"HideFromForm") AS HideFromForm
FROM Dummy;
I added a single textbox control with a ControlSource of ShowInForm.
I then opened the form and got this in the immediate window:
1 ShowInForm
1 ShowInForm
1 ShowInForm
I then went back to the RecordSource and previewed it in Datasheet view and got this:
1 ShowInForm
1 HideFromForm
I'm not sure why the "ShowInForm" expression is evaluated three times in the form, but it seems pretty clear that the unused field, "HideFromForm", does not get evaluated.
To address #HansUp's comment, I went back and saved a query named "Qry":
SELECT Watch([ID],"ShowInForm") AS ShowInForm,
Watch([ID],"HideFromForm") AS HideFromForm
FROM Dummy;
Then changed the form RecordSource to:
Select ShowInForm FROM Qry
This produced the same result as before when I opened the form (ie, 3 lines of 1 ShowInForm). Interestingly, when I opened the RecordSource in datasheet view I got this:
1 ShowInForm
1 ShowInForm
In other words, it evaluated the ShowInForm field twice. Presumably, once in "Qry" and again in the RecordSource query.
The end result is still a confirmation of #Andreas's answer.
If you include the calculated fields in the SELECT clause of your report or form's recordsource, it will calculate them for each row AS IT IS RETRIEVED.
If you leave them out of the SELECT and include the calculations only in the ControlSource properties of controls on your report/form, then what you say is true.
Also, if you do any sorting/grouping on the calculations or put criteria on them, all rows will be calculated.
Thus, in this recordsource:
SELECT Field1/Field2 As Ratio1, Field3/Field4 As Ratio2, FIeld1, Field2, Field3, Field4
FROM MyTable;
...for each row that is retrieved, both calculations will be executed regardless of whether or not the result is used in the form or report.
If you want to delay calculations to the last possible moment, do NOT include them in the SQL recordsource, but just use them as the ControlSources of the controls that are displaying the calculations. However, this has the downside that you'll see the calculations painting onscreen in many cases.
EDIT:
It seems this may not be correct, but I feel there's something going on here that is not completely explained by #mwolfe02's answer. I'll just leave this here for further discussion.
I put calculations like this on the server side.. computed columns are awesome.
I think that Access finally got this feature in 2007, I don't know how people ever survived without it.

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])