Using Sum IIF within placeholder expression - reporting-services

I have the following expression as a placeholder within a textbox which gives me an error of Too many arguments, what can I do to change this please so that I can count the number of current interviews in dataset 1?
Probably very easy to figure but I've now been looking at it too long!
="<b>" & "Current Interviews" & "</b>" & vbcrlf & Sum(IIF(Fields!Count.Value, "DataSet1" = "Current",1,0))

I can't test at the moment but I think it should be
="<b>" & "Current Interviews" & "</b>" & vbcrlf & Sum(IIF(Fields!Count.Value = "Current",1,0), "DataSet1")
The scope expression ("DataSet1")should be the last argument in the SUM()

Related

Access: Filtering form on field - fails when fields contains an apostrophe

I have a filter on a continuous form that uses a Combo Box to select records to match; the code is:
Private Sub SelectHospitalCbo_AfterUpdate()
Me.Filter = "[ContactHospital] = " & "'" & Me.SelectHospitalCbo & "'"
Me.FilterOn = True
End Sub
This was working fine until I discovered that if the ContactHospital field includes an apostrophe (e.g. Children's Hospital) I get an error message:
Run-time error '3075':
Syntax error (missing operator) in query expression '[ContactHospital] = 'Children's Hospital".
I understand why the error is occurring, but I can't find a workaround. A recent question on this forum seemed to have a similar problem to mine, but there were no answers. Does this mean I can't get around it?
In case anyone wants to suggest removing all the apostrophes form the hospital names, I would consider that, but unfortunately this database interacts with a (much larger) database where the hospital names can't be changed and have to match, so that's not an option for me.
Any help from more experiences Access developers appreciated!
Options:
filter by numeric hospital ID instead of its name
"[ContactHospital] = '" & Replace(Me.SelectHospitalCbo, "'", "''") & "'"
"[ContactHospital] = """ & Me.SelectHospitalCbo & """"
"[ContactHospital] = " & Chr(34) & Me.SelectHospitalCbo & Chr(34)

Multiple filter clauses for query and subsequent report from unbound form controls, button activated

Been a while since I used Access and now stuck with a reporting problem.
I have a form, with various unbound controls. There a start date, end date and three levels of business/asset/location in combo boxes.
I can get my query and report to work for each of these as individual 'where' clauses when clicking on a button. That's fine.
However I would like to know what code I need to use when clicking a button so I can combine one or more of the above controls to filter the report i.e. my date range + Business, or date + Business + Asset etc.
I have been trawling the internet and testing different variations but seem to have gone through the error book so far.
My latest effort (on click) as one long string gives me a data mismatch error. If I remove the BU/Asset/Facility parts then my date range code does work. However, it's the combination of these I want to filter by.
DoCmd.OpenReport ReportName:="rptVerification_Results", View:=acViewPreview, WhereCondition:="[Date Entered] Between #" & Me.StartDate & "# AND #" Me.EndDate & "#""" And "BU = " & Me.cboBusiness & "" And "Asset = " & Me.cboAsset & "" And "Facility = " & Me.cboFacility & ""
As you can probably tell I'm winging it and need some direction please.
Thanks
It can be tricky to get the combination of quoted strings and form fields right, as you need to be aware of which quotes are being used to concatenate the WhereCondition string together and which quotes are being presented to the query engine. You also need to know which fields are text and which are numeric, because text fields need to be enclosed in quotes in the resulting string, while numerics don't. I'm assuming below that cboBusiness, cboAsset and cboFacility are all text.
I suggest you create a separate variable to store your WhereCondition in:
Dim myWhereCondition As String
myWhereCondition = "[Date Entered] BETWEEN #" & Me.StartDate & "# AND #" & Me.EndDate
& "# AND BU = '" & Me.cboBusiness
& "' AND Asset = '" & Me.cboAsset
& "' AND Facility = '" & Me.cboFacility & "'"
DoCmd.OpenReport ...
WhereCondition:=myWhereCondition
You can then create a debug breakpoint on the "DoCmd" statement and check the value of "myWhereCondition" in the Immediate window, to make sure it is formed correctly, before DoCmd runs.
IIRC, you can use apostrophes/single quotes as an alternative to double quotes in MS Access, as I've done above. If this is not the case, then each of the single quotes above would need to be converted to "double double quotes" (because a double quote on its own would terminate the string).
The somewhat messier "double quotes everywhere" version of the WhereCondition would be:
myWhereCondition = "[Date Entered] BETWEEN #" & Me.StartDate & "# AND #" & Me.EndDate
& "# AND BU = """ & Me.cboBusiness
& """ AND Asset = """ & Me.cboAsset
& """ AND Facility = """ & Me.cboFacility & """"
Note that if any of the cbo fields are numeric, you need to remove the corresponding single (or double double) quotes from each side of that field.
Try this WhereCondition:
Dim WhereCondition As String
' First, adjust the WhereCondition:
WhereCondition = "([Date Entered] Between #" & Format(Me!StartDate.Value, "yyyy\/mm\/dd") & "# And #" & Format(Me!EndDate.Value, "yyyy\/mm\/dd") & "#) And BU = '" & Me.cboBusiness & "' And Asset = " & Me.cboAsset & " And Facility = " & Me.cboFacility & ""
' Then, open report:
DoCmd.OpenReport ReportName:="rptVerification_Results", View:=acViewPreview, WhereCondition:=WhereCondition

Can't get syntax correct for DCount with multiple criteria

My table is tbl_WCCSRQA, the relevant fields are CustomerServiceRep and Claim Date.
My form that passes the variables is called frm_CSRErrorTracking, and the relevant boxes are called CSRNameCB (a combo box), and StartDate EndDate (both are text boxes)
My report is called rpt_CSRErrorTracking and I want to set the DCount as a control source for a textbox called TotalClaims
I need a DCount that counts all instance of a CustomerServiceRep name between two dates. I'd like to pass the Name and Dates from my form. I'd like to set the DCount as a control source for the textbox on the report
Here is the DCount I have tried so far;
=DCount("CustomerServiceRep", "tbl_WCCSRQA", "CustomerServiceRep = '" & [Forms]![frm_CSRErrorTracking]![CSRNameCB] & "'" AND "ClaimDate = '" BETWEEN [FORMS]![frm_CSRErrorTracking]![StartDate] AND [FORMS]![frm_CSRErrorTracking]![EndDate]"'"
I've looked at this for the past 30 minutes and cannot figure out where I am going wrong. I continue to get the error message "You may have entered an operand without an operator".
You have a few double quotes out of sync - try the following:
=DCount("CustomerServiceRep", "tbl_WCCSRQA", "CustomerServiceRep = '" & [Forms]![frm_CSRErrorTracking]![CSRNameCB] & "' AND ClaimDate BETWEEN #" & Format([FORMS]![frm_CSRErrorTracking]![StartDate],"yyyy-mm-dd") & "# AND #" & Format([FORMS]![frm_CSRErrorTracking]![EndDate],"yyyy-mm-dd") & "#")

Run-time error 3075

I got this error message "Syntax error in query expression 'Select sum([BatteryDataTable].NumberOfBatter as)'"
Original sql statement below:
sqlDateRangeSearch = "Select *, **Select sum([BatteryDataTable].NumberOfBattery as " & Me.SumOfBattery & ")** from BattryDataTable" & _
" where BatteryDateChanged BETWEEN " & _
Format(Me.FromDTPicker.Value, "\#yyyy-m-d\#") & " and " & _
Format(Me.ToDTPicker.Value, "\#yyyy-m-d\#") & ";"
Me.RecordSource = sqlDateRangeSearch
What I am trying to do is to display all selected records, sum of values of all selected records, between selected dates. A selected records will display in a form, and the sum of values of all selected records, is displayed in textbox in the same form. BTW, somebody on here has helped me with this code. I was deeply appreciated. The part of the syntax in bold has caused the error. Thank you very much in advance.
When you create a write an SQL, you are not required to repeat statements such as SELECT, WHERE etc. You simply separate your selected staements with a comma as you have done already. Removing the second instance that you have mentioned SELECT will hopefully remove the error for you.
Many issues with your SQL.
as Dane wrote, you don't need to repeat a SELECT in this SQL
statement
the alias name you give for your sum column have to be included in
your SQL (within the double quotes) & should be outside of the closing bracket of the SUM
the ** you use before & after the SUM are irrelevant.
So here is your modified SQL Statement.
sqlDateRangeSearch = "Select *, sum(NumberOfBattery) as SumOfBattery from BattryDataTable" & _
" WHERE BatteryDateChanged BETWEEN " & _
Format(Me.FromDTPicker.Value, "\#yyyy-m-d\#") & " AND " & _
Format(Me.ToDTPicker.Value, "\#yyyy-m-d\#") & ";"
Me.RecordSource = sqlDateRangeSearch
Hope you find this helpful.

DCount will nor return 0 but returns Error instead

Test = DCount("*", "tblWorkNew", "GP = " & GPID & " And Month = #" & Month & "#")
This function gives correct results when the answer is >0. Whenever it's 0, I get #Error. I have tried putting my code within a Nz but that doesn't help either.
I tried to duplicate on a Northwind db what I believe you are trying to do. I used the Order List form. In the header of that form I created a textbox named txtMonth. The Control Source for txtMonth is =Month([Order Date]). Then I created another textbox in the header named txtMonthOrders with Control Source as =GetMonthSum(). Then I created a function in Modules called GetMonthSum(). This is the function: GetMonthSum = DCount("[Order ID]", "Order Summary", "Month([Order Date]) = '" & Forms![Order List]!txtMonth & "'") . This seems to work.