How to detect an empty text box with VB?

AaronR

New member
Local time
Today, 00:17
Joined
Oct 15, 2008
Messages
3
I have a large text box, and an "add comment" button, that will insert two carraige returns, the name of the person logged in, and the date. (I know I could have designed it differently, but I thought this would make it more user friendly).

What I want it to do, is not insert the returns if there is nothing in the box. I attempted to do this by telling it, if the contents of the box <>"" put in the returns, which works, but if the contents do = "", do not put in the returns. At first I thought that maybe the contents were null, so as a test, I tried changing it to not put returns in if the value = "X"(and remming out the first if then set). But after putting an x in there, it doesn't work. So basically, I can't get an = comparison to return true.

If I have nothing in the box, it won't insert anything when I press the add comments button. So I can assume that Access is detecting the contents of the empty text box as being equal to =. So how can a condition register as a false, but the opposite not be registered as true?


Dim user As String
Dim curdate As String
user = CurrentUser
curdate = Now()
Dim Notes_text As String
If Me.Notes_txt.Value <> "" Then
Notes_text = Me.Notes_txt.Value & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "-" & user & " " & curdate & " writes:"
End If
If Me.Notes_txt.Value = "" Then
Notes_txt = "-" & user & " " & curdate & " writes:"
End If
Me.Notes_txt.Value = Notes_text
 
Try and see if doing this:

Code:
MyString = Me.Notes_txt.Value

If MyString = "" Then

Will work.
 
*Sigh*, after a half our of pure frustration, I figured out what it was. Mis-spelled variable name.
 
Last edited:
Actually, that's expected because memo can have null values (well, technically not literally as the issue centers on the representation, but that's a different topic).

I've always found it to be safe to test for all trivial value. In your case:

Code:
If MyString = "" Or IsNull(MyString) Then

If you want more complete test (at expense of performance), search for IsTrivial function I wrote a while ago that will work for any controls and datatypes.


PS George's point is excellent- you would save by just using one If/Then block than two If/Then, but in the case where you need to test for different conditions, use If/ElseIf/Then block.
 
I have a large text box, and an "add comment" button, that will insert two carraige returns, the name of the person logged in, and the date. (I know I could have designed it differently, but I thought this would make it more user friendly).

What I want it to do, is not insert the returns if there is nothing in the box. I attempted to do this by telling it, if the contents of the box <>"" put in the returns, which works, but if the contents do = "", do not put in the returns. At first I thought that maybe the contents were null, so as a test, I tried changing it to not put returns in if the value = "X"(and remming out the first if then set). But after putting an x in there, it doesn't work. So basically, I can't get an = comparison to return true.

If I have nothing in the box, it won't insert anything when I press the add comments button. So I can assume that Access is detecting the contents of the empty text box as being equal to =. So how can a condition register as a false, but the opposite not be registered as true?


Dim user As String
Dim curdate As String
user = CurrentUser
curdate = Now()
Dim Notes_text As String
If Me.Notes_txt.Value <> "" Then
Notes_text = Me.Notes_txt.Value & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "-" & user & " " & curdate & " writes:"
End If
If Me.Notes_txt.Value = "" Then
Notes_txt = "-" & user & " " & curdate & " writes:"
End If
Me.Notes_txt.Value = Notes_text

The second If Statement is not necessary (although it could become the Else part of the first one).

Code:
    Dim user As String
    Dim curdate As String
    user = CurrentUser
    curdate = Now()
    Dim Notes_text As String
 
    Notes_text = ""
 
    If Me.Notes_text.Value <> "" Then
        Notes_text = Me.Notes_text.Value & vbCRLF & vbCRLF
    End If
 
    Notes_text = Notes_text & "-" & user & " " & curdate & " writes:"
 
    Me.Notes_text.Value = Notes_text

OR

Code:
    Dim user As String
    Dim curdate As String
    user = CurrentUser
    curdate = Now()
    Dim Notes_text As String
 
    Notes_text = ""
 
    If Me.Notes_txt.Value <> "" Then
        Notes_text = Me.Notes_text.Value & vbCRLF & vbCRLF & "-" & user & " " & curdate & " writes:"
    Else 
        Notes_txt = Notes_text & "-" & user & " " & curdate & " writes:"
    End If
 
    Me.Notes_text.Value = Notes_text
 
Last edited:

Users who are viewing this thread

Back
Top Bottom