Shannon Shang-I think therefore I am

We came here, you and I, to this place and this profession, to be great, to do great things, and give form to great dreams - and we have

How to ascertain a string variable wether has been initialized

In VB,if a string variable has not been initialized yet, it's value is vbNullString,if a string variable has  been initialized ,but it assigned a zero-length string "" .

In VB, you cannot compare an empty string to a vbNullString because VB equates "" to be equal to vbNullString even though the two are quite different.

However, you can use the undocumented StrPtr function to determine whether the string has been initialized.

 A vbNullString's pointer is, by definition, zero

Example:

Dim str As String



Private Sub Command1_Click()

    If StrPtr(str) = 0 Then

      Debug.Print "str has not been initialized yet"

    Else

      Debug.Print "str has  been initialized"

    End If



End Sub



Private Sub Command2_Click()

  str = "test"

End Sub

Leave a Comment

(required) 

(required) 

(optional)

(required)