Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing and debugging SQL queries in MS Access

MS Access has limited capabilities to manage raw SQL queries: the editor is quite bad, no syntax highlighting, it reformats your raw SQL into a long string and you can't insert comments.

Debugging complex SQL queries is a pain as well: either you have to split it into many smaller queries that become difficult to manage when your schema changes or you end-up with a giant query that is a nightmare to debug and update.

How do you manage your complex SQL queries in MS Access and how do you debug them?

Edit
At the moment, I'm mostly just using Notepad++ for some syntax colouring and SQL Pretty Printer for reformatting sensibly the raw SQL from Access.
Using an external repository is useful but keeping there's always the risk of getting the two versions out of sync and you still have to remove comments before trying the query in Access...

like image 858
Renaud Bompuis Avatar asked Jan 07 '09 02:01

Renaud Bompuis


People also ask

How will you handle SQL queries in MS Access?

To see how your query is created in sql when you create it in query design, let us open your database. Select the Query Design from the Create tab and add the tblEmployees table. Select the field you want to see as query result and then run your query. You can now see all the employee information as query result.

What is SQL debugging?

The Transact-SQL debugger allows you to interactively debug stored procedures by displaying the SQL call stack, local variables, and parameters for the SQL stored procedure.

What are the 4 types of queries in MS Access?

There are five types of query in Access. They are: Select queries • Action queries • Parameter queries • Crosstab queries • SQL queries.


2 Answers

For debugging, I edit them in a separate text editor that lets me format them sensibly. When I find I need to make changes, I edit the version in the text editor, and paste it back to Access, never editing the version in Access.

Still a major PITA.

like image 114
recursive Avatar answered Oct 28 '22 15:10

recursive


I have a few tips that are specific to SQL in VBA.

Put your SQL code with a string variable. I used to do this:

Set RS = DB.OpenRecordset("SELECT ...")

That is hard to manage. Do this instead:

strSQL = "SELECT ..."
Set RS = DB.OpenRecordset(strSQL)

Often you can't fix a query unless you see just what's being run. To do that, dump your SQL to the Immediate Window just before execution:

strSQL = "SELECT ..."
Debug.Print strSQL
Stop
Set RS = DB.OpenRecordset(strSQL)

Paste the result into Access' standard query builder (you must use SQL view). Now you can test the final version, including code-handled variables.

When you are preparing a long query as a string, break up your code:

strSQL = "SELECT wazzle FROM bamsploot" _
      & vbCrLf & "WHERE plumsnooker = 0"

I first learned to use vbCrLf when I wanted to prettify long messages to the user. Later I found it makes SQL more readable while coding, and it improves the output from Debug.Print. (Tiny other benefit: no space needed at end of each line. The new line syntax builds that in.)

(NOTE: You might think this will let you add add comments to the right of the SQL lines. Prepare for disappointment.)

As said elsewhere here, trips to a text editor are a time-saver. Some text editors provide better syntax highlighting than the official VBA editor. (Heck, StackOverflow does better.) It's also efficient for deleting Access cruft like superfluous table references and piles of parentheses in the WHERE clause.

Work flow for serious trouble shooting:

VBA Debug.Print >       (capture query during code operation)
  query builder   >     (testing lab to find issues)
     Notepad++      >   (text editor for clean-up and review)
  query builder   >     (checking, troubleshooting) 
VBA

Of course, trouble shooting is usually a matter of reducing the complexity of a query until you're able to isolate the problem (or at least make it disappear!). Then you can build it back up to the masterpiece you wanted. Because it can take several cycles to solve a sticky problem, you are likely to use this work flow repeatedly.

like image 42
Smandoli Avatar answered Oct 28 '22 16:10

Smandoli