Whats the equivalent of null in VBA?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I have the following code:
Public parent As Node
Set parent = New Node
parent = Null

This doesnt work (Error 438). The error is on the last line, where I try to
assign a null value to parent. How do I assign null values to objects in VBA?

Btw, "Node" is defined as a class module.

Thanks.
 
You might want to use something other than parent - might be reserved.
anyway try:
set parent=Nothing
 
First, take a look at Null in VBA Help.

To dissociate object variables from their objects, you set them to
Nothing:

Set parent = Nothing


For most simple cases, that's not really necessary since the object will
be destroyed when the object variable goes out of scope (i.e., when the
Sub exits).
 
I'll try all of these work arounds. I'll only be able to test if they work
when my application is 'semi-complete'.

Why are all of these work arounds? Is there no such 'null' as there is in
other languages?
 
This is the VBA explanation for the term NullString:

Returns or sets the string displayed in cells that contain null values when
the DisplayNullString property is True. The default value is an empty string
(""). Read/write String.
 
Set someObject = Nothing

is a perfectly normal thing to do *in VBA*: it's not a "workaround".

I suspect that some of the other suggestions you got were from mis-reading
your original question.

Tim
 
They're not really workarounds - just not positive what you are trying to
accomplish. It looks to me like the suggestions using "", vbnullstring, etc
were prompted by you not using a set statement in the example provided (which
is required for objects). It looks like you could be trying to assign null
to a default property of the parent object or you could be trying to destroy
the parent object itself (ie-set it to nothing).
 
It seems that your idea that "work arounds" are required stems from your
misunderstanding the VBA/VB language and what Null means.
To me, outside of a database environment, it does not have a real meaning in
VBA, where you have Nothing for objects and Empty for variants (although you
can use Null with a Variant also) & vbNullString for strings.

Private Sub CommandButton1_Click()
Dim Nullable As Variant

Nullable = "some string"
Nullable = Null
Debug.Print IsNull(Nullable)

Nullable = "some string"
Nullable = Empty
Debug.Print IsNull(Nullable)

Dim NullStr As String
NullStr = "Null String"
NullStr = vbNullString
Debug.Print IsNull(NullStr)

End Sub

NickHK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top