41

Note. Check if the TextBox1 is empty is easy by using TextBox1.Value = "".

But the problem is when the user hit the spacebar, TextBox1 will still recognize it as a value. In such case, my data will appear as an empty cell with 1 space inside. So my question is, is there any method to check TextBox1.value for empty and also not consist of space whether there are 1 or more space? Million thanks to all.

4 Leave Cover
  • 1,248
  • 12
  • 40
  • 83

4 Answers4

86

A common trick is to check like this:

trim(TextBox1.Value & vbnullstring) = vbnullstring

this will work for spaces, empty strings, and genuine null values

Lord Peter
  • 3,433
  • 2
  • 33
  • 33
  • 1
    Dear Sir, this works perfectly! But is there any shorter version of it? Do I have to type or copy/paste for every TextBox? – 4 Leave Cover Jan 01 '13 at 08:33
  • You could use "" instead of vbNullString. (vbNullString is just an internal pointer to a common instance of ""). But I wouldn't worry about the length of the expression - it is stored in tokens, so the length doesn't matter. To save typing you can copy/paste the expression using Ctrl-C/Ctrl-V and change it as needed. – Lord Peter Jan 01 '13 at 08:38
  • 1
    It is working well. I will be using `Trim(TextBox1.Value & "") = ""` from now onwards. Once again thank you :) – 4 Leave Cover Jan 01 '13 at 08:54
  • 1
    Using vbNullString is actually a better option since it requires less byte size to execute. – Doug Coats Apr 24 '16 at 14:31
32

Most terse version I can think of

Len(Trim(TextBox1.Value)) = 0

If you need to do this multiple times, wrap it in a function

Public Function HasContent(text_box as Object) as Boolean
    HasContent = (Len(Trim(text_box.Value)) > 0)
End Function

Usage

If HasContent(TextBox1) Then
    ' ...
pyrospade
  • 7,870
  • 4
  • 36
  • 52
4

Here is the code to check whether value is present or not.

If Trim(textbox1.text) <> "" Then
     'Your code goes here
Else
     'Nothing
End If

I think this will help.

nateAtwork
  • 178
  • 1
  • 4
  • 14
  • Your code is not working Sir. It's not checking for spacebar. By the way, I've marked above answer and I truly appreciate your faster reply too. Once again thank you! – 4 Leave Cover Jan 01 '13 at 08:31
  • This code will remove starting and ending space from value in textbox. The code must work, may I know what is the error you are getting please. – Kanwaljeet Mehta Jan 01 '13 at 09:06
  • Sorry for my misunderstanding. I meant is that I want to check whether the user hit the `spacebar` in the TextBox1. Your code do not check for single and multiple `space`. What I want is to only check for `space` without any other alphanumeric. – 4 Leave Cover Jan 01 '13 at 09:27
  • Then also this code will work. for example if I have entered just a space in textbox and nothing else then this code will consider that textbox contains null. If you want to consider space then you can yes keyascii function. – Kanwaljeet Mehta Jan 01 '13 at 09:29
  • 1
    I've revised your code, I think there should be `=` instead of `<>` in `Trim(textbox1.text) <> ""`. – 4 Leave Cover Jan 01 '13 at 09:39
  • In this case if there is no space in textbox then also code will return true. – Kanwaljeet Mehta Jan 01 '13 at 09:47
-3

You can use the following code to check if a textbox object is null/empty

'Checks if the box is null

If Me.TextBox & "" <> "" Then

        'Enter Code here...

End if
Thomas G
  • 9,886
  • 7
  • 28
  • 41