How does Access's query editor decide whether to discard my formatting? - sql

Like a lot of developers who are comfortable with SQL syntax I get frustrated when working with Access's query editor. I'm talking about the raw SQL Syntax view, obviously.
One of its many annoying properties is that upon saving it will discard my layout / formatting. When reopening the query all I see is a bunch of unformatted SQL.
However, if my syntax is long and/or complex enough I've noticed that Access will retain my formatting and layout and, oh joy, the query remains clear and readable. I'm looking at an example right now with a page of SQL containing couple of UNIONs all nicely laid out from a few days ago.
At what point does Access flip over to allowing the user to retain his own formatting? Is it length? Complexity? And is there maybe even a trivial structural edit (if trivial structural isn't an oxymoron) I can make to all my queries which will force Access to leave my layout in place?

There are certain things that Access' query editor is not able to display in design mode.
Queries with UNION are the only thing that come to my mind right now, but there are probably more.
In my experience, Access always changes the layout as long as it's able to display the query in design mode.
As soon as you put something in the query that Access can not display in design mode (like UNION), Access leaves your layout and formatting as it is.

I couldn't figure out why Access kept changing my format in a union query (but not for every query or table included).
I simply created another SELECT query based upon the Union query and corrected everything in design view. It's a lot easier.
When I created the SELET query based upon the UNION query, I included tables or queries that I used as lookup tables and had formatted a field to select the second column from a record in a lookup field that the ubion query had anoyingly converted back to the first field in the selected record (usually the ID No of the record).
For example, I might lookup the account name in a record in the cash disbursements table that should display "Office Supplies Exp" but the Union Query converts at least one of the queries or tables I have combined in the union query to the Account Number, the first record in the lookup table, which was originally hidden in the lookup field.

Just to add to Christian's answer, I've done some more testing and find that UNION and DDL queries are left alone by Access.
If we add Pass through queries to that list, then that would match the queries deemed SQL Specific on the menu:
So, those would seem to be the three special cases.

Before saving just type the word union before the ;.
After opening Access next time, remove the word union and start working. When you want to save, first type union again.

Related

Choose AS400 query records directly from Excel

I've been searching the internet for hours trying to figure out if the following is even possible:
To choose the AS400 query records directly from Excel.
I haven't found any solution or description of how this could be achieved, which makes me guess that it's simply not possible. However, I haven't seen anyone confirm that it is impossible.
So my question is: Is this possible? And if it is, could you point me in the right direction in order for me to start learning how to do it?
I know its possible to run a query from Excel, and then adding parameters via SQL statements, but in my case, this presents several problems that could be avoided by choosing the records before the query is executed.
Example:
I have a query with a column (lets call it ColVal) that can hold the values 1 and/or 2. In the AS400 program under the menu "Work with queries" and then "Choose records" I can specify which records the query should contain when it has run based on the value in ColVal. This means i can get three different situations (A, B and C) when i run the query:
A) The query only contains records where the value in ColVal is 1
B) The query only contains records where the value in ColVal is 2
C) The query contains records where the value in ColVal is either 1 or 2
The goal is to be able to choose which situation I want from Excel in order to circumvent opening and using the AS400 program.
However, using situation C and then editing the query in Excel with an SQL statement to mimic situation A or B is not an option, as this means the query still contains undesired records.
This whole thing boils down to the following: Is it even possible to run the query from Excel essentially changing the data it contains and not just outputting it to excel? If this is possible, is it then possible to pass a parameter to the AS400 system and use it to create situation A, B or C?
I hope this example makes sense.
Edit - New example
Say i have different customers A and B. I can open the AS400 program and run a query in which i have specified that I only want data on customer A. I can then open Excel and use filters (as Hambone described) on the query to determine which records I want to output. However, if I want to work with data from customer B, I have to open the AS400 again and run the query with different parameters. I would like to be able to "change" my dataset from customer A to B from Excel, without having to include both in my recordset and then filter out one of them.
I imagined this is doable if you could pass a parameter to the AS400. The AS400 then runs the query using this parameter as the criteria for which records should be stored in the query. This means that if the parameter is Customer B, then there is no way to acces data from customer A, without running the query through AS400 again.
Any ideas are greatly appreciated :)
Follow up to my comment, here is a quick primer on how to run an ODBC query directly in MS Excel using Microsoft Query. This is very different than Power Query, which you referenced, in that MS Query is standard with Excel -- it's not a plug-in. This is relevant because it means everyone has it. If you are deploying a solution to others, that's an important consideration.
To start an MS Query in Excel, go to the data tab, select "From Other Sources" -> "Microsoft Query."
A list of your ODBC connections will come up. Pick the one that you want and select "OK."
It may or may not ask you for a login (depending on which ODBC connection you use and how its configured).
The next part is important. MS Query is going to try to have you use its builder to create the query. If you have the SQL, skip this part. It's horrible. Click "Cancel" on the query wizard, and then click the "SQL" button to enter your own SQL. If you can, make sure the result set is small (like use where 1 = 2 in the query).
When MS Query returns results, click the button next to the SQL Button to have it return the results to the spreadsheet. It looks like a little door.
From here, any time you want to refresh the query, you can simply right-click the data table in Excel and select "refresh." Alternatively you can go to the data tab on the ribbon and select "Refresh."
By the way if you have linked pivot tables and charts, the "Refresh All" option will refresh those as well, in the correct order.
To edit your query at any time, right-click on the table in Excel, go to Table-External Data Properties:
Then Click on the Connection Properties icon (highlighted below)
Click on the second tab (Definition) and edit the SQL Directly.
Parameters can be declared simply by inserting a bare "?" in place of your literal.
In other words, if your query looks like this:
select *
from users
where user_id = 'hambone'
Just change it to:
select *
from users
where user_id = ?
Excel will prompt you for a user id before it runs the query. From here, you also have the option of putting the parameter value in a cell within the spreadsheet and having the query read it from there. You'll see these when you right-click the table and go to the "Parameters" menu option.
Let me know if this helps or is unclear.
-- EDIT 7/23/2018 --
To follow up on your latest edit, it is possible to handle the scenario you describe, where you want to be able to filter on a value, or if none is given, then not have a filter. You see this a lot when you present multiple filter options to the user and you want a blank to mean "no filter," which is obviously counter to the way SQL works.
However, you can hack SQL to still make it work:
select * from activities
where
(activity = ? or ? is null) and
(energy = ? or ? is null)
In this example you have to declare four parameters instead of two, two for each.
You might also have to play with datatypes, depending on the RDBMS (for example for numerics you might have to say ? = 0 instead of ? is null or even ? = '' for text).
Here is a working example where a single filter was applied on the query above and you can clearly see the second one did not have an impact.
Yes it's possible. You need to use an ODBC driver to connect to the AS400 and retrieve the data. The driver and documentation are Here

RSA - Archer - Hide multiple fields in a search result

When performing a search in Archer, the result contains some un-necessary fields. Is there a way to show only fields that belong to Application "General information" ? In other words, there are some fields that are suggested to being added to "General Information" tab which appear in a search result.
I know I can disable these fields by selecting the field, then clicking on "modify field properties", "options" and untick the show field in a search result.
Since I have multiple applications and a lot of fields, doing this will take a lot of time. Is there any script or trick to hide all these fields in a search result?
You could always use the API and create your own search XML to contain only the fields you want to see.
But short of doing that, I think you've hit the nail on the head. You'll need to go into each application and then modify each of the fields you don't want shown. I don't think it's a waste of time, as you're surely not changing this search result every time... it's a one time deal and if it makes it easier for you or the end users, it's certainly worth it IMO.
If you have access to the database then you can modify table "fielddef" and make fields not searchable by default. You can join "fielddef" table with "level" and "moduletranslation" and target only specific modules this way.
I don't have the SQL code for this since I didn't have to make a bulk update to the fields and make them not searchable.
It should take 5 min to join these 3 tables and make an update if you are good with SQL.

How can I search the entire SQL database for a specific column name, and then search only those columns for a specific value?

At work, we use an absolutely massive database with multiple different programs writing to the main bucket. Unfortunately, there's not always a good way to know from those individual programs where specific data points might be written to the database. I have a query that can search everything for a specific value or string, but because of the size of the database, it usually takes about 7 hours to complete.
I thought that if I could narrow the search a bit, I might be able to speed that up. A lot of the values I often have to look up would be under similarly named columns (i.e. "UserName", "UserID", etc.) Is there a good way to do a search that looks through all of my tables for a specific column name, and then searches only those columns for the value that I'm trying to locate?
I found a similar question asked a few years back
Search sql database for a column name, then search for a value within the retuned columns
but when I run the answer, all it returns is "Command(s) completed successfully."
I'm still a little green at this, so I'm not discounting the fact that it could be a PICNIC issue on my end.

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

Searching in PL/SQL /Oracle Forms

This is with respect to search of a text in a table
Table_Name:
Details
Columns:
Fname,Mname,Lname,NName
This table contains nearly one lakh records
We are using Oracle forms for some querying option
The user input one name the form searches the table for the name and based on the name either(Fname/Mname/Lname/NName) in which column its is present further actions are proceeeded.
The search is taking a long time since we have huge amount of data present in the table.
I tried with Functional indexes for the table but t did not work its also taking more time
Later i tried with something like this
concatenated all the names into one name and put it into a cursor.
Using the cursor output i tried with Instring but it is hanging
I also tried with searching for building a dynamic cursor but it did not work.
My database is Oracle
Can u help me to out to find an effective solution or please help me if i have missed something.
Thanks
First of all, 1 lakh (100,000) records is not in itself a large table.
The problem I can see is the query appears to be doing an OR against the Fname/Mname/Lname/NName columns.
This means the query will be doing at least one full-table scan to obtain the results.
You may wish to use debug to obtain the query it is firing against the database and attempt to tune this at the SQL prompt using auto trace.
You may need to clarify if the search is also doing something like a LIKE against these columns rather than an EQUALS. As a LIKE will impact the query further and affect indexes.
Certainly the use of INSTR will disable indexes on your searched column.
It is not clear if what your block is based on ie. table, view, query, procedure
You may want to try using the hint on the block properties of the form FIRST_ROWS.