Where do you want to go today?
  Home
  Who am I?
  Iceland
  OU tips
  Film & DVD
  Festivities
  Credits
  books
  Chili
  Chinese
  How to
  win friends

geek stuff
  spam
  PowerBook love
  Mac hacks
  Windows code
  Visual Basic
  Linux
  My Public Key
diversions...
  Kung fu
  Buffy
  Surfing...

VB Annoyances

I use Visual Basic 6 and VBA at work all the time. Although I couldn't really say I hate the language, there are times when it catches you out for reasons that make little sense.

 

"Huston, we have null problems"

Null values are one of the biggest headaches in the entire VB world. None of the basic data types can store a null (except Variant, which takes virtually anything so has no type safety and suffers from poor performance), so if you're reading from a database or accessing control values, you have to check every value. For instance:

lOrderID = rst("fldOrderID")

will generate an error if fldOrderID is null -- you need to test it first:

If Not IsNull(rst("fldOrderID")) Then
  lOrderID = rst("fldOrderID")
End If

In Access you can use the null-to-zero function, which simplifies things a lot:

lOrderID = Nz(rst("fldOrderID"))

And a little-known fact is that the Format function will convert nulls to zero-length strings:

strOrderID = Format(rst("fldOrderID"))

although it will truncate strings longer than 255 chars -- remember that when you're reading memo fields from Access.


Perhaps the most irritating of all features of nulls is that if you evaluate a true/false expression containing a null, the answer will always be false.

If Me.cboOrderID.Value <> lngOrderID Then
   ' If Me.cboOrderID.Value is null, the condition is ALWAYS false,
  ' and the next line is never reached.

  Me.cboOrderID.Value = lngOrderID
End If

That issue has caught me up more times than I care to mention.