Re: creating and executing queries in VBA.
- From: Steve P <SteveP@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 1 Nov 2007 09:31:01 -0700
Albert,
Thank you for your explanation...it was VERY helpful.
I'm gong to change my select statements to insert statements and utilize
some tables for temporary record storage.
THANKS AGAIN!
"Albert D. Kallal" wrote:
.
From what I can gather, v_qryENTITY_MASTERJOIN_ALLMATCHED is a query
object
now, but how can I execute it?
Execute it into what?
You can't just "execute" a select query. You can ONLY do this for an action
query (a query that does smting to the data, say delete, or update query).
When you execute a select command you have to have a place for the results
to go to.
so the following is legal
strSql = "update tblCustomers set City = 'New York' where city = 'N.Y.'"
currentdb.Execute strSql
however the following is not legal
strSql = "select * from tblCustomers"
currentdb.Execute strSql
When you execute sql it has to go into a report, or a form or a record set.
You can't just execute a select out of the blue and expect MS access to
guess were the results are supposed to. go. Further compounding this issue
is that MS access does not have temporary or intermediate cursors. We do
however have record sets and in effect they do represent the results of a
sql select. However, you can't execute FURHTER SQL statements on that
reocrdset like you can with temporary curses and SQL server for example.
Yyou can however loop and process the data row by row in a recordset (which
is often the case of what we need to do).
There is little if any advantage to building the SQL directly in code or
building in the sql via the query builder. AT the end of the day in both
cases you can run some code that will execute that SQL and put the results
into a record set.
If you're looking to execute sql select in code about only reasonable choice
here is to send it into a record set.
Dim rstData As DAO.Recordset
Set rstData = CurrentDb.QueryDefs("nameofquey").OpenrecordSet
As you can see you can type in your big huge long text of SQL. You gain
nothing in terms of your code.
Next, a new query is used to run a number ofcomputations on the previously joined table.
You might be able to base this 2nd query on the first query you built.
O\It's not clear what kinda computations are talking about so it's really
hard to guess the correct approach.
If your data processing means you actually have to update or modify the data
and save some values in each record for further processing, then you'll
likely have to send that data to a temporary table. So instead of using a
select query you might actually wind up using an append query.
That menas:
1) - emppty our temp data table
currentdb.execute "delete * from myTempTable"
2) run append query (our original query that is a joint of the two tables --
but now it's gonna be an append query)
CurrentDb.QueryDefs("NameOfAppendQuery").Execute
3) Possibly open up the data we just created and do further data processing
on it for example
dim rstData as dao.RecordSet
set rstData = currentdb.OpenreocrdSet("nameofThatTempTable")
do while rstData.EOF = false
' data process reocrd
rstData.MoveNext
loop
rstData.Close
it really depends on what you're really trying to accomplish at the end of
the day here.
It is very possible with the summary sorting and grouping options that the
report writer can actually build you the summary and totaling and do the
calculations that you actually need. You might not need a lot of code (or
any code).
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pleaseNOOSpamKallal@xxxxxxx
- References:
- Re: creating and executing queries in VBA.
- From: Albert D. Kallal
- Re: creating and executing queries in VBA.
- Prev by Date: RE: Importing Excel Files into Access
- Next by Date: Re: DAO Add/Edit/Update Methods
- Previous by thread: Re: creating and executing queries in VBA.
- Next by thread: Re: Get Data From List Box Selection
- Index(es):
Relevant Pages
|