如果 [ComboBox] 是 VBA/Access 2007 中的 Null 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3782787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:48:42  来源:igfitidea点击:

If [ComboBox] Is Null Statement in VBA/Access 2007

ms-accessformsvbams-access-2007access-vba

提问by myClone

I have a form with one ComboBox (YearToBeBuilt) and two textBox fields (Costand YearofExpenditureCost). All controls are linked to a main table and the table is updated once the selections/entries have been made on the form.

我有一个包含一个 ComboBox ( YearToBeBuilt) 和两个 textBox 字段 (CostYearofExpenditureCost)的表单。所有控件都链接到一个主表,一旦在表单上进行了选择/条目,该表就会更新。

I have written a procedure in VB called ReCalcIt()which performs the following procedure when called:

我在 VB 中编写了一个调用的过程,ReCalcIt()它在调用时执行以下过程:

Private Sub ReCalcIt()
If Me.YearToBeBuilt = "" Then
    Me.YearofExpenditureCost = Me.Cost
Else
    Me.YearofExpenditureCost = Me.Cost * (1 + 0.031) ^ (Me.YearToBeBuilt - 2010)
End If
End Sub

When I wrote this I thoughtthat this would do the following:

当我写这篇文章时,我认为这会做以下事情:

If the ComboBox [YearToBeBuilt]is blank (e.g.-no selection made) then the textbox [YearOfExpenditureCost]will return the value of the TextBox [Cost]. Else, the calculation for YearofExpenditureCostis performed.

如果 ComboBox[YearToBeBuilt]为空(例如,未进行选择),则文本框[YearOfExpenditureCost]将返回 TextBox 的值[Cost]。否则,YearofExpenditureCost执行的计算。

But this is not working the way it should

但这并不像它应该的那样工作

What am I doing wrong? I am a VBA n00b so perhaps my syntax is incorrect?

我究竟做错了什么?我是 VBA n00b,所以也许我的语法不正确?

回答by CristiC

Try it with

试试看

If Len(Me.YearToBeBuilt & vbNullString) = 0

So the code will look like this:

所以代码看起来像这样:

Private Sub ReCalcIt()
If Len(Me.YearToBeBuilt & vbNullString) = 0 Then
    Me.YearofExpenditureCost = Me.Cost
Else
    Me.YearofExpenditureCost = Me.Cost * (1 + 0.031) ^ (Me.YearToBeBuilt - 2010)
End If
End Sub

回答by Dr Xorile

It seems to me that this might be dependent on the format of the combo box? If it's a number, then

在我看来,这可能取决于组合框的格式?如果是数字,那么

If Len(Me.YearToBeBuilt & vbNullString) = 0

doesn't work. Because the Me.YearToBeBuilt has a value of 0, and so the above returns 1.

不起作用。因为 Me.YearToBeBuilt 的值为 0,所以上面的返回 1。

I'm wondering if there is a more robust method? Maybe

我想知道是否有更强大的方法?也许

Me.YearToBeBuilt.ListIndex = -1