• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 349
  • Last Modified:

Object Initialized?

Is there a way to check to see if an object has been initialized before trying to reference it?

eg.

Calling
If qMyQuery.recordcount then
  do something
end if

before qMyQuery has been set.

Thanks
0
jrcp
Asked:
jrcp
  • 3
1 Solution
 
chandukbCommented:
if not qMyQuery is nothing then
   Set qMyQuery
else
   'object has been set, so do your stuff
end if

Chandu
 
0
 
chandukbCommented:
Sorry,

If qMyQuery Is Nothing then
  'Set qMyQuery
else
  'Object has been set, do processing
end if

Chandu
0
 
chandukbCommented:
As an example, Here is how I open my connection

If moConnection Is Nothing Then 'Connection not yet open
    Set moConnection = New ADODB.Connection
    moConnection.CursorLocation = adUseClient
Else
   'Connection already opened
   'I call my processing procedures
end if

Hope this helps

Chandu

0
 
andyclapCommented:
Well, you've got to remember that you're checking whether your object variable has been set to the object here, not whether the object itself is instatiated.
There's no real way of doing this, apart from some global shared storage somewhere you can write a flag to. Mind you, most of the time you'll want to do this to check instatiation of a module/global scope variable so chandu's comments are perfectly valid (and indeed I use this sort of thing all the time in my code).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now