• Popular Now

• 12
• 12
• 9
• 10
• 13

Archived

This topic is now archived and is closed to further replies.

Why can't VB do IF statements

This topic is 5497 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Right I'm sure I'm missing something here I have this if statement in VB (I'm actually using VBA in Access 2000 but it probably doesn't matter)
If (Thing <> Null) Then
Exit Function
Else
....

This bit wasn't working so I put a breakpoint on If(Thing <> Null) Then And so the debugger kicked in on that line and I checked what thing was buu holding the cursor over it and it pops up in a tool tip, it said
quote:
Thing = "5"
So it didn't equal Null and so should've gone onto exit function and for some reason it skipped that and went into the else block. Can anybody tell me why VB isn't doing would it should do? [edited by - Monder on March 3, 2003 5:09:32 PM] [edited by - Monder on March 3, 2003 5:10:25 PM]

Share on other sites

If (Thing <> Null) Then Exit FunctionElse....

psuedo code: if thing is anything but null exit function

5 is not null, so it will exit function

Share on other sites
try "" instead of null or vbnull

Share on other sites
Are you adding those parenthesis? You dont want to use those on VB If statements. Not like in Java or something..

If thing <> null then
...
Else

and so on I think works better, could be wrong. Been a bit since ive used VB.

Share on other sites
VB is doing exactly what it should. The question really is, why don''t you understand the proper use of null?

The only variable type that can utilize a Null value is the Variant data type, and it is reserved for those times when the contained value does not exist. If you''ve declared your variable (Thing) as anything else, it cannot be evaluated as null as all variables are initialized upon declaration. If you have declared Thing as a class, then don''t use Null, use Nothing.

-Kirk

Share on other sites
Try using Nothing instead of Null. I didn't even know Null was valid VB syntax.

Side note, you can also use the word NOT instead of <>.

[edited by - FieroAddict on March 3, 2003 5:26:04 PM]

Share on other sites
Check out this help page:

Null, Empty, Nothing, and vbNullString

Scroll down to find that section.

If (Not IsNull(Thing)) Then           Exit FunctionElse

or possibly

If (Not (Thing Is Nothing)) Then           Exit FunctionElse

[edited by - Waverider on March 3, 2003 5:35:39 PM]

Share on other sites
Thanks Waverider, that page had the solution I now use

If IsNull(Thing) Then    ....Else    Exit Function

I''m a C coder so I when I something that''s meant to equal Null (As is said Thing was(not in the example I used)) and when I use something like If Thing = Null or if Thing <> Null and it doesn''t work I get confused

Share on other sites
May I recommend this book:

Programming Visual Basic 6.0

I wasn''t even aware of the Nothing keyword or the Is Nothing syntax until I read it. Nor was I aware of cluster data types like collections and property bags.

Share on other sites
The question to ask, is why VB even let that code compile.

- Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]
[Free C Libraries | zlib ]