Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Run Time Error 91

Posted on 2001-06-26
Medium Priority
Last Modified: 2010-05-02
I have an application with 2 projects: an exe and an ActiveX DLL project.
The DLL contains some classes.
These 2 projects are in 2 different directories.
I grouped these 2 projects (in group1.vbg) and I developed an application that run very well.
But when I generate an exe file from the exe project and a dll file from the ActiveX project the things don?t work.
I have registered the dll with regsvr32 and I have referenced this dll in the exe project.
When I run the program I get Run Time Error 91 (Object variable or with block variable not set), when the program tries to instantiate a class from dll for the first time.
Why did I get this error? Is it something like I haven?t set a path to the other directory with dll files?
MSDN help tells that the error occurs when it isn?t a reference to BOT project (ActiveX DLL project). But I have the reference to BOT project.
Question by:redundguy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6226948
Runtime 91 is the Object Variable or With Block not set!

Your ActiveX Dll project should be set to Binary Compatible in order to function correctly when compiled...
You can meanwhile inverse the order: first compile the DLL and then the EXE (you should not need to register this DLL, as during the MAKE it is registered automatically by VB).

Anyway, you might try to Add both projects into a project Group (See File->Add Project), which should simplify things for the debugging...

LVL 22

Expert Comment

ID: 6226955
You probably referenced the dll in your project group. So I suggest you open the EXE project only, set the new reference to the compiled dll and then recompile the executable.


Author Comment

ID: 6227087
I have set DLL project to Binary Compatible and I get the same error.
When I use Group of projects my reference is set to the vbp file of ActiveX project.
When I open only EXE project my reference is set to dll file of the ActiveX project.
When I run the group of 2 projects all run OK.
When I run EXE project I get error 91.

I am not very interested in what I have done wrong; but I?m more interested in what have I do to create and run the exe and dll files.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 17

Expert Comment

ID: 6227206
When you het the errors message, which module causes the error is it the EXE or the DLL?

Also, show us the code where you are creating a reference to your object within the DLL.

Does it look something like this:

Dim MyObj

Set MyObj=CreateObject("MyProject.MyClassName")


LVL 18

Expert Comment

ID: 6227553
are you using late or early binding?
LVL 28

Expert Comment

ID: 6229566
>>When I open only EXE project my reference is set to dll file of the ActiveX project

this leads me to believe he is using early binding (otherwise, supplying the reference would be pointless)

it sounds like a versioning problem.  try and unregister the component using regsvr32 /u [componentpath]
you really dont need to set binary compatibility until you know your component is pretty well developed, and doing so wont cause possible future requirements to break compatibility.
as mentioned previously, simply choosing File -> Make dll will register your component (on your machine).  in the exe that is referencing the component, open up project -> references, and deselect the component and click OK.  then, open up project -> references again and select your newly compiled component.

in addition, in code, ensure you have actually created an instance of your component before using it.  e.g.,

'you didnt do something like this
Dim obj As MyComponent.ClassName
Call obj.SomeMethod

ensure it is declared using the New keyword, or preferrably, setting the variable to a new instance of your component when it is needed.

Author Comment

ID: 6230027
Thanks, but I think that Visual Basic cheated me. I tried to trace the DLL program with MsgBoxes included in DLL program, and I got an error in DLL program. The error is related to a connection object.

In fact it is a small story. I got an error related to connection object. Then I insert a code line (MsgBox oConnection,,?connection string?) before the line where I got the error. When I run the program in IDE I see the connection string and then the program run OK. When I run the EXE program I get this 91 runtime error.

Now I have to discover the error cause. It is something related to using public variable of type connection in the Modules section of ActiveX project. One object (class) of the DLL project sets the public variable (connection) and other objects (classes) use this connection. I get the error in the first object code which try to use the connection.
I repeat that I don?t get the error when I run the program in IDE, only when I run the EXE and DLL programs.

LVL 28

Expert Comment

ID: 6230101
>>I repeat that I don?t get the error when I run the program in IDE, only when I run the EXE and DLL programs

this can happen if your exe is not referencing the most current version of your component.  try de-referencing and then re-referencing as i suggested above.  if that does not work, post some code (or check what differences there are in code between your test app and actual exe)

Author Comment

ID: 6233950
I have to reformulate the issue: Problem with Public variable in ActiveX DLL project

I have an application with 2 projects: an exe and an ActiveX DLL project.
The DLL contains some classes and is  ?Single threaded apartment?.
The module section of DLL contains a public variable vp1 (integer)
The EXE program instantiates an object in DLL program and call a method that set the vp1 variable.
Then the EXE program instantiate another object in DLL  program and call a method that show the value of vp1.
But I don?t see the value already set  with the first object: vp1 variable isn?t public for all classes from DLL project.
Why ?

Author Comment

ID: 6233985
I mention that these things happen when I run the exe file of the EXE project. When I run the Project from IDE, the second class shows the right value for the public variable.

Author Comment

ID: 6234188
I have to add some words about EXE project:
It is a form. In the load event I instantiate an object in DLL program and call a method that set the vp1 variable.
In the Click event of a button I instantiate another object in DLL  program and call a method that show the value of vp1. I cannot get the value allready set.
If I instantiate both objects in the same procedure (in load event of the form) I can get the correct value for vp1 when I call the method of the second object.

LVL 28

Expert Comment

ID: 6234749
please clarify what you mean when you say "object".  do you mean you are creating a new instance of the same class?  a different class in the DLL?  how many classes are in your DLL?  just give us the basic outline of your component, e.g.,


and then show the variable declarations and code you use to create each class and how you are calling the methods.

Author Comment

ID: 6236937
My application is quite complex but I actually reduced my problem at this code:

DLLname project contains:
-in Modules section:
public vp1 as integer
-in Class Modules section I have:
Class1, that have a method (method1) that sets vp1 variable
Class2 that have a method (method2) that gets vp1 variable and shows it.
In fact I want to share data between classes of the same DLL.

The exe program is a form with 2 buttons.

In the click event of the first button I have:

Dim oClass1 as DLLname.Class1
Set oClass1= New DLLname.Class1

In the click event of the second button I have:

Dim oClass2 as DLLname.Class2
Set oClass2= New DLLname.Class2

If I run my exe file application, method2 doesn?t show the value already set with method1.

If I run the application from the IDE all is OK. (But my application will be an exe file).

(Another thing is: If I instantiate and run method of both classes in the same event procedure, the method2 show the right value. But I really need 2 buttons)

The problem is that I cannot share data between classes. A solution is to keep the common data in the EXE project and to send this data as a parameter to the second class method (method2). I'm going to do that if I cannot use a public variable in the DLL.

LVL 28

Expert Comment

ID: 6238490
what is your threading model?  it should be set to apartment threading if you wish to share global data in this way.

Author Comment

ID: 6244159
I have set to apartment threading but it doesn't work.

I'm going to use the solution with keeping common data in the client exe and pass them as parameter to dll methods.

Maybe there are a direct solution to my problem but I realy have no time now to try.

Thanks to all of you


Author Comment

ID: 6248393
I have got a brilliant answer from Jim McNamara,
 on vb-world Q & A forum:

In general, you are trying to violate basic COM rules -
encapsulation in this case. Don't do it, not because of purist reasons, but because it won't work reliably as already you see, I'm not going into why it won't work. Just don't do it.
LVL 49

Accepted Solution

DanRollins earned 308 total points
ID: 7139826
Hi redundguy,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:

    Refund points and save as a 0-pt PAQ.

EXPERTS: Post a comment if you are certain that an expert deserves credit.  Explain why.
DanRollins -- EE database cleanup volunteer

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question