使用 vba 将空值插入到访问中的日期字段

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

Insert null value to date field in access using vba

vbadateaccess-vba

提问by Cherry

Am trying to insert date from a table to another table using vba in access, But one of the date values from the table is blank. So while inserting the date to new table it shows Invalid use of null exceptionRun-time error 94.

我试图在访问中使用 vba 将日期从一个表插入到另一个表,但表中的一个日期值是空白的。因此,在将日期插入新表时,它显示Invalid use of null 异常运行时错误 94

采纳答案by Cherry

I think i figure it out , declare the variable to string. Then check whether value from the table is empty, then assign null to it.Other wise assign the value from the table.

我想我想通了,将变量声明为字符串。然后检查表中的值是否为空,然后为其分配空值。否则从表中分配值。

for example

例如

   Dim newdate As String

Check whether the fetched values is null or not

检查获取的值是否为空

   If IsNull(rst![value]) Then
            newdate = "null"
   Else
           newdate = rst![value]
   End If     

回答by Chris Patterson

If writing DAO code to clear a Date field, I found that I had to use "Empty". Null and "" won't work. So for field dtmDelivery (type Date), I had to use the following. strDelivery is just a string with the date in it.

如果编写 DAO 代码来清除日期字段,我发现我必须使用“Empty”。Null 和 "" 不起作用。因此,对于字段 dtmDelivery(日期类型),我必须使用以下内容。strDelivery 只是一个带有日期的字符串。

Set rst = dbs.OpenRecordset("tblSomething", dbOpenDynaset)
If (strDelivery = "") Then                      
    rst!dtmDelivery = Empty    
Else 
    rst!dtmDelivery = strDelivery
End If

rst.Update

回答by cboden

you can use the NZ() function to define a value that should be used instead of NULL. Internally a date is a float value where the number represents the days between 01.01.1900 and the date that should be stored. The part behind the decimal point represents hours/minutes (.25 for example is 6 AM). So you can use the NZ() funtion to replace NULL bei 0 (what would be interpreted as 01.01.1900).

您可以使用 NZ() 函数来定义应该使用的值而不是 NULL。在内部,日期是一个浮点值,其中数字表示 01.01.1900 和应该存储的日期之间的天数。小数点后面的部分代表小时/分钟(例如 0.25 是早上 6 点)。因此,您可以使用 NZ() 函数来替换 NULL bei 0(将被解释为 01.01.1900)。

Another solution would be to configure the target table in a way that it allows NULL values. You can do this easily in the design view of the table.

另一种解决方案是以允许 NULL 值的方式配置目标表。您可以在表的设计视图中轻松完成此操作。

回答by Peter Mali

I encountered this problem and solved it this way.

我遇到了这个问题并通过这种方式解决了它。

SQL = "update mytable set mydatefield = '" & txtdate & "'"

On the txtdateon your form, put a date format to ensure date is either a real date or just blank.

txtdate您的表单上,输入日期格式以确保日期是真实日期或只是空白。