Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VbNullString vs. If Not IsNull

Status
Not open for further replies.

tman72

Technical User
Jan 22, 2003
116
US
I'm wondering which method works the best for checking for null string values.

Currently I have:

If Not IsNull(txtmyvalue) Then
'Do something here
Else
'Do something else here
End If

What is the difference if I did this instead:

If (txtmyvalue)<>vbNullString Then
'Do Something here
Else
'Do something else here
End If

Any comments or insights are appreciated.

Thanks.
 
vbNullString is an empty string which isn't a null value.
A general test taking care of null, empty and blank:
If Len(Trim(Nz(txtmyvalue, "") & "")) = 0 Then
MsgBox "txtmyvalue is either Null, Blank or Empty"
End If

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
vbNullString is not Null.

Null does not contain a value.

vbNullString is an allocated string which contains a value, the value just happens to be Null.



Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thank you PHV and Cajun. I wasn't aware vbNullString wasn't null and represents an empty string instead. I've been using the approad of testing for null using:

If IsNull(txtmyvalue) Then
'do this
Else
'do that
End If

So I should be just fine. Good info to know. Someone was trying to tell me to use vbNullString here at work, but it wasn't working correctly and now I understand why.

Thanks again.

Todd
 
It's better to use vbNullString over "" because vbNullString is faster. vbNullString is not the same as "", but using "" requires 1 level of memory access redirection, whereas vbNullString does not. That's where the speed comes from.

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
From my object browser:
Const vbNullString = ""

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
That's really the only way to describe the proper value, but there is a difference in how memory is allocated and referenced, and in what is contained in the symbol table between the following two statements.

str1 = ""
str2 = vbNullString

Essentially the difference is that in the first case, a byte in the heap is allocated for str1, and a pointer to that byte is stored in the symbol table. The value of byte is \0. For str2, the \0 is in the symbol table in lieu of an actual pointer. This means that to resolve the value of str2, only the symbol table is checked. For str1, the symbol table is checked, then the memory location pointed to is checked.

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top