Refer to Custom Class Object in Immediate Window

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 00:42
Joined
Jan 20, 2009
Messages
12,895
I have an object instanced from a custom class.

I can refer to its properties from a VBA sub in either Word or Access.
Debug.Print Myobject.SomeProperty

However the same code in the Immediate Window results in:

Run-time Error '424': Object Required
(Word 2007)

Method Or Data Member Not Found
(Access 2007)

Is there something different about custom class objects and the Immediate Window?
 
Is the instantiated object a global variable in a Module?
 
Yeah, as vbaInet said, you need to have an instantiated variable before you can do it and only with that can you refer to it. So, you can set a breakpoint after it is instantiated or use a watch or the locals window in conjunction with a breakpoint.
 
If it's more of something that you can create & throw away:

Code:
set x = myclass:?x.membername

should work.

EDIT: to give a more practical example, I usually find myself needing to recall fields name for a query that's not bound at the moment or such:

Code:
set d = currentdb:for each f in d.querydefs("myquery").fields:?f.name:next

This gives me all fields names in the immediate windows.
 
The object is declared as a Global variable in the delarations section of a Standard Module.

Then a procedure in that module instantiates it and calls a method of the object that creates a disconnected ADO recordset. This recordset is declared as Private in the Declarations section of the Class Module. (I also tried Public there which made no difference and besides I don't want it Public).

All procedures in the project happily retreive the values from the object's properties but not for expressions in the Immediate Window. (These are Property Get procedures, not Public variables)

The Immediate Window expressions do work while any procedure has an active break, even if that procedure does not refer to the object.
 
It works if I instantiate a new object in the Immediate Window.

x = New clsClass: x.MakeRecordset: ? x.member
 
Did you declare it like:

Dim myobject As New ClassName
 
Glad you worked it out.

Though.. I'm surprised it worked because I'd have expected to require a set statement for this part:
Code:
x = New clsClass...
 
Did you declare it like:

Dim myobject As New ClassName

vbaInet, Dim isn't valid in immediate windows. Even with an Option Explicit, you cannot dimension anything in Immediate so you have to treat it as if Option Explicit do not apply & make up variables.
 
I meant Public in place of Dim ;)

and I was referring to the declaration in a Module not the immediate window.
 
Including New in the Public or Dim line makes the object auto-instancing, meaning it will instance automatically when it first appears in the code without explicitly being instanced using the Set command. It is usually best not done this way.

However I tried it anyway and it doesn't change the behaviour of the immediate window.
 
Glad you worked it out.

Though.. I'm surprised it worked because I'd have expected to require a set statement for this part:
Code:
x = New clsClass...

Just a typo. Yes, the Set is essential.
 
It is usually best not done this way.
Yep! For testing purposes it is necessary unless you're going to call a public sub/function to instantiate the object.

However I tried it anyway and it doesn't change the behaviour of the immediate window.
Any chance of us seeing parts of your class or probably upload the class for us to test?
 
Found the problem.
In Access I had inadvertantly named the new object with the same name as the Project. Changing either the Project name or the object name fixed it in Access.

Apparently VBE can deal with the repeated name from within a module but spits the dummy when referred to from the Immediate Window.

In Word the problem was that the object had been instantiated in a Word Object Module. These do not allow Public variable declarations. Conseqenetly while the variable was available to procedures in within that module only Public variables are available to the Immediate Window. It worked find when instantiated as Public in a Standard Module.

I had created the Class Module in Word and switched to Access without really looking for the problem when it didn't work properly. (I trust Access more than Word.) Unfortunately by the time I started looking at the declarations I had already moved over to Access where the problem had a completely different cause. Getting much the same error in both environments for completely different reasons blinded me to what was wrong in each of them.

Leaving it alone for the weekend was the key to solving it. Thanks for all the suggestions.
 

Users who are viewing this thread

Back
Top Bottom