how to get query criteria in access vba - sql

Now I am trying to create customize query design view form with Access VBA. I want to show fields name and criteria of saved query.
For example, SQL statement that executed by QueryDef.sql is
SELECT AllCars.ID, AllCars.CarName
FROM AllCars
WHERE (((AllCars.ID) Between 15 And 25) AND ((AllCars.MinPay)<1000)) ;
I would like to get criteria expressions( ID = Between 15 And 25, MinPay = <1000) and field names(ID , CarName) from it.
I can get field name but I am not sure how to get query criteria expression in Access VBA.
I tried to split sql statement but I think this is not a best way for multiple criteria expressions.
If getting criteria in access vba may be possible, could someone help me?
Thanks in advance.

Change the code that creates the saved query: instead of saving all the SQL together, save the field names separately and the expressions separately.
To keep all the fields together but in a way that you can later separate them, put the character ";" between the field names, and to separate them you will use the split function.
You can do the same for the expressions, and when you read the saved data you associate the position in the array of the expression with the position of the field in the array and thus you connect them together to show the user which expression applies to which field.
In the same way you build the query

Related

convert an option value stored in a table field (without a reference table) into a corresponding text

We have a legacy vb6 solution working with an access database with one of the forms containing a number of mutually exclusive option buttons
There is no reference table and the options are saved/loaded using hard coding ie. values in the field optState would be either 0, 1 or 2.
We are building a query in Access to export data into XML and looking for a way to convert the options into text fields without updating the DB or VB application!. So if 0 show some text, 1 some other text ... etc.
Is there a way we can do this in the access query or access sql?
No plans to upgrade the VB or DB so looking for a workaround.
thanks
jay
You could use IIf (Immediate If) to do this in a query. As you have multiple values per field, you would need to nest them. Something like:
SELECT tblStatus.*,
IIf([Status]=0,"Available",IIf([Status]=1,"Sold",IIf([Status]=2,"Withdrawn",""))) AS StatusOut
FROM tblStatus;
This returns an empty string if a value is not 0/1/2. Or you could use Switch in the query:
SELECT tblStatus.*,
Switch([Status]=0,"Available",[Status]=1,"Sold",[Status]=2,"Withdrawn") AS StatusOut
FROM tblStatus;
Regards,

Dynamic Parameter in Power Pivot Query

We are using Excel 2013 and Power Pivot to build modules that consist of several Pivot tables that are all pulling data from the same Power Pivot table, which queries our T-SQL data warehouse.
In an effort to simplify and fully automate this module, we wanted to create a text field that would allow a user to enter a value (a client ID# for example), and then have that value be used as a parameter in the Power Pivot query.
Is it possible to pass a Parameter in the Power Pivot query, which is housed in a text field outside of the query?
You can also pass a slicer or combobox selection to a cell. Define a name for that cell. Put that cell (and others if you have multiple text variables to use) in a table. For convenience, I usually name this table "Parameters". You can then 'read in' the parameters to your query and drop them in your query statements.
The code at the top of your query to read these parameters in might look like...
let
Parameter_Table = Excel.CurrentWorkbook(){[Name="Parameter"]}[Content],
XXX_Value = Parameter_Table{1}[Value],
YYY_Value = Parameter_Table{2}[Value],
ZZZ_Value = Parameter_Table{3}[Value],
Followed by your query wherein instead of searching for, say a manually typed in customer called "BigDataCo", you would replace "BigDataCo" with XXX_Value.
Refreshing the link each time a different customer is selected will indeed be a very slow approach, but this has worked for me.
Rather than pass a parameter to the data source SQL query, why not utilize a pivot table filter or slicer to do allow the users to dynamically filter the data? This is much faster than refreshing the data from the source.
If for some reason you need to pass this directly to the source query, you'll have to do some VBA work.

Create SQL String in Microsoft Access Dynamically

I apologize if this is an easy one, but I can't find this on the web anywhere!
I have a list of tables and queries, and a full list of fields from each table and query.
I want to choose my fields in an Access form, and then, on another form, choose a query/table in one column (along with a field) and join it to a table/query in another column (along with the field.) My form for the joins would look like this:
Object1 Field1 Object2 Field2
and so on. I would want to be able to choose my fields through combo boxes, THEN make the SQL string dynamically. I can't use a where clause -- it has to be a join.
The problem is, the structure will change every time. Access gets funny about putting in parentheses. Also, if an object is chosen more than once, Access will want to join it (assuming I do it right) in a different manner than T-SQL.
Is there a way to write a query in T-SQL, and quickly convert to Access? Does anyone know the FROM clause algorithm Access uses to construct the FROM clause? I am stuck here, and I cannot find a solution to save my soul! Thank you in advance, David

Pulling field names from SQL query

Is there a way to retrieve the field names from an actual query, similar to how you can retrieve field names from a table using the INFORMATION_SCHEMA? In essence, I'm wanting to accept an entire query as a parameter, and be able to build an empty table with the field names from said query.
Right now, I'm using a STUFF() function to replace the SELECT with SELECT TOP 1 and replacing the FROM with a INTO tablename FROM, and then truncating tablename. It works, but I need this to be able to handle complex queries where the first occurrence of FROM might not be the one I need (in case the user is using a subquery for a field, for example).

Calculating the Sum of values in ms-access

I have created a query to calculate the sum of all of the profit values in a table, I tried to output this to a textbox on the main form of my database and I just the error #NAME?.
Has anyone tried this before and are there any major things I am missing?
We would need to see some code/design details to understand why your text box gets that #Name error.
Without those details, I'll just suggest you consider a DSum() expression, instead of a query, to load the text box. And DSum() is kind of like a SELECT query, but returns only a single value instead of a result set.
DSum("YourNumericField", "YourTable")
Examine the DSum online help topic for more details. You might find the optional Criteria parameter useful (like a WHERE clause in a SELECT statement) if you ever want to sum only a subset of rows from your table.
DSum("YourNumericField", "YourTable", "account_status = 'ACTIVE'")
If you have create query to calculate the sum called "querySum". On the form property sheet, make sure selection type you change to FORM, go to Data and select "querySum" as record source.
And then, click text box, go to the property sheet and choice Data > Control Source. So you can choice column from query to the text box.
Otherwise, if you want to use VBA. You can do like this
DSum("NumericField", "YourTable")
Or with condition
DSum("NumericField", "YourTable", "type = 'Payment'")