a. I can run the SQL using DoCmd.OpenQuery "Qdf.SQL" method and at the
same time I can use xx.OpenRecordset method. Is OpenQuery also create
a recordset?
Most of the DoCmd methods are simply vba handles to normal Access GUI
commands, rather than actual data manipulations. DoCmd.OpenQuery opens a
datasheet on the desktop; there is no easy programmatic access to the
rows.
If you want to manipulate the data, you need one of the OpenRecordset
methods:
DAO.Database.OpenRecordset() is the usual way of handling any SQL
SELECT statement.
DAO.QueryDef.OpenRecordset() will create a recordset from a pre-defined
qdf. It is said that precompiling makes a query faster, but if it does it
must by milli-seconds. An advantage is that once a query is debugged, you
don't have to mess about with vba string slicing any more. The
disadvantage is that you need to protect it from users rebugging it!
The database and querydef objects both have .Execute methods for
carrying out action queries such as delete, update, and insert. They are
generally preferred to DoCmd.RunSQL because you don't get warnings about
rows being changed, and you can get a trappable error if there's a
problem with the SQL parsing.
b. How can I refer to a recordset in the form or report? As stated, I
have created forms and report based on a Query which can be changed by
Qdf; however, I do not know how to refer to a recordset yet instead.
There are a couple of ways to change the recordsource of a form or
report.
At design time, you can use a parameterised query that points its
parameters at a UI control -- you just have to make sure that the form
containing it is available when the report is opening:
SELECT This, That, TheOther
FROM Somewhere
WHERE That >= Forms!MyForm!txtMinimumValue
An alternative method is to set the RecordSource property from within the
object itself, usually in the Load event:
Private Sub Form_Load()
' obviously, this need proper error trapping
strSQL = "SELECT This, That, TheOther " & _
"FROM Somewhere " & _
"WHERE That >= " & Me.OpenArgs
Me.Recordsource = strSQL
Me.Requery
End Sub
with this form, you can get info from the user, look up values on the
internet, whatever.
To orient yourself, you would do best to look round a working sample,
like the Northwind application; or get a good general Programming Access
book.
Hope that helps
Tim F