Error 91 only occuring when VBA editor closed?

JamesM

Registered User.
Local time
Tomorrow, 05:34
Joined
Jul 8, 2013
Messages
16
Error 91 - Object variable or With block variable not set

I am getting this error telling me that an object variable is not set.

I know which variable it is but when I step through the debugger it sets the variable and all is fine? has anybody ever had an issue where a public variable of a class is not getting set when the VBA Editor is not open?

This is so hard to explain? If you are need to know more info just ask!

Cheers in Advance
 
Little more info would help.
Version of MS Access
Are you by chance running the accdb from a networked location to your PC rather than running it locally on your PC? My network has so much latency, that kind of thing could happen.
 
are you stepping through code after an error?

if so, note that the error handler is not reset until you specifically issue a resume statement.

that might be the issue.
 
Error 91 - Object variable or With block variable not set

I am getting this error telling me that an object variable is not set.

I know which variable it is but when I step through the debugger it sets the variable and all is fine? has anybody ever had an issue where a public variable of a class is not getting set when the VBA Editor is not open?
Yes sometimes it has happen for me - mostly a DoEvents in the line after the SET helps.
 
Little more info would help.
Version of MS Access
Are you by chance running the accdb from a networked location to your PC rather than running it locally on your PC? My network has so much latency, that kind of thing could happen.

Ok, So I am running my access db from my c:

I have a main form that loads. This form has subforms with subforms in them..

I have a module called "Globals" with a public variable declared
Code:
Public log as AppLogger

I have a Class Module called "AppLogger"
This class has public functions that write to a text file log


In my main form, On Load I set the Public variable in my "Globals" class
Code:
Set log = New AppLogger
This sets the initializes the Class "AppLogger" and sets an instance to my public "log" variable in my globals module

So when I load the main form all is good. The public variable is set and I can call
Code:
log.Debug("testing debug")


My problem is that this public variable that should stay set is being set back to nothing/emty after the Main form load sub has ended?

BUT! as soon as I open the VBA editor, the varibale "log" is set again and logging works fine?

I have no Idea why my variable is losing its instance of my "AppLogger" Class

The only thing I can think is that the subforms loading within my main form are causing an issue? seems to be fine when there is no subform? Still, makes no sense why it is fine when VBA editor is open?

Any help is much appreciated!

If I can work out this bug I will share my entire logging code with everyone that allows you to log to a text file at many levels from within access with simple calls
eg:
Code:
log.Debug("testing debug")
log.Error("Error doing something")
log.Warning("warn the user")

Thanks James :)
 
Keep in mind that in your own class modules, if you have the locals window open, you can run code in your class by checking values in the locals window.
 
Keep in mind that in your own class modules, if you have the locals window open, you can run code in your class by checking values in the locals window.

Thanks, but my all is fine when I am using the vba editor? I add a watch on public variable and it stays set?

As soon as I close the vba editor and reopen the database the variable is just empty after first initialize?
 
Also, a class exposed in a public standard module is not reliable. If you want to maintain a class for the lifetime of your application I would open a hidden form, and expose the class from there.

You can also expose a class like this, in a property, which builds a new instance of the class if the current instance is nothing . . .

Code:
dim m_class as cMyClass

Public Property Get MyClass As cMyClass
[COLOR="Green"]  'creates an instance if the private variable m_class is nothing[/COLOR]
  If m_class Is Nothing Then Set m_class = New cMyClass
[COLOR="Green"]  'always returns an instance[/COLOR]
  Set MyClass = m_class
End Property
This way the class is only ever created if it is called, and automatically recreated if the base variable m_class gets broken.

Also, if you expose a class on a hidden form, you can re-expose it in a Standard Module from the form, so if formA is a hidden form exposing the custom property MyClass of type cMyClass, you can do this in a standard module and get a reliable globally available class instance . . .

Code:
Public Property Get MyClass as cMyClass
  Set MyClass = Forms("FormA").MyClass
End Property

. . . and in that way the class is reliably located on the hidden form, but your code can call it without constantly prefixing it with the Forms("FormA") reference.

And you can guarantee the form is available using this in a standard module . . .
Code:
Private Property Get FormA as Form_FormA
  Const FN = "FormA"
  If Not CurrentProject.AllForms(FN).IsLoaded Then DoCmd.OpenForm FN, acHidden
  Set FormA = Forms(FN)
End Property

Public Property Get MyClass as cMyClass
  Set MyClass = FormA.MyClass
End Property
You can see what's going on there, all available as a pull, and only when and as required.
 
Thanks lagbolt,

So do,

I create a form called say "frmAppLogger" and I open as hidden when database starts

Inside "frmAppLogger" I create a property that creates and instance of my AppLogger class

Code:
'frmAppLogger
Private m_appLogger as AppLogger
 
Public Property Get MyAppLogger as AppLogger
 
  If m_appLogger Is Nothing then 
    Set m_appLogger = new AppLogger  
  End If
 
  MyAppLogger = m_appLogger
 
End Property

No how do I implement on main form load and use it?

so I just want to use my AppLogger class as

log.debug("test") ect..


Thanks so much for your help!
 
Well, now it's on that form, so it's at . . .
Code:
Forms("frmAppLogger").MyAppLogger
If you need to use it frequently on a different form, you can re-expose it on that form as a private property like . . .
Code:
Private Property Get LocalAppLoggerReference As AppLogger
  Set LocalAppLoggerReference = Form("frmAppLogger").MyAppLogger
End Property
. . . but use a shorter name.
Or look at the last code block in my last post. You can expose the form as a property and then expose the AppLogger as an application-global property from a standard module, so you can use it like a global, but it actually lives on the form. But these are all just variations on the same theme.
 
Hi lagbolt,

I am still getting a the exact same error!

I have created the dummy hidden form with get property of my logger class and also created the property of my hidden form so it ensured that it was accessible.

Once again on first load all good, then as I click a button that calls the log, ERROR!

So open vba editor, then it all works. Steps through and all is fine!

I swear there is nothing wrong with the code. I believe this is a serious bug in Access 2010.

I have no Idea what to do other than have no logging in my application which will mean that once the database is shipped out I'll have no way of knowing what happened if an error occurs!

I can send you my databases if you like and see it happen first hand.

Once again, thanks for your help!

:banghead:
 
maybe you are declaring/instantiating "log" elsewhere in your programme and "losing" this particular assignment.

maybe you need

if log is nothing then set log = new Applogger
 
Thanks, but my all is fine when I am using the vba editor? I add a watch on public variable and it stays set?

As soon as I close the vba editor and reopen the database the variable is just empty after first initialize?[/QUOTE]

perhaps it's this. if you debug and END/RESET a program, you will lose any and all public variables (and log will be reset to nothing). if you continue running code your variables will be preserved.
 
I want to thank everyone that assisted on this thread!!!

I have found what is causing the null object

Code:
Application.VBE.ActiveCodePane.CodeModule

I was using this to get the current class that was running when the log was fired. The log variable was always initialized and was a red herring!

Dam!! 2 nights of pulling my hair out. Oh well, I have learnt some new stuff!

Thanks everyone!!!:D
 

Users who are viewing this thread

Back
Top Bottom