Writing queries in VBA

  • Thread starter Thread starter Jone
  • Start date Start date
J

Jone

Hi
Can anybody refer me to a web site where I can learn to write quires in VBA,
if I’m not mistaking its called SQL?
 
Queries in MS Access and other DMBS' are constructed in SQL (structured query
language). You can find various tutorials on SQL by googling "SQL" - one of
the first results being

http://www.w3schools.com/sql/default.asp

Constructing SQL in VBA is simply a matter of constructing strings, where
the string you are building is an SQL statement:

Dim strSQL as String
strSQL = "Select tblName.Name, tblName.Email From tblName " _
& "Where (((tblName.ID)=" & lngVariable & "));"

hth.
 
Jone said:
Can anybody refer me to a web site where I can learn to write quires
in VBA, if I’m not mistaking its called SQL?

One way to learn is to mock up a query in the normal Access query designer.
Then switch to SQL View (View menu), and you have a sample of the statement
you need in your VBA code.

That's not only a good way to learn: it's something I still do after years
of working with Access. In fact, I then use this utility to copy the SQL
into code:
Copy SQL statement from query to VBA
at:
http://allenbrowne.com/ser-71.html

As a basic example of SQL syntax, this query returns 3 fields from 1 table,
applies criteria, and sorts the results:
SELECT CompanyID, Company, City
FROM Table1
WHERE (City = "Springfield")
ORDER BY Company;
The clauses must be in the right order.
Line endings and brackets are optional.

If you want to get serious about learning SQL, get "SQL Queries for Mere
Mortals" by Michael J. Hernandez and John L. Viescas :
http://www.amazon.com/SQL-Queries-Mere-Mortals-Hands/dp/0201433362
 
Back
Top