O'Reilly logo

Access™ 2007 VBA Programmer's Reference by Armen Stein, Geoffrey Griffith, Rob Cooper, Teresa Hennig

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 15. SQL and VBA

You may be familiar with SQL; after all, it's inside every query you create. SQL (Structured Query Language) is the language of queries and recordsets; it's how you retrieve, update, insert, and delete records in your database tables.

When you use the query Design view in Access, you are actually building a SQL statement under the covers. Most of the time, you won't actually need to look at the SQL code, but you can see it using the SQL view if you're curious.

Conversely, you can take most SQL statements, paste them into the SQL view of a new query, and then switch over to Design view to see how they work. There are a few types of SQL statements for which this won't work—union queries and pass-through queries, for example, cannot be viewed using Design view.

Even if you're comfortable using SQL in queries, you may not be familiar with building SQL statements in VBA. If you're not, you're missing out! Using SQL in VBA is a powerful technique that can enable many great features in your Access applications. By using VBA, you can build custom SQL statements for combo boxes, forms, and reports. For example, you'll be able to change the sorting and selecting of records on continuous forms, control the record selection on reports, and limit the drop-down lists of combo boxes based on other combo boxes.

Let's begin by exploring how to build SQL statements using string variables in VBA.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required