Text1 The...">

The correct way to check if an unrelated control has a value

A simple scenario: the form and one text box (unbound) Text1.

If "" <> Text1 Then
    MsgBox "Not Empty"
End If

The above code works. The expression ""<> Text1is True if the text field contains characters.

The opposite does not work, regardless of the text field is empty or not:

If "" = Text1 Then  ' or alternatively, False = ("" <> Text1)
     MsgBox "Empty!"
End If

Can you clarify this problem?

+5
source share
3 answers

A text field is usually null, if it does not contain anything, it is not the same as a zero-length string (""). It can often be used best:

If Trim(Text1 & "") = vbNullString
   'Empty

This will be true if the text field contains spaces, a string of zero length, or zero.

+5

, , , , .

'-----------------------------------------------------------------------------'
' True if the argument is Nothing, Null, Empty, Missing or an empty string.   '
'-----------------------------------------------------------------------------'
Public Function IsBlank(arg As Variant) As Boolean
    Select Case VarType(arg)
        Case vbEmpty
            IsBlank = True
        Case vbNull
            IsBlank = True
        Case vbString
            IsBlank = (arg = vbNullString)
        Case vbObject
            IsBlank = (arg Is Nothing)
        Case Else
            IsBlank = IsMissing(arg)
        End Select
End Function

.

+4

I always use the function function Nz (variable, valueIfNull) to test this kind of thing. For example:

 if (Len(Nz(Me.txt1, "")) > 0) then
        'Me.txt1 does not contain a value
 end if

Or, if I want to save the value of a text field in a recordset:

rst!Name = Nz(Me.txtName, "No name given")
+1
source

All Articles