SQL MS Access Query to Get Text Value from Form Text Box - ms-access

I have a 'very' long query written through MS Access with lot of unions (around 30) and other dynamic variables.
Now, I need to insert a 'Where' clause in all those unions, and the where clause would be constant and generated through a form (selecting some options, I have written a vb code to create a string containing the where clause.
Is there any way I could use some variables in the SQL query so that the variable gets the where clause from the text result of the form action?
Illustrative: My Query
Select a,b,c,d from
(select x as a,b,c,d from abc <where clause> union
select y as a,b,c,d from abc <where clause> union
select z as a,b,c,d from abc <where clause> union
select p as a,b,c,d from abc <where clause>)
order by a
The should be populated with the text box result which would look something like 'where b=1'
I thought of writing it in the VB code itself, but to numerous linebreaks and chaning nature of the queries, I do not want to manually keep adding " & _ at the end of each line.
Any help would be greatly appreciated.
Regards,
Navs

You can write the WHERE clause like this:
WHERE B=[Forms]![FormName]![FieldName]
so it gets the value from the form. But, if i understand it correctly, in the form there's not just the condition, but there's the whole where clause, that could be 'where b=1' but it could also be 'where c=4' or 'where a=2 and b=6'?
I think there's not a perfect solution to this, you can't use plain Access but you have to write some VBA code, and you can generate the query string with something like this:
$src_query = "Select a,b,c from abc <where clause> union select ..."
$src_query = Replace($src_query,
"<where clause>",
[Forms]![FormName]![WhereClause])
then you just have to modify the query programmatically:
CurrentDb.QueryDefs("QueryName").SQL = $src_query
and now your query is ready to be executed.

You could put the Where clauses in a separate query, and not alter the original
Select *
from MyQuery
Where (a=1 and b=7) or (c=3 and d="StackOverflow")

With little homework, I found how to assign the contents of the text file to a variable. I was then able to change the variable with the code snippets 'Fthiella' had given.

Related

How to convert IF or nested If statement like EXCEL to dynamic SQL Query in SQL server

I have came across one pretty problem in which I need to convert the nested if statement (like we used to write in excel) to dynamic SQL query.
I want to convert this
select 'IIF(A>B,A,IIF(B>C,B,C))'
to like this
select 'CASE WHEN A>B THEN A ELSE CASE WHEN B > C THEN B ELSE C END END'.
Why I am doing this I have formula column in the table user can insert formula in that and I need to convert this to SQL statement.
Do anyone has idea how can I do that?
The above is for only example I will be having 150 rows for different formula's entered by user.
Note : Here do not interpret IIF as SQL server IIF, it can be IF.
Assuming that A,B,C are numbers we can do something like this
SELECT
(
Select MAX(cols)
from
(VALUES (ColA), (ColB), (ColC))tbl(cols)
) as greatest
from yourtable

IIF When Using Similar Criteria

Their are 2 different jobstatuses that I want to display the same way, my IIF statement changes the display, BUT I would like for only one entry to be returned for both. For example, I want it to show like this
JobStatus
---------
Inactive
Let Go
But the returned result set I get is
JobStatus
---------
Active
Inactive
Let Go
Let Go
What do I need to change in my query to make it show like I need? (and I need this to be done via a query not vba)
jobstatus1: IIf([jobstatus]="Terminated","Let Go",IIf([jobstatus]="Fired","Let Go",[jobstatus]))
EDIT -- Full Syntax. The IIF statement I need to use is for the alpha.jobstatus line right after the Select
SELECT alpha.jobstatus, Count(beta.ID) AS CountOfID
FROM alpha LEFT JOIN beta ON alpha.jobstatus = beta.jobstatus
GROUP BY alpha.jobstatus, alpha.order
HAVING (((alpha.jobstatus) Not In (Select [jobstatus] From tbl_Valid)))
ORDER BY alpha.order;
Use SELECT DISTINCT to return only distinct values.
Also you can use a simpler expression for that calculated field --- one with a single IIf instead of nested IIf's. Switch your query to SQL View and revise the start of the statement text to this ...
SELECT DISTINCT IIf([jobstatus] IN ('Terminated', 'Fired'), 'Let Go', [jobstatus]) AS jobstatus1

Running a SQL SELECT statement against a MYSQL column of SET type

I'm trying to run a SQL SELECT statement against a column that is of type SET. The table is called myTable and the columns in myTable are called base_props and names. The base_props column is of type SET. The values in base_prop are vb,nt, cnt,poss and loc. So I would like to SELECT entries from the column 'name' where base_props have both the values, vb and poss. The results I'm looking to get may have values other than just vb and poss. So to be clear I would like to select all entries that have the values vb and poss regardless if they have other values as well. I've tried the following SQL queries but I can't get the desired results.
SELECT name from myTable WHERE base_props = 'vb' AND base_props = 'poss'
That query returns an empty result set. I've tried using FIND_IN_SET() and IN() but I couldn't get anywhere with that. I've written SQL statements before but never had to deal with columns that are type SET. Any help is appreciated.
The only thing I can come up with is using the LIKE keyword:
SELECT name FROM myTable WHERE (base_props LIKE '%vb%' AND base_props LIKE '%poss%');
This will make sure both vb and cnt are in the base_props column. Of course you can use cnt, nt and loc in there, or any number of base_props values in the sql, just add more AND statements.
OR as a deleted answer by samitha pointed out, you can use FIND_IN_SET:
SELECT name from myTable WHERE FIND_IN_SET('vb', base_props) AND FIND_IN_SET('poss', base_props);
Comment (by spencer7593): "both of these work, but there is a slight difference. The LIKE operator will actually match any member that includes the search string anywhere in a term; the FIND_IN_SET function will only match an exact member. It's also possible to search for members in set by the order they appear in the SET definition, using the MySQL BITAND operator: for example, to match the 1st and 4th members of the set: WHERE base_props & 1 AND base_props & 8". So for example, if you have 'a' and 'aaa' in your set, then using the LIKE "%a%" method will also return rows containing 'aaa'.
Conclusion: use the FIND_IN_SET solution since it will work for all cases.
FIND_IN_SET return index, Try this
SELECT name from myTable WHERE FIND_IN_SET(base_props, 'vb') > 0 AND
FIND_IN_SET(base_props, 'poss') > 0

Can i execute query in iif function

I want to know can i run a query in iif function used in ms access database. My case
Select field1,(iif(3<4,'Select * from tbl1','select * from tbl2')) from tblmain
I am facing syntax error when i try to executed query like that whats the problem
What you're trying to achieve isn't clear from your sample query.
You can use IIF functions in Access queries, for example:
SELECT IIF([SomeField]<15, "Smaller than 15", "Greater than!") As Whatever
FROM myTable
You can use subselects in Access as well, for example (example shamelessly stolen from http://allenbrowne.com/subquery-01.html):
SELECT MeterReading.ID, MeterReading.ReadDate, MeterReading.MeterValue,
(SELECT TOP 1 Dupe.MeterValue
FROM MeterReading AS Dupe
WHERE Dupe.AddressID = MeterReading.AddressID
AND Dupe.ReadDate < MeterReading.ReadDate
ORDER BY Dupe.ReadDate DESC, Dupe.ID) AS PriorValue
FROM MeterReading;
Note that the specified subselect query must be guaranteed to return a single record - either by specifying TOP 1 or using an aggregate function - and must link back to the parent query in the WHERE clause.
You can't use an IIF statement the way you're trying to in your question, however, even if your subselect was valid, which it is not.
Two options to suggest, although it is less than clear to me what you're trying to achieve here. First, you might want to consider doing it in VBA instead. Something like:
const query1 As String = "Select * from tbl1"
const query2 As String = "select * from tbl2"
Dim recset as DAO.Recordset
set recset = CurrentDB.OpenRecordset(iif(3<4, query1, query2))
Alternatively, if both tbl1 and tbl2 had the same fields you could do something like this:
SELECT * FROM tbl1 WHERE 3<4
UNION ALL
SELECT * FROM tbl2 WHERE NOT (3<4)
If you replace 3<4 by whatever actual condition you're checking for, you'll only get back records from one or the other or query, never both. However, my suspicion is that if you need to do this, your database may have design issues - I can think of many questionable scenarios where this would be needed, and few valid ones, although I'm sure they exist.

MySQL select with subquery having replace

So I have a data with format like ;1;;2; and then I need to use this number in a query so I thought I'd convert it to 1,2 and use that in a IN condition. In my table, the result should return 2 rows but instead it is returning only 1 row.
My query is like this. The subquery return 1,2 with no problem but only 1 row is retrieve.
select *
from wt_lists
where id IN ((select replace (replace(sendto, ';;',','),';','')
from wt_stats where statsid IN (1)))
But when I try it with this. It returns the correct result, which in my case is 2 rows.
select *
from wt_lists
where id IN (1,2)
What am I missing here?
Comma delimited strings need to be explicitly defined in the query in order to be used in the IN clause - there's countless examples on SO where people need to use dynamic SQL to incorporate user submitted comma delimited strings.
That said, I have a solution using the FIND_IN_SET function:
SELECT DISTINCT wl.*
FROM WT_LISTS wl
JOIN (SELECT REPLACE(REPLACE(ws.sendto, ';;',','),';','') AS ids
FROM WT_STATS ws
WHERE ws.statsid = 1) x ON FIND_IN_SET(wl.id, x.ids) > 0
You are replacing the string:
';1;;2;'
To:
'1,2'
So, you SQL query looks like:
select * from wt_lists where id IN ('1,2') from wt_stats where statsid IN (1)
To use IN clause you need select different values in different rows.
I found this store procedure that does exactly what you need.
http://kedar.nitty-witty.com/blog/mysql-stored-procedure-split-delimited-string-into-rows/
I have not tested, but it is the way.
Obs: Like David said in the comments above, parsing the data in your application is a better way to do this.