calling a query from a report textbox - ms-access

I have a report based on a number of different queries. Most of the queries use the value of a row's customerID textbox as a key to extract specific data from other fields.
Here is what I have for the Control Source property of one of the textboxes:
=DLookUp([Level],[qryLevel],[Me].[customerID])
Here is the SQL for qryLevel:
SELECT TOP 1 Level, myDate FROM sometable WHERE custID=Me.customerID ORDER BY myDate DESC
qryLevel works when tested independently, but the DLookUp function does not seem to be working properly because Access gives a dialog box asking for each parameter and then outputs #NAME? in the textbox when no values are input into the dialog boxes.
How can I get each of these textboxes to output its own result from a separate query?

DLookup function arguments must all be strings: http://allenbrowne.com/casu-07.html
So for the first two arguments, just enclose them in double-quotes.
For the last argument, the documentation says it's equivalent to a SQL where clause, without the word 'where'. Actually since you're returning only a single record from your query you probably don't need the last argument at all:
=DLookup("[Level]", "[qryLevel]")
Although, I don't see how qryLevel can work as an independent query since it refers to Me which implies a container object. Better to express as:
SELECT TOP 1 Level, myDate
FROM sometable
WHERE custID = [Forms]![MyForm]![customerID]
ORDER BY myDate DESC
... which will work in any context--inside or outside a form.

Related

Pass a Parameter in SSRS while removing Dashes

I am passing a unique ID as parameter in SSRS report. In the source table, unique id does not contain dashed. However, the user may insert Unique ID including dashes "-" and in some cases without dashes. Is there a way that we could remove dashes from the parameter.
For example, unique id 3120-20268-8 is stored in table as 3120202688. How I could retrieve if user pass multiple values with or without dashes in the SSRS Report.
When is used below query, it gives record against single value only. However, gives error when more than one values are provided.
select * from Table
where Unique_ID in (REPLACE(#Unique_ID,'-',''))
For more than 1 values, it gives errors mentioned below:
The replace function requires 3 argument(s).
Query execution failed for dataset 'ATL_List'.
Thanks
One of the simplest mechanisms for this is to create an expression based parameter to hold the sanitised input. This parameter would be hidden so the user is not aware of it, but the rest of the usage of the parameter is the same.
NOTE: You could do something similar with a query based default value, but this case is easier to do via a simple expression
Single Value Parameter
Create a new parameter:
set it to hidden
Set the default value expression:
=Str(Parameters!inputID.Value).Replace("-","")
Multi-Value Parameter
This is only slightly trickier, in the expression we can join the selected values together into a CSV string, then process that value and then split it back:
Set the parameter to multi-value, but still hidden:
Set the default value expression:
=Join(Parameters!inputID.Value,",").Replace("-","").Split(",")
Without going to detailed, if we made the sanitised parameter temporarily visible, just to demonstrate the conversion, it should look like this:
The parameter MUST be hidden!
NOTE: DO NOT make your sanitised parameter visible as in the above screenshot in your deployed report! Doing so will mean that it will not pickup changes made to the input value after it has rendered the first time.
remember that we have exploited the default value, we haven't arbitrarily defined en expression to always execute.
The output when the parameter is hidden is calculated when the report is rendered, it's just harder to visualise the behavior in this static post:
In your DataSet query you would just use the sanitised parameter:
SELECT * FROM Table WHERE Unique_ID IN (#sanitisedMultiValue)
You should be able to use the replace function in your report to format the parameter value after it has been entered, something like the below
replace(Fields!Paramater.Value,"-","")=FieldinYourTable

Sum Values not equal to a space from a Control Source in MS Access

As the subject expresses, I'm trying to sum the values of a string field where spaces may exist. It must be done this way, unfortunately.
The database is very old. The original developer chose to make all fields Text fields; to get over the null value problems, a function was written in VB6 to replace any null value with a space. This cannot be changed.
Fast forward to now, I'm trying to create a report that sums the length field without changing spaces to nulls first, and it should be done entirely through the control source property of the report.
I've added some of what I've tried below, but every time the report is run, I receive:
Data Type Mismatch
...and I'm not sure how to get around it.
Ideally, I'd like to keep the users out of the database completely, and just add a combo box that lists the reports created in the database so they can be opened by name without having to run any additional update queries first.
=Sum(IIf([MY_LEN]<>" ",DCount("[MY_LEN]","MY_TABLE"),0))
=Sum(Nz(Iif(Trim([MY_LEN])='',Null,[MY_LEN]),0))
=DSum("[MY_LEN]","[MY_TABLE]","[MY_LEN]<>' '")
=Sum(Iif(Val([MY_LEN])>0,[MY_LEN],0))
=(SELECT Sum([MY_LEN]) AS MyLen FROM MY_TABLE WHERE (((MY_TABLE.[MY_LEN])<>' ')))
Is this possible?
Can't compare anything to Null. Can't say If x = Null Then because Null is undefined. So you can't test if undefined = undefined. Use If IsNull(x) Then in VBA and Is Null in query criteria. Don't really need IIf() for Sum() aggregate, other aggregates such as Count or Avg would.
To handle possible space, empty string, or Null for a text field holding numeric data.
=Sum(Val([MY_LEN] & ""))

MS Access 2010 query using tempvar inside IN() condition

I am trying to filter a query using a temporary variable that is inside an IN() condition. The temporary variable contains a list of text values. I am using the macro-builder tool in Access 2010.
Assume I have a query qryMain that produces:
Field1 Field2
1 A
4 B
2 C
3 D
without a WHERE clause. Using the clause
WHERE [tblMain].[Field2] IN([TempVars]![tmpField2])
to filter the query, the desired results are
Field1 Field2
1 A
4 B
when tmpField2 is set to "A,B". I set tmpField2 using an on-click event in form frmMain using SetTempVar and Requery the subform/subreport object sfrmMain that is based on qryMain. This is done via the MS macro-builder, not VBA.
Unfortunately, requerying sfrmMain produces an empty table rather than the expected results. Note that if I set tmpField2 to "A", then the requery macro works as expected.
I have tried multiple variations of initializing tmpField2, based on Access's double quote requirements, but still no success. My question is similar to this as-yet unanswered question, but my question involves passing the temp variable inside an IN() statement within the WHERE clause, without using VBA.
The problem is not actually due to TempVars. If your value list came from a form's text box instead of TempVars, the result would be the same.
For what you're attempting to do, IN () requires a hard-coded list of values:
SELECT m.Field1, m.Field2
FROM tblMain AS m
WHERE m.Field2 IN ('A','B');
But, instead of a hard-coded list of values, you want to supply the list dynamically when the query is run:
WHERE m.Field2 IN (something_dynamic);
Unfortunately, Access will not cooperate. Whatever you supply for something_dynamic, Access will interpret it to be only one value ... not a list of values. And it doesn't matter what method you use to supply something_dynamic ... a TempVar, a text box, a formal query parameter, a custom VBA function which returns a string containing a list of values ... that list will be evaluated as only a single value.
If you were willing to use VBA, you could write the query at runtime to include a hard-coded value list before executing it. Since you want to avoid VBA, you can try something like this ...
WHERE InStr(1, [TempVars]![tmpField2], "'" & m.Field2 & "'") > 0
Note that approach requires quoting text values within tmpField2: 'A','B'
Also beware that approach could be painfully slow with a large table. Access would need to evaluate that InStr expression for every row in the table.

Date field filter is not working in Row Group level filter

I have added a Filter in Row Group-> Group Properties to perform sum of quantity only for those transactions which have done before a certain date.
Whenever I am selecting the 'Cdate' as Expression field from my dataset1, the type is showing as Date/Time but after saving it when I check,I found it as 'Text'. As a result the filter for 'cDate' is not working during report generation.
Note that, I can't filter the data in dataset side or tablix side as I have to show all the column items. This is a matrix report.
OK this is going to be a bit confusing because your dataset contains a field with the same name as one of the built-in expression functions ("CDate" - Convert to Date).
I sometimes run into these datatype issues when using filters and I find the best way to handle it is to force both the filter field and the filter value to be the same data type.
So in your case try setting the Filter expression to:
=CDate(Fields!CDate.Value)
then select the operator as "<=" and set the value using an expression as well:
=CDate(Parameters!MyParameter.value)
and see if that works.
I understand you so that your date field in the dataset is called CDate, try casting it as date, so instead of selecting it in your filter, type the following into the filter
=CDate(Fields!CDate.Value)

How to display a query record count in a form control

I have a query that returns a fluid # of records, depending on criteria selected in the form. I would like to display the total # of records returned to the form.
I have added a unbound text field to the footer in the form that is displaying the controls and resulting records. I tried the following expressions in the text field, both of which result in #error:
=Count([qrnname]![fieldtocount])
=DCount([qrnname]![fieldtocount])
This should be simple.
DCount requires string values for its arguments. Assuming fieldtocount is the name of a field returned by the named query qrnname, use this as your text box's Control Source ...
=DCount("[fieldtocount]", "qrnname")
Since that query depends on criteria selected in the form, Requery the text box whenever those criteria change to update the count displayed in the text box.
use this =DCount([fieldtocount]![qrnname])
The syntax for the DCount function is:
DCount ( expression, domain, [criteria] )
expression is the field that you use to count the number of records.
domain is the set of records. This can be a table or a query name.
criteria is optional. It is the WHERE clause to apply to the domain.
Dcount in detail
An other alternative is to use =Count(primaryKey) in the Control Source property
It seems better if you have some filter on your original query, so you don't have to apply them again in the DCount (expression, domain, [criteria]) function.
A quick method for counting Access records in a form