比较object.Value = Null不会产生预期的结果 [英] Comparing object.Value = Null does not produce expected results

查看:61
本文介绍了比较object.Value = Null不会产生预期的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个令人沮丧的简单问题,我似乎无法解决.

So I have a frustratingly simple issue that I cannot seem to solve.

If Me.Bank_Credit.Value = Null Then
Me.Bank_Credit.Value = 0
End If

基本上,我有一个未绑定的框,用户可以在其中输入数据,然后单击一个按钮.在确认框上单击是"后,未绑定框上的数据将复制到绑定框上.但是,如果用户未输入任何内容,则将创建一个空的绑定字段,这可能会严重打乱沿途的查询.

Basically, I have an unbound box that the user enters data into and then hits a button. After a YES on confirmation box, the data on the unbound box is copied over to the bound box. However, if the user does not enter anything, that in turn creates an empty bound field which can seriously screw up queries down the road.

话虽如此,上面的代码对我来说根本行不通.例如,如果我设置了如果Me.Bank_Credit.Value = 1然后运行它,那么1就会变成2,正如应该发生的那样.但是它只是拒绝为Null甚至是"工作.

That being said, the above code simply will not work for me. If I set, for instance, If Me.Bank_Credit.Value = 1 and then run it, the 1s get turned into 2s, as should happen. But it simply refuses to work for Null or even "".

我很确定有一个解决此问题的简单方法,我只是想不通.

I'm so sure there is a simple solution to this issue, I just can't figure it out.

预先感谢

推荐答案

没有东西等于Null,甚至没有另一个Null.没有任何事物会等于Null,甚至不会等于Null.

Nothing is ever equal to Null, not even another Null. And nothing is ever not equal to Null, not even another Null.

Bank_Credit 为Null时,以下表达式将返回Null ...而不是您期望的True,甚至不是False.

When Bank_Credit is Null, the following expression will return Null ... not True as you might expect, or even False.

Debug.Print (Me.Bank_Credit.Value = Null)

与立即"窗口中出现此结果的原因相同:

It's the same reason for this result in the Immediate window:

Debug.Print Null = Null
Null

使用IsNull()功能.

If IsNull(Me.Bank_Credit.Value) Then

此外,请查看Nz()帮助主题,以了解它是否有用.您可以执行此操作,尽管它并不是对IsNull()的真正改进.但是Nz()对于其他VBA代码可能非常方便.

Also, look at the Nz() help topic to see whether it can be useful. You could do this, although it's not really an improvement over IsNull(). But Nz() can be very convenient for other VBA code.

Me.Bank_Credit = Nz(Me.Bank_Credit, 0)

这篇关于比较object.Value = Null不会产生预期的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆