"JimS" <(E-Mail Removed)> wrote in message
news:77CE4153-C0B6-4473-88D4-(E-Mail Removed)...
>I developed a class module in A2007. As I went along, I would instantiate
>it
> each time I needed it ("dim oBoM as new clsBillofMaterial"), then set it
> to
> nothing when I was done with it, typically at the beginning and end of an
> event procedure. Then, I thought, "why re-instantiate it so often...why
> not
> instantiate it in the opening declarations for a form where I might use
> it,
> then use it as needed.
>
> Well, I'm gettting all balled up in scope issues. Am I doing this right?
> What's the approach?
>
I usually create and setup the class object in the forms load event.
However, often that object needs to be used in the next several forms that
will be opened.
So, you often see the following in my code:
clsBookInfo.MyClear ' setup class object
DoCmd.OpenForm "frmCalenderMain",,,,,,"Select date for event booking"
In form CalendarMain, you see in the defs:
Option Compare Database
Option Explicit
Public clsBookInfo As clsBooking
Dim frmPrevious As Form
Note how there is no new keyword used. I not going to instantiate a new
copy,
but I want the class object from the previous CALLING form.
So, my on-load code goes:
Set frmPrevious = Screen.ActiveForm
Set clsBookInfo = frmPrevious.clsBookInfo
Note how I now have a pointer to the previous class ojbect. Also, as a
codeing standard, I also pick up the previous form as per above. That way, I
cna have many differnt forms call this form, but on a close event I can go:
frmPrevious.Requery, etc.
In other words, I can access the prevous form as a varible.
And, when this form closes, all of the setup and use of the clbBookInfo
object is returned to the privous calling form. My applcaions will often go
2, 3 or even 4 forms deep using the above concepts... When the use finally
is done and starts closing the forms to get back where they came from, that
class objects values return back along for hte ride.
On the form when being closed, I can now call/execute code in the calling
form like:
frmPrevious.SelDoneB
DoCmd.Close acForm, Me.Name
SelDoneB is custom code in the calling form that needs to be run when I am
closign the current form. The nice part about using this approach is that I
don't have to make the form dialog (it is model, but not dialog).
The above desing also allows for multiplile instances of a form to be
opended...and the correct code from the calling (prevous) form is run when
using the above approach.
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
(E-Mail Removed)