Hi. sql skills not great. Sometimes I'd like to let
Access generate the sql for me (for example, in the query
builder) and then use it in vb. But of course it isn't
the same. Can vb sql handle inner joins?
Yes, virtually all of the sql you make in the query builder will work in the
VB code editor.
Can it handle
union queries?
Yes...
What differences in the syntax do I need
to look out for and change to make it work in vb?
There is not much different in syntax, but you do have to be aware of what
the sql does. For example, the follow code in VB uses sql to update a city
field.
dim strSql as string
strSql = "update tblCustomers set City = 'New York' where City = 'N.Y.'"
currentdb.Execute strSql
We can also type the above sql into the query builder, and hit the ! (run)
button.
However, what about the following:
strSql = "select City from tblCustomers"
currentdb.Execute strSql
Now, the above will NOT work. The reason here is that the sql will not know
where the results should go!! Should the results go the screen, or maybe a
report, or where? So, you need to be aware of quires that don't return any
records, and modify data. These types of queries are called action queries,
and are most often used in VB code (they modify data..but do NOT return
records).
So, it not so much that things are different in VB, but you do have to be
aware of what the sql is going to do. You can pull sql records into what is
called a reocrdset, and then use code to work on that data. We can go:
dim strSql as string
dim rstRecs as dao.RecordSet
strSql = "select City from tblCustomers"
set rstRecs = currentdb.OpenRecordSet(strSql)
do while rstRecs.EOF = false
debug.print rstRecs!City
rstRecs.MoveNext
loop
rstRecs.Close
set rstRecs = nothing
The above program loop would diplay all cities in the debug window. So, you
can pull data as a select, or union query in VB, and process it as above.
However, most of the time, you can just build the sql in the query builder,
and then have the VB code run that query. There is no need to "paste" the
sql into the code if you already got a query built. You can simply build the
query, and then run it in VB code.
Currentdb.Execute "yourQueryNameGoesHere"
So, just leave the sql in the query builder...