Using the Immediate Window

Debugging SQL in VBA

Often, when trying to build SQL in VBA, you run into problems getting the SQL to work correctly.  One important technique to use is to print the SQL out to the VBA Immediate window.  Often you will spot the problem right away there.  If you don't, you can copy/paste the SQL to a new query and try to run it there, where you will often get a more descriptive error than when running the SQL from VBA.  The way to print out the SQL is to add:

Debug.Print YourVariableName

to your code after the SQL is built but before it's used.  This is how it looks after being run: