How to use Access 2013 Parameter Query within Query? - ms-access

In Access 2013 db, I've written two queries.
A Query having Parameter
SELECT [par], IIf(Len([par])>=4,Left(([par]),4),"") AS first_part, IIf(Len([par])>=6,Left(([par]),6),"") AS second_part, IIf(Len([par])>=8,Left(([par]),8),"") AS third_part;
A Query without Parameter
SELECT par_list.par, IIf(Len(par_list.par)>=4,Left((par_list.par),4),"") AS first_part, IIf(Len(par_list.par)>=6,Left((par_list.par),6),"") AS second_part, IIf(Len(par_list.par)>=8,Left((par_list.par),8),"") AS third_part
FROM par_list;
I want to rewrite the second query with the first query. How ?
I want to use the first query as function within other queries. How ?
Most Access parameter query call examples use VBA. CAN we call parameter query within another query, WITHOUT using VBA?

Do use VBA for this. That is also what it is intended for, and it will save you a lot of trouble when debugging.

Related

How do I use FIND_IN_SET() with a '?' parameter in SSRS Report Builder 3.0?

My SSRS Server is using an ODBC connection to pull data from a MySQL server. I have a query that's making use of the FIND_IN_SET method. My requirements state that the report should provide options for a start and end date parameter and a comma delimited list of "usernames". The start and end date parameters work fine so I've left them out of my sample code for brevity.
When I run the query in MySQL Workbench, I get results. When I run the query in the query designer of Report Builder 3.0, I get results, IF AND ONLY IF, I hard code the value of the parameter being used for the FIND_IN_SET() method. When I try to use a '?' parameter for the value, and paste in the exact same value as when hard coded, I get no results.
This query works in Workbench and the Report Builder's Query Designer:
select pwk.username,
sum(case when pwk.event = '/some-value/' then pwk.count else 0 end)
as order_detail_count
from table pwk
where find_in_set(pwk.username, 'foosername#test.net,foo2#test.com,username#test.org')
This same query returns nothing when I use the '?' parameter and copy/paste the above value when running the query:
select pwk.username,
sum(case when pwk.event = '/some-value/' then pwk.count else 0 end)
as order_detail_count
from table pwk
where find_in_set(pwk.username, ?)
It seems as though the Report Builder is not using the text parameter the way I expect it to but I have no way to determine how to use it "correctly". Has anyone ever run into this problem?
I would expect the results to be identical but that's not the case. Is there a way to use FIND_IN_SET with a text parameter? Or is there some other way to accomplish this? I'm perfectly okay with using something other than FIND_IN_SET if it will work. I tried using IN() but that didn't even work in WorkBench. My users just need to be able to enter multiple usernames as a parameter.
If it's relevant, I'm using a Tablix to display the data.

SSRS 2008 :Is it possible to specify an entire where clause within parameter?

I have a table that I am building a report for. There are about 10 fields, but 4 of those fields may contain value(s) that users would want to filter the report by.
I was thinking I could create a parameter with a list of label/value pairs and the value portion of the parameter item would be an actual where clause for the underlying dataset like:
#filter
label/value
exceptions/where error_field like '%exception%'
counts/where count_field > 100
2016/where year_field = 2016
I tried dataset:
select error_field, count_field, year_field from mytable
#filter
I also tried(leaving where out of parameter value):
select error_field, count_field, year_field from mytable
where #filter
Both dataset queries failed to save. I am thinking I could include all the varying where clauses inside the dataset query statement, but it may require different parameters but how can they be empty unless I used 1=1 as default value. I only wanted to use a single parameter tho.
Any other ideas?
Thank you.
Your dataset query needs to be valid SQL. You can use an expression for the dataset, and work with the SSRS expression language to generate the SQL you need.
Something like:
="SELECT * FROM TABLE " + IIF(Value = True," WHERE 'A' = 'B'","")
However, I think they are a pain to work with. They are harder to understand, and take longer to maintain, and much easier to contain a mistake.
I find it easer and safer just to pass the parameters to a SQL Stored Procedure on the server, especially if you are using a TEXT BOX entry field.

Multiple datasets Count with IIF in SSRS

I am trying to write an expression in SSRS which counts only specific data using IIF. I found the following solution:
=Sum(IIF(Fields!Program.Value = "FC", Fields!QuantityToShip.Value, 0))
The code above works but only when there is ONE dataset, while I have several.
Here is the code I wrote:
=Count(IIF(Fields!Mgroup.Value,"DataSet1"=303,1,0))
I get the aggregation error:
Textbox refers directly to the field ‘Mgroup’ without specifying a dataset aggregate
I added a sum:
=Count(IIF(Sum(Fields!Mgroup.Value,"DataSet1")=303,1,0))
Still getting the same error.
Why is that?
What can I put instead of Sum? All I need is to count how many groups named 303 I have.
The expression you used has some sintax errors. The Count function only aggregate from the scoped Dataset.
Try this:
=LookupSet(303,Fields!Mgroup.Value,Fields!Mgroup.Value,"DataSet1").Length
Let me know if this helps you.

How to use Excel Object Model in Access Query Expression?

I am moderate to advanced user of Microsoft Excel, but very new to Microsoft Access (2010 version). What I am trying to do is use Excel functions in an Access Query Expression. So far, I've researched how to set Access to reference the Microsoft Object Model by going to Create/Module/Tools/References and then selecting the Microsoft Excel 15.0 Object Library.
From there, I went to my Query (Design View) and attempted to add an expression in the Field row to calculate the distance between two points. As a test, I typed:
Distance: Excel.WorksheetFunction.ACOS(50)
I thought this would work, but once I closed, saved the Query, and reran the Query I received the following error:
Undefined function 'Excel.WorksheetFunction.ACOS' in expression
I've done some Googling to determine why this isn't working, but have been unsuccessful. I'm not sure if Access allows you to reference Excel directly from the expression. Or, perhaps my syntax is incorrect.
Write a simple function in VBA (in Access) that calls the Excel function you want to use:
public function myTestFunction(x as double) as double
myTestFunction = Excel.WorksheetFunction.ACOS(50)
end function
Use this function in your query:
If you use the query design grid:
Simply write the function in a column and put the column name of the column that holds the input value as the argument. If you want to put an alias to the column, write the alias before and use :; something like this: columnAlias: myTestFunction([OtherColumn])
If you're writing your query using SQL:
Write your query as usual; use the function like any other function available in SQL:
select [OtherColumn], myTestFunction([OtherColumn]) as function
from [YourTable]

Pass IIF Expression's Results to Another Query's Criteria

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.