I have some weird behavior that has me perplexed. I created a function in vb (Microsoft Word Macro version of VB) which looks like this:
Public Function isitnullorempty(newstring As String) As Boolean
If IsNull(newstring) Or IsEmpty(newstring) Or newstring = "" Then
isitnullorempty = False
Else
isitnullorempty = True
End If
End Function
It seems to work as expected if I create a variable and then set it to null, “”, or leaving it empty. I manipulate the variable and then do MSGBOX isitnullorempty(variable) and it echos what I expect. It doesn’t behave the same way when I put a recordset inside the function however. I’ve even tried to make a variable set it to the record set and then test it but that doesn’t work either. If I try to invoke the following
'ADO query code here (This is an ODBC connection)
DO While (Not oRecordset.EOF)
MsgBox isitnullorempty(oRecordset.Fields("CustomerTypeRefFullName"))
oRecordset.MoveNext
Loop
This yields the error: Invalid use of Null.
So two fold question.
Is there already a vb function that exists which does what I want?
Why does my code behave this way?
Weirder yet is that if I do the test individually outside of the function, it works.
Temporary Solution
It writes more code than it needs to but since I can’t seem to call the function with the recordset value I will just individually check each record according to my needs like this:
If IsNull(oRecordset.Fields("CustomerTypeRefFullName").Value) Or IsEmpty(oRecordset.Fields("CustomerTypeRefFullName").Value) Or oRecordset.Fields("CustomerTypeRefFullName").Value = "" Then
MsgBox "potato"
End If
The answer is if you want to pass a null to a function’s parameter that parameter needs to be set as variant.
Public Function isitnullorempty(newstring As Variant) As Boolean