I have just a user form in access 2007 with a text box and a Save command.
MY code:

Private Sub Save_Click()
If School.Value = "" Then
MsgBox ("Text Box is empty")
else
Msgbox(" Not Empty")

End If
End Sub

When the Save is clicked, and the text box is empty, the second statement is executed with message "Not Empty".
Why not first statement is exected first??

Dani AI

Generated

Likely cause: the control isn’t a zero‑length string. Two common situations make a direct = "" test fail: the value is actually Null (not an empty string), or it contains spaces / invisible characters. ’s hunch about blanks is a good lead, and ’s question about what is being saved points to whether the textbox is bound to a field (which can be Null).

A more robust emptiness test converts Null to "" and trims whitespace before checking length. For example:

If Len(Trim(Nz(Me!School, ""))) = 0 Then
    MsgBox "Text box is empty"
Else
    MsgBox "Not Empty"
End If

Why this works: Nz turns Null into a zero‑length string so comparisons won’t misbehave; Trim removes leading/trailing spaces; Len(...)=0 is a clear emptiness check. Use the Me!ControlName or Me.Controls("School") qualifier to avoid ambiguous references.

Debugging tips to confirm the real contents (set a breakpoint and use the Immediate window):

Debug.Print "[" & Me!School & "]"           ' shows visible brackets to reveal hidden spaces
Debug.Print "Len=" & Len(Me!School & "")    ' treats Null as ""
' to see character codes:
Dim i As Integer
For i = 1 To Len(Me!School & "")
    Debug.Print i & ": " & Asc(Mid$(Me!School, i, 1))
Next i

Other notes: .Text is only available when the control has focus; prefer .Value (or Me!... & "" to coerce). If the textbox is bound, check its ControlSource and DefaultValue for unexpected values, and avoid naming controls the same as fields or built‑in keywords.

Recommended Answers

All 2 Replies

Member Avatar for Member #949455

I have just a user form in access 2007 with a text box and a Save command.

What are you trying to save? Are you exporting a doc?

I'm going to go out on a limb here and say that maybe the text box isn't empty. Maybe it just looks empty. It could have blanks in it. Set a breakpoint at the start of the if and look at the value in the debugger.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.