Recordsets across event procedures?

  • Thread starter Thread starter Pat Franks
  • Start date Start date
P

Pat Franks

Hi,

Is it possible to use a recordset across two different
event procedures?

For example, if I were to create a recordset in response
to the before update event of a control, would it be
possible to use the recordset in the after update event
procedure?

Or does the recordset have a life only as long as the
event procedure runs?

Pat
 
You could declare the variable that you're using as the Recordset object to
be a global variable in the form's Declarations section. Then it would be
available in both event procedures' codes. Just be sure to close and set it
to Nothing in the AfterUpdate event procedure.
 
Ken,

Thanks - so there is a way.

Just one more request, I'm not sure what you mean by
declaring the variable used as the recordset object as
global?

At the moment, I would declare something like:

rstEmpee as Recordset

How do I make this global?

Thanks again.

Pat.
 
Go to the top of the form's module (above the first subroutine or function)
.... likely where you see
Option Explicit
or
Option Compare Database

Under these lines, you type the statement to declare the variable.

To make the variable "global" just within the form's module (cannot be seen
by other modules), type
Private rstEmpee As Recordset

To make the variable "global" for all modules throughout the database, type
Public rstEmpee As Recordset


One thing to watch for, though. If you have an error occur in your code
while it's running, and you don't have an error handler set up to handle it,
ACCESS has a nasty habit of "resetting" global variables to default values,
which, for a recordset variable, is Nothing. So be very careful in handling
all errors via error handlers so that your recordset doesn't get "lost"!
 
Thanks - it worked fine.

-----Original Message-----
Go to the top of the form's module (above the first subroutine or function)
.... likely where you see
Option Explicit
or
Option Compare Database

Under these lines, you type the statement to declare the variable.

To make the variable "global" just within the form's module (cannot be seen
by other modules), type
Private rstEmpee As Recordset

To make the variable "global" for all modules throughout the database, type
Public rstEmpee As Recordset


One thing to watch for, though. If you have an error occur in your code
while it's running, and you don't have an error handler set up to handle it,
ACCESS has a nasty habit of "resetting" global variables to default values,
which, for a recordset variable, is Nothing. So be very careful in handling
all errors via error handlers so that your recordset doesn't get "lost"!
--

Ken Snell
<MS ACCESS MVP>






.
 
Back
Top