Pass IIF Expression's Results to Another Query's Criteria - ms-access

I'm trying to pass the result of a select query to another query's criteria. Where I'm struggling is passing a complex value that contains an operator. I'm not even sure it's possible to pass an operator and have the next query recognize it. I've tried similar to the code snippet below to no avail. I can't seem to get the third result to work ("A or B"). Essentially the select query is a parameters query and based on a user's specifications grabs the values to be used in another query that displays results on a form.
IIf([field1]="1","A",IIf([field1]="2","B",IIf([field1]="3","A or B","Like *")))

Ended up using an iif in the criteria of the second query which solved the problem.
Like IIf([Q1]![F1]=1,1) Or Like IIf([Q1]![F1]=2,2) Or Like IIf([Q1]![F1]=12,Right([Q1]![F1],1)) Or Like IIf([Q1]![F1]=12,Left([Q1]![F1],1)) Or Like IIf([Q1]![F1]=123,"*")
Essentially if the value was '12', it would parse the result using right and left into '1 Or 2' as the criteria with a proper operator allowing for both values to be returned in the query.

Related

SSRS Multi-Value Parameter - When Select ALL, returns zero rows

My current definitions for this report are like this:
DataSet A = a pretty extensive query with 'AND ai.Channel IN (#ChannelParameter)'
DataSet B = Channel 'Select distinct channel from Account_Info'
result set 'Retail Channel' or 'Wholesale'
Parameter = #ChannelParameter is set to Allow Multiple Values and is Getting the values from Query.
Tablix Properties - Filters
Expression [Channel]
Operator IN
Value =Parameters!ChannelParameter.Value(0)
When I run the report and select 'Retail Channel', I get the correct data.
When I run the report and select 'Wholesale', I get the correct data.
When I run the report and select both values, I get zero rows returned.
When I modify the query for DataSet A to be ai.Channel IN ('Retail Channel','Wholesale'), I get all of the rows. There are no rows in the data where the Channel field is NULL.
I've seen and tried some changes to the expression in the parameter using a JOIN statement, but no better results there.
What am I missing?
You filter expression is incorrect, as you stated (0) that means only the first parameter value will be used.
Having said that, it looks like you are already filtering the data in your A dataset so there is no need for the tablix filter, just remove it.

MS Access query based on filtered query

I'm trying to use a query (query2) based on another query (query1).
On a form where both are displayed, I use VBA to add filters for query1. This works for query1, but query2 keeps using the unfiltered query1 as its source no matter what I try. Any suggestions welcome
Many thanks
Two ways to approach this:
Approach 1: in Query2 set Filter on Load to 'Yes', then have your VBA add the filter clause to Query2 and re-run it. So, if you want to filter Query1 based on column [foo] having the value "bar", your VBA would add this to the Filter property of Query2:
Query1.[foo] = "bar"
Approach 2: Parameterize Query 1 - have it use a WHERE clause that points to a control on the form (perhaps a hidden text control if you don't want users to see it). The structure of your VBA would then:
1. change the value of the hidden control
2. Requery Query 1, which will now use the new parameter value
3. Requery Query 2, which will be based on the values of Query 1 (which points to the hidden control).

How to apply multiparameter to ssrs report filter

I have a master that can be filtered using 4 different parameters. I used a iif statement to join all the parameters to filter the report.
The problem I am now having is when more than one paramater is selected, it tends to return values for the first parameter rather than for all
My paramter expression is as follows:
expression
iif(IsNothing(Parameters!Div.Value)=0,Parameters!Div.Value
,iif(isnothing(Parameters!St.Value)=0,Parameters!St.Value
,iif(isnothing(Parameters!Sp.Value)=0,Parameters!Sp.Value
,Parameters!Hc.Value)))
values
=iif(IsNothing(Parameters!Div.Value)=0,Parameters!Div.Value
,iif(isnothing(Parameters!St.Value)=0,Parameters!St.Value
,iif(isnothing(Parameters!Sp.Value)=0,Parameters!Sp.Value
,Parameters!Hc.Value)))
Any help will be helpful
I think what you are trying to do is something like this:
=IIF(NOT ISNOTHING(Parameters!Div.Value), Parameters!Div.Value,
IIF(NOT ISNOTHING(Parameters!St.Value), Parameters!St.Value,
IIF(NOT ISNOTHING(Parameters!Sp.Value), Parameters!Sp.Value,
Parameters!Hc.Value)))
Do you only want to check for one value?
I usually check each parameter separately so it uses all of them at once. Though there may be a situation where your theory is what you want.
If you want to evaluate all the parameters, just add them to the FILTER of the dataset, table, or group. Choose your field in the Expression and the Parameter in the Value.

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.

calling a query from a report textbox

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.