Write SQL Vba
Write SQL Vba
http://www.fontstuff.com/access/acctut15.htm
Home
Excel
Access
Word
Outlook
FrontPage
VBA
Downloads
Index
Access & SQL 2 Access Home Continue the tutorial: Part 1: Introduction Part 2: VBA & SQL Part 3: Examples Part 4: Building Queries Part 5: More Instant Queries Part 6: Dynamic Reports Printer Friendly Version
For clarity, the different clauses are shown on separate lines. This is how the SQL view of the Access query design window displays its SQL.
1 of 9
27/07/2012 9:04 AM
Access Tips: Access and SQL Part 2: Putting VBA and SQL Together
http://www.fontstuff.com/access/acctut15.htm
2 of 9
27/07/2012 9:04 AM
Access Tips: Access and SQL Part 2: Putting VBA and SQL Together
http://www.fontstuff.com/access/acctut15.htm
field names and criteria, to construct essentially simple (although sometimes long) statements detailing what you want the program to do with your data. SQL can often be used alone, for example when setting the RecordSource property of a form or report in design view, or when working in the SQL View of the Access query design window. But this tutorial is about how to combine SQL with VBA. Its aim is explain the rules of SQL and to encourage you to use good code-writing practice and to avoid the pitfalls and problems that can occur. The way you write your code is a very personal thing so I'm going to show you how I do things, using some techniques I have learnt from others and some I've figured out myself. You don't have to do it my way, but it works for me so it's what I teach! VBA is a very flexible language and there are often many different ways in which VBA can achieve the same task. SQL on the other hand is a very precise and inflexible language. Everything has to be just so, but it has the advantage of also being very simple. As I explained in Part 1: Setting the SQL Scene there are several dialects of SQL. Here I will be using the Jet SQL that drives the Access database engine. ^ top
3 of 9
27/07/2012 9:04 AM
Access Tips: Access and SQL Part 2: Putting VBA and SQL Together
http://www.fontstuff.com/access/acctut15.htm
(such as [Date] which is also the name of a function) without causing conflicts. But I do this for another reason too. I know that if I see some text in square brackets I know it's a field name, whether it has spaces in it or not... SELECT tblStaff.[Firstname], tblStaff.[Lastname] FROM tblStaff WHERE tblStaff. [Office]="Paris;
There is a problem with nested quotes in the WHERE clause. Compare the two examples below. In the first example the VBA sees two text strings enclosed by double quote marks, and between them a word it doesn't know (Paris) so it generates an error.
But when the quote marks are alternated as shown in the second example, the problem doesn't arise. The VBA sees a text string enclosed by double quotes, inside which is some more text enclosed in single quotes. I working with multiple sets of quotes gets confusing, you can always use the ASCII character code for the double quote mark - Chr(34) - instead. There is an example of this in the next section.
4 of 9
27/07/2012 9:04 AM
Access Tips: Access and SQL Part 2: Putting VBA and SQL Together
http://www.fontstuff.com/access/acctut15.htm
^ top
You want to allow the user to choose a value for the Office criteria each time the query is run, so you build a dialog box in which there is a combo box containing a list of Offices. The combo box is named cboOffice. You can insert a reference to the value of the combo box directly into the SQL statement:
Alternatively, you can place the value of the combo box into a variable and then insert the variable into the SQL statement:
Using a variable can make the SQL statement code easier to read and understand, especially when there are several variable criteria to consider. It is sometimes essential to use this method when the value has to be examined or manipulated in some way before it is passed to the SQL. Whatever method you choose, you must remember to include any necessary data type qualifiers in the SQL string. In the illustration below, a single quote mark is included in the SQL string either side of the text variable (marked with red arrows):
5 of 9
27/07/2012 9:04 AM
Access Tips: Access and SQL Part 2: Putting VBA and SQL Together
http://www.fontstuff.com/access/acctut15.htm
Alternatively, the ASCII character code for the double quote mark (Chr(34)) can be inserted either side of the text variable, but outside the SQL string. This method requires more typing but avoids conflicts and confusion arising from nesting quotes.
Remember that as with "hard-coded" criteria, variables require the correct qualifiers for their data type: quotes for text, hash-marks (pound signs) for dates, and nothing for numbers. ^ top
Sometimes error messages are more specific and point you directly to the clause that is causing the problem. Here the word "BY" has been omitted from the ORDER BY clause of the
6 of 9
27/07/2012 9:04 AM
Access Tips: Access and SQL Part 2: Putting VBA and SQL Together
http://www.fontstuff.com/access/acctut15.htm
SQL statement:
If you misspell the name of a database object such as a table, or refer to one that doesn't exist, the Jet database engine will not recognise it. These errors are usually quite easy to trace:
Not really an error message, but a response to an error in the SQL statement. If you type the name of a field incorrectly, the Jet database engine sometimes interprets your SQL as a parameter query and displays the familiar parameter dialog. Although this might seem confusing at first, it is usually quite easy to diagnose and correct because the offending name is displayed on the dialog. You just need to find it in your code and correct it:
7 of 9
27/07/2012 9:04 AM
Access Tips: Access and SQL Part 2: Putting VBA and SQL Together
http://www.fontstuff.com/access/acctut15.htm
The error messages are delivered in the usual way using the Visual Basic error message dialog and don't explicitly state that the problem with your code lies in your SQL statement, although this is usually evident from the message itself. Pressing the Debug button will take you to the offending line of code but this may not be the SQL statement itself. Here are some examples:
In the example above the code crashed when it tried to apply the SQL statement to a stored query. Access checked the SQL statement before applying it to the query and found a syntax error ("ORDER" should be "ORDER BY") and highlighted the code line that it could not execute, rather than the one that contained the error. In the next example, it is clear that there is a spelling error somewhere but it might take a while to find:
Here, the error lies in the misspelling of a table name in the SELECT clause of the SQL statement but the error did not arise until the code tried to run the query and the specified table could not be found.
The next example must get the prize for the most confusing error message that Access has to offer!
I have seen it in two different circumstances. It is displayed if an error occurs because, from
8 of 9
27/07/2012 9:04 AM
Access Tips: Access and SQL Part 2: Putting VBA and SQL Together
http://www.fontstuff.com/access/acctut15.htm
a VBA procedure you ran a parameter query - or a query that thought it was a parameter query and so displayed the parameter dialog (see the example above where the field name was spelled incorrectly) - and you clicked the parameter dialog's Cancel button. In normal circumstances that would be OK but if as a result your code crashed, the "You canceled the previous operation." message is displayed and, I suppose, it makes sense. But this message also appears when there is a different kind of error, when you attempt to use an SQL statement from VBA in which the data type of a WHERE clause's criteria does not match the data type of the corresponding field. For example you might have a date field and supply a text data type: WHERE tblStaff.BirthDate='Tuesday instead of matching the data types correctly: WHERE tblStaff.BirthDate=#09/27/1950#.
What's Next?
Enough theory! Now it's time to see some SQL code in action. The next tutorial is a practical session showing you how SQL can build and modify tables and records: Access and SQL Part 3: Some Practical Examples ^ top
Hit Counter
9 of 9
27/07/2012 9:04 AM