INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Class_Initialize firing several times when the object isn't even bein instantiated yet
2

Class_Initialize firing several times when the object isn't even bein instantiated yet

Class_Initialize firing several times when the object isn't even bein instantiated yet

(OP)
Hi,

I am creating a new class, which is instantiated on a form as follows...

CODE

Option Compare Database
Option Explicit
Private oNP As New clsNarrowPanel 

I.E.

It's a form global, which I thought only gets created when the form opens, but it seems the class Class_Initialize is firing several times, before the form it is declared on is even opened?

What am I missing here?

Thanks,
1DMF.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

RE: Class_Initialize firing several times when the object isn't even bein instantiated yet

(OP)
OK, it seems to be with bound subform object frame even when the source isn't set yet.

Access is opening forms on a non-visible tab control that doesn't even have the form as the source set against the subform control?

I changed the global to just the declaration, moved the instantiation to form open and it still fired several times.

So I moved the instantiation to form_current and Bingo!

Now only when the tab control is used, the page with the bound subform object is loaded with the form and an actual record is loaded onto the form does the class object get instantiated.

However, now each time the record changes it will fire... which I don't want, it is a global for the form not each record?

I guess it's a 'nothing test if statement' so at least it's only instantiated the once!

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

RE: Class_Initialize firing several times when the object isn't even bein instantiated yet

>I guess it's a 'nothing test if statement' so at least it's only instantiated the once!

Won't work, since Is Nothing will always be False ... (and will itself cause an Initialize event if the class happens to be uninitialized)

RE: Class_Initialize firing several times when the object isn't even bein instantiated yet

IMO it is pretty sloppy to declare and instantiate at the same time for just this reason especially in event driven coding. I always declare the object than instantiate right before I need it.
Forms open from the inside out so the subform loads first which may be the cause of the problem. Try the onload event which occurs after the on open. In any case do not see how this could fire more than once. Put a message box in the on open event to test if that is in fact happening. If not there is something more to it.

RE: Class_Initialize firing several times when the object isn't even bein instantiated yet

(OP)
>>IMO it is pretty sloppy to declare and instantiate at the same time for just this reason

I assume you mean in a VBA world not OOP in general?

>> Try the onload event which occurs after the on open
Yup , currently playing with that event handler smile

>> Won't work, since Is Nothing will always be False ... (and will itself cause an Initialize event if the class happens to be uninitialized)

How so? If I declare an object of a data type without the new, it's nothing isn't it until set to a new instantiated object, can you elaborate please Mike.

>> Put a message box in the on open event to test if that is in fact happening. If not there is something more to it.

Dang, you are right MajP, had some other global module code using the declaration with 'new' so it was other instances of the object firing the initialise - d'oh.

So I was missing something.

<< heading to bathroom to look for flannel to wipe egg off face! hammer

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

RE: Class_Initialize firing several times when the object isn't even bein instantiated yet

>Private oNP As New clsNarrowPanel

This form of declaration creates a self-instantiating class. Every time you try and access it checks to see if it is already instantiated and, if not, instantiates itself. You can get some very odd bugs if you are not aware of this behaviour.

RE: Class_Initialize firing several times when the object isn't even bein instantiated yet

(OP)
Yes, but I meant remove the 'new' and then use 'nothing check', so it is only instantiated the once.

Got it working perfectly now, so appreciate the heads up from both!

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

RE: Class_Initialize firing several times when the object isn't even bein instantiated yet

@Strongm

Quote:

won't work, since Is Nothing will always be False
Not sure what you are referring to but that is incorrect.
Simple test

CODE

dim rs as adodb.recordset
msgbox rs is nothing
set rs = new adodb.recordset
msgbox rs is nothing 
true
false
Maybe you are referrring to isNothing used on value types?

@
1DMF

Quote:

I assume you mean in a VBA world not OOP in general?
No. OOP in general. One of the tenants of good code design is always to limit the lifetime and scope of any variables. Why instantiate an object prior to being ready to use it. There may be reasons that you can declare and instantiate at the same time, but do not do it out of habit. Do it as the exception where it makes sense to do it. Your example shows just how there may be unattended consequences.

RE: Class_Initialize firing several times when the object isn't even bein instantiated yet

Nope. You are not using the form of declaration that 1DMF was using.

Private rs As New ADODB.Recordset
MsgBox rs Is Nothing

RE: Class_Initialize firing several times when the object isn't even bein instantiated yet

I see the confustion. You missed the part where he said he had changed the module level variable to only declaration and moved the instantiation to the open event.

Quote:

I changed the global to just the declaration, moved the instantiation to form open and it still fired several times.
....
I guess it's a 'nothing test if statement' so at least it's only instantiated the once

RE: Class_Initialize firing several times when the object isn't even bein instantiated yet

(OP)
Thanks MajP, think Mike needs to go to spec-savers bigglasses

TBH I do use a few 'declare as New' global form variable, because they are like model/orchestrating controllers for MVC and then use an UpdateView sub to populate unbound controls on the form.

I have some apps with a mish-mash of bound and unbound form controls and where possible I am moving to an unbound MVC type paradigm especially when I add new functionality.

I thought (and was taught by the OU), that having a global form controller class instantiated when the form opens and lasts the life-cycle of the form, was better than every form event handler keep creating and destroying multiple copies of the same object?

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close