访问 VBA:如果表单值 <> NULL 然后运行查询 else,结束 if。- 什么都不做
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3751233/
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
Access VBA: If Form Value <> NULL then run query else, end if. - not doing anything
提问by Andy
I have a piece of code that I've haphazardly written in order to run a query on_click of a form button.
我有一段代码是为了运行表单按钮的 on_click 查询而随意编写的。
Private Sub Command153_Click()
If Forms![form name1]![form name2]!Text143 <> Null Then
DoCmd.OpenQuery "InsertGroup", acNormal, acEdit
Else
End If
End Sub
I haven't written a custom if before in VBA so I think I'm probably just getting this wrong as it is literally not doing anything (No errors or anything, I assume its just not running the query). I know that the query I'm calling in works fine as if I simply get the on_click to run the query without the if, I have no problems whatsoever. I'm also referencing the correct textbox and have tried this both with and without test data in the text box.
我之前没有在 VBA 中编写过自定义 if ,所以我想我可能只是弄错了,因为它实际上没有做任何事情(没有错误或任何事情,我假设它只是没有运行查询)。我知道我正在调用的查询工作正常,就好像我只是让 on_click 在没有 if 的情况下运行查询,我没有任何问题。我还引用了正确的文本框,并在文本框中使用和不使用测试数据都尝试了这一点。
Any help is greatly appreciated!
任何帮助是极大的赞赏!
Regards,
问候,
Andy.
安迪。
采纳答案by gbn
You'd normally cover both null and empty string. The textbox is liable to have a value, not null which you will see if you breakpoint the code to examine the value
您通常会同时涵盖空字符串和空字符串。文本框可能有一个值,而不是 null,如果您对代码进行断点以检查该值,您将看到该值
If Nz(Forms![form name1]![form name2]!Text143,"") <> "" Then ...
回答by Fionnuala
The usual way is to say:
通常的说法是:
If IsNull(Forms![form name1]![form name2]!Text143) Then
I do not believe it is possible to get an empty string in an Access control, unless the value is coming from a database that allows zero-length strings. Furthermore, if you are testing for zero-length strings, you may as well cover all your bases and test for space-filled strings, so you may see in some cases:
我不相信在 Access 控件中获得空字符串是可能的,除非该值来自允许零长度字符串的数据库。此外,如果您正在测试零长度字符串,您也可以覆盖所有基础并测试填充空格的字符串,因此您可能会在某些情况下看到:
If Trim(Forms![form name1]![form name2]!Text143 & vbNullString ) = vbNullString Then
回答by WhatEvil
The above answers seem good and correct but it's worth explicitly noting that a comparison to null as you had written in your first code:
上面的答案似乎很好且正确,但值得注意的是,与您在第一个代码中编写的 null 进行比较:
If Forms![form name1]![form name2]!Text143 <> Null Then....
Will always fail / be evaluated as false, even if your form names are null, as Null has no value to compare to. You must use IsNull(value)or Not Isnull(value)if you want to know if something is null.
即使您的表单名称为空,也会始终失败/被评估为 false,因为 Null 没有可比较的值。您必须使用IsNull(value)orNot Isnull(value)如果您想知道某些内容是否为空。
As an alternative method or to cover more bases you can use nz(VariableName)which returns a zero length string ""(which is also the same as vbNullString) if VariableNameis null. Combined in this way you can check for both a zero length string and a null value in a single operation:
作为替代方法或覆盖更多基数,您可以使用nz(VariableName)它返回零长度字符串""(也与 相同vbNullString)如果VariableName为空。以这种方式组合,您可以在单个操作中检查零长度字符串和空值:
If nz(VariableName) = vbNullString then
Debug.Print "The variable is Null or Zero-length"
End If

