开发者

Textbox null problem

I have a textbox and a button on my Access form. In the click event of the button i want to see if the textbox is empty, if it is, nothing will be executed. So i use

If Me.textbox.Value = Null Then
    Exit Sub
End if

But it doesn't work... I checked the textbox.value in the execution window and it is Null, but the if clause just doesn't work... Why?

EDIT: @Dimse, I tried "", doesn't work. And also textbox.text = Null, it pops an error telling m开发者_StackOverflowe the textbox is not active.. Very strange.


Null is never equal to anything, not even Null. Use the IsNull() function.

If IsNull(Me.textbox.Value) Then

If you want Me.textbox treated the same when it contains an empty string as when it's Null, concatenate an empty string to it and check the length of the combined string:

If Len(Me.textbox.Value & "") = 0 Then

You could also use the named constant, vbNullString, instead of the string literal, "", for an empty string.

If Len(Me.textbox.Value & vbNullString) = 0 Then

Using the string literal requires VBA to construct that string from scratch each time. With the named constant, VBA only needs to reference it, so should be faster and use less memory. However in many (probably most) cases, the performance advantage with vbNullString would be so minor that you wouldn't notice the difference. Also see the comment below from David-W-Fenton.

For me, the more compelling reason to use vbNullString is that it's instantly recognizable to my aging eyes. Conversely, with the string literal, it takes (a tiny bit) longer for me to confirm that "" is not actually something else ... like " " or "'". The only downside with vbNullString, IMO, is that requires more typing than "".

And finally, although you don't actually need to explicitly reference the Value property (since it's the default property of a text box), I left it in because you had it that way and because I prefer to be explicit with Value, too. :-)


I also apologize for being awaking the dead, but i'm wondering that no one has considered the use of Nz Function (see it @MSDN), very popular in VBA, also usable in Access/SQL and in my opinion the more convenient, concise and powerful solution for nullable values in expressions.


I apologize if I am awaking the dead, but just for completeness sake I am going to give the code for how to test for spaces (visibly 'blank/empty') as well:

If IsNull(Me.Textbox) Or Trim(Me.Textbox) = vbNullString Then
If Trim(Me.Textbox & vbNullString) = vbNullString Then 'Shorter version
If Len(Trim(Me.Textbox) & vbNullString) = 0 Then 'Shortest version

I came here looking for how to handle spaces, empty/ZLS, & NULL's


Null is not equal to another Null ;)

try If isNull(Me.textbox.Value) Then


Expand your sub like so:

If is null(Me.textbox.Value) Or (Me.textbox.Value = "") Then
    Exit Sub
End if


I think you may need to check againt "", the empty string, and not Null.


I couldn't get this to work, since I was using the KeyUP event. So instead, this is what worked for me.

If(Textbox.Text = '') 
  ...

Since Textbox.Value only updates on change event, it wasn't updating on keyup, so Textbox.Text is what is currently in the box.

Summary: Alternatively, Use the .Text property


Just use a second criteria, that will work !!
In this case just a simple word like "check".

If Forms![Basic]![Table.Item] & "check" = "check" Then

MsgBox "Field Empty"

Else

MsgBox "Field Not Empty"

End If


I saw this somewhere and thought I'd share:

If Len(Trim(Me.txtBox.Value)) > 0 Then

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜