Checking for Nulls in Fields

  • Thread starter Thread starter Bernie Hunt
  • Start date Start date
B

Bernie Hunt

Is there a cleaner way to check for nulls in fields? Faster, shorter in
code, or just better way?

If Not IsDBNull(drPatients("Address")) Then tmpStrAddress = CStr(drPatients
("Address"))

Thanks,
Bernie
 
I think the datareader has its own IsDbNull method, which you could use as
well.

There are probably other equivalent ways of checking that are about as long.
Is this really a lot of code anyway?

If you wanted it to be shorter, you could write helper functions. Something
like (not tested)

Public Sub AssignValue(ByVal val As Object, ByRef target as String)
If Not IsDBNull(val) Then target = cstr(val)
End Sub

Then you would just call:

AssignValue(drPatients("Address"), tmpStrAddress)
 
I think the datareader has its own IsDbNull method, which you could
use as well.

I came up short on the syntax to get the build in one to work. I couldn't
fine an example of how to call out a field and the IsDBNull method at the
same time.
There are probably other equivalent ways of checking that are about as
long. Is this really a lot of code anyway?

Yes when there are a couple of hundred fields that are being mapped to
new files, hahahaha.
If you wanted it to be shorter, you could write helper functions.
Something like (not tested)

For anyone reading this in the future I came up with this;
Function CheckForNulls(ByVal IncomingString As Object) As String
If Not IsDBNull(IncomingString) Then
Return CStr(IncomingString)
Else
Return ""
End If
End Function

Which adds the advantage of setting the value to an empty string ("") if
it was null. That way you can use in line in assignment statements.

As always,
Marina, Thanks for all your help!!!!

Bernie
 
Bernie,

I assume that your are looking for the AndAlso that is the shortcutted
command for VB.

The last means that

If A IsNot Nothing AndAlso A Not = ""

The second evalutation will not be processed if the first is not true and
therefore you don't get those errors.

I hope that this was something you was looking for.

Cor
 
The sample is not related to anything and can really shorter. It is just to
show the AndAlso.

Cor
 
Back
Top