1

I'm trying to read the value of a text box to a string and check if it's empty:

Dim dataFileName as String

dataFileName = Text0.Value

If dataFileName = "" Then
End If

This crashes with "Invalid use of Null" when making the assignment. How can I do this?

2
  • What is "Text0" composed of? Can you show the code where it's obtained/created? Commented Jan 20, 2015 at 12:44
  • It's a TextBox, I just dragged it onto the canvas Commented Jan 20, 2015 at 13:01

1 Answer 1

1

There are one or many ways to test this.

Here are two ways,

Dim dataFileName
dataFileName = Me.Text0

If IsNull(dataFileName) Then
'Or If Len(dataFileName & vbNullString) = 0 Then
    MsgBox "It is a Variant Type, but is Null"
Else
    MsgBox "It is a Variant Type, but is not Null"
End If

The other way is to declare it as String, but make sure you pass a String not Null

Dim dataFileName As String
dataFileName = Me.Text0 & vbNullString
'Or dataFileName = Nz(Me.Text0, vbNullString)

If Len(dataFileName) = 0 Then
    MsgBox "It is a String Type but is a NullString, but NOT NULL"
Else
    MsgBox "It is a String Type, it is not 'empty' persay."
End If
Sign up to request clarification or add additional context in comments.

1 Comment

& is the concatenation operator in VBA. So when you concatenate the Null with a NullString, the value is not Null anymore, but is converted to a String of ZERO length.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.