I\'m doing some work on a legacy application, and my VB6 skills aren\'t that great. I need to check whether a String field has been initialized and set to something other th
use Is Null for stings or is nothing for objects
use len("string") instead of "string"="" because it is faster
Dim s As String
If Not (s Is Null) Then
MsgBox "SET"
if (len(s)>0) then
MsgBox "size > 0"
else
MsgBox "size = 0"
end if
Else
MsgBox "not SET"
End If
regards
VB6 was designed to be easy
Use
If str = "" Then
' uninitialised, null or empty ""
I tried, If str = ""
Then
but it did not work.
The best way to identify if the given string is null is:
If IsNull(str)
Then
' This will work perfectly
'Using VbNullstring can be a very effective way
Dim YourString as string
dim HasValue as boolean
If YourString = vbnullstring then
HasValue = False
Else
HasValue = True
End if
Just adding info to MarkJ's answer. If you are working with a recordset, this
if rs.fields.item("rsField").value = "" then
Will throw a runtime error in case that item is null. You should do this:
if rs.fields.item("rsField").value & "" = "" then
Good luck.
The most optimized and safe way to meet your 3 requirements is as follows:
If LenB(myObj.Str) = 0
Then Debug.Print "String is empty/null/not initialized"
Else Debug.Print "Not Empty"