Solved

Access VBA: weird result of comparison of 2 variants

Posted on 2014-07-21
6
294 Views
Last Modified: 2014-07-22
In one of my Access 2007 apps, I have a comparison between 2 cells of 2 two-dimensional variant arrays, like this:

If SMD(jSMD, 33) <> sExcelData(nRow, nCol) then ... do something

The second array cell is always populated with data at this point, otherwise this particular if would be skipped. But the first array cell can be empty, indeed the idea was to put the cell content of the second array in the cell of the first one if that one was not yet filled.

To my huge surprise, following the code in debug mode, this if return false when the first array cell is Null. I would never in my wildest dreams have thought that " Null <> 'some string content' " would return anything but true !

Is this:
- a huge VBA bug ?
- intended behavior ?

If the latter, I'd like a rational explanation.

I had to change all my similar if's to >> If Nz(SMD(jSMD, 33), "") <> sExcelData(nRow, nCol) then ...<< to get a propoer behavior.

Thanks for enlightening me on this one.
Bernard
0
Comment
Question by:bthouin
6 Comments
 
LVL 26

Assisted Solution

by:MacroShadow
MacroShadow earned 150 total points
ID: 40208390
No it isn't a bug. It's just the way that Null works.

Read EE's harfang's article Understanding Null, Empty, Blank, #N/A, ZLS, Nothing, Missing

The value Null is said to "propagate", in that (almost) every expression containing Null evaluates to Null. This can be tested in queries, but also in VBA's immediate pane; the expressions below all return Null.

    ? Null + 2
    ? "Hello " + Null
    ? 1 / Null
    ? Len(Null)
    ? Str(Null)

That being said, Null <> 'some string content' will return null not true.
0
 
LVL 84
ID: 40208599
If SMD is an array, then I can't quite figure out the syntax here:

SMD(jSMD, 33) <> sExcelData(nRow, nCol)

If you're pulling values from a multi-dimension array, you typically use syntax like this:

SMD(1,1)

However, your first argument seems to point to the array itself. Maybe I'm just overlooking something obvious, but I can't quite follow this ... perhaps you could enlighten me a bit.

<disclaimer>I have not yet had a full gallon of coffee, so my judgement may be impaired</disclaimer>
0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 150 total points
ID: 40208641
Null is handled differently in different products.   Even SQL Server changed its handling of NULL at one point.

The current thinking is that because something is Null and it's value is unknown, then it is impossible to compare it to anything, even another null.

Jim.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Author Comment

by:bthouin
ID: 40209391
Hi Scott,

Yes, I guess you were in need of coffee, or rather, of a magnifying glass. The first index is called jSMD, not SMD. But the font used for this site makes the letter j nearly disappear between the opening bracket and the S of SMD... :-)

Cheers
Bernard
0
 
LVL 1

Author Comment

by:bthouin
ID: 40209402
Hi Jim

Thanks for this clearer answer. However, failing to be able to compare should, IMHO, result in a runtime error/warning, and not in an arbitrary decision that the result of the comparison is false. If the if cannot operate, why does it return a valid-looking result ?

Bernard
0
 
LVL 57
ID: 40209418
I guess the only way to answer that is sometimes choices need to be made.  If can only return true or false and certainly you would not want to say true, so that leaves only false.

Jim.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now