访问 VBA - 为声明为 long 的函数返回某种空白/空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11831973/
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-11 17:11:15  来源:igfitidea点击:

Access VBA - Returning some sort of blank/null for function declared as long

vbams-accessms-access-2007access-vba

提问by GeoffDS

Question:I want to use a VBA function in Access that is declared as type Long. I want to return an integer between 0 and 35 some of the time, but I also want to be able to return a blank or null or something like that most of the time. Is there a way to do this? What I have tried (variable = "" or Set variable = Nothing) just calls an error. This will be used in a query and will give the value for one column. I want that column to be type Long. If such a thing is not possible, I guess that is all I need to know, as I already have a different but less desirable solution. Thanks for any help.

问题:我想在 Access 中使用一个声明为 Long 类型的 VBA 函数。有时我想返回 0 到 35 之间的整数,但我也希望在大多数情况下能够返回空白或空值或类似的东西。有没有办法做到这一点?我尝试过的(变量 = "" 或设置变量 = 无)只是调用了一个错误。这将在查询中使用,并将给出一列的值。我希望该列的类型为 Long。如果这样的事情是不可能的,我想这就是我需要知道的全部,因为我已经有了一个不同但不太理想的解决方案。谢谢你的帮助。

Update:Of course, right after asking the question, I figured out a good solution. If I just do Range("whatever").Value = Range("whatever").Value in Excel, then it changes a left aligned 20 to a right aligned 20, at which time it is recognized by the pivot table as a number (though when I just convert the cell type to a number in Excel, it is not recognized as a number in the pivot table). So, I am deleting the background because it is not necessary. I am still interested to know if you can return some sort of blank or null for a function declared as long. Thanks

更新:当然,在提出问题后,我想出了一个很好的解决方案。如果我只是在 Excel 中执行 Range("whatever").Value = Range("whatever").Value,那么它将左对齐的 20 更改为右对齐的 20,此时数据透视表将其识别为数字(虽然当我只是在 Excel 中将单元格类型转换为数字时,它在数据透视表中不被识别为数字)。所以,我正在删除背景,因为它没有必要。我仍然很想知道您是否可以为声明为 long 的函数返回某种空白或空值。谢谢

回答by SeanC

Nullcan only be returned from a Variantfunction.
Nothingcan only be returned from an Objectfunction.

Null只能从Variant函数返回。
Nothing只能从Object函数返回。

All others, you are restricted to returning a variable of the type defined as the return value of your function.
If you do not set a value, then it returns the default value
A numeric variable is initialized to zero
A variable length string is initialized to a zero-length string ("")
A fixed length string is filled with the ASCII code 0.
A date/time variable is initialized to zero

所有其他人,您只能返回定义为函数返回值的类型的变量。
如果不设置值,则返回默认值
数值变量初始化为零
可变长度字符串初始化为零长度字符串 ("")
固定长度字符串用 ASCII 码 0 填充
。日期/时间变量初始化为零

回答by Doug Glancy

Since an unitialized long has a value of 0, the answer is "no." A function declared as long cannot return null or blank. If zero wasn't a valid return value for your function, you could use it a "null" equivalent, but you already knew that :)

由于未初始化的 long 值为 0,因此答案为“否”。声明为 long 的函数不能返回 null 或空白。如果零不是您的函数的有效返回值,您可以将其用作“null”等价物,但您已经知道:)