Recordset/recordsource clarification

T

TeeSee

As far as I am aware the "recordsource" of a form could be a reference
to a table, query or SQL which i will call the "incumbent" record
source.
If I create a new record source in code will that override the
incumbent or do I need to have a "blank" record source property on the
form to create a new "recordsource"/recordset ?
Thanks as always
 
G

Graham Mandeno

Hi TeeSee

You can change the RecordSource in code on the fly with no problems:

Me.RecordSource = "Select ... "

The form will be requeried automatically.
 
A

Allen Browne

I take it you are modifying the RecordSource of a form to load a limited
number of records (perhaps just one), e.g.:
Me.RecordSource = "SELECT * FROM Table1 WHERE ID = 99;"

If you do that while you are already editing/entering another record, Access
first saves that record, and then loads the matching record(s), or gives you
the new record if none match. If you were entering a record that cannot be
saved (e.g. a required field is missing, a validation rule is not met, a
unique index is violated), the attempt to save the record fails, and so the
attempt to set the RecordSource property does not succeed (i.e. you are
still with the not-yet-valid entry.)

For clarity, I would encourage you to explicitly save before setting the
RecordSource, or doing anything else that requires an implicit save, e.g.
closing the form, applying a filter, changing the sort order, requerying,
.... Use:
If Me.Dirty Then Me.Dirty = False
or if you prefer (and the form has focus):
RunCommand acCmdSaveRecord

You may find it easier to set the form's Filter rather than changing the
RecordSource. The Filter is effectively a WHERE clause, so anything you can
do in a WHERE clause can be done in a filter (including subqueries.)

If you do change the entire SQL statement, make sure that you don't drop any
fields that were present previously, introduce fields from a different table
that have the same name, or allow fields to change data type (particularly
with calculated fields.) Some versions of Access can crash if these things
happen.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top