简体   繁体   English

MS-Access中条件表达式中的数据类型不匹配

[英]Data Type Mismatch in Criteria Expression in MS-Access

I am using Excel-VBA to insert data into a table. 我正在使用Excel-VBA将数据插入表中。 I have a .csv file that was parsed and used to create an insert string. 我有一个.csv文件,该文件已解析并用于创建插入字符串。 I am using it on multiple tables in a row. 我在连续的多个表上使用它。 The first table processes fine, but the second one gives me an error: 第一张表处理正常,但是第二张表给我一个错误:

Run-time error '-2147217913 (8004e07)': Data type mismatch in criteria expression. 运行时错误'-2147217913(8004e07)':条件表达式中的数据类型不匹配。

After researching this, it appears that I am trying to pass data in a bad form, but I'm not seeing where or how, as the first table processed. 经过研究之后,似乎我试图以一种错误的形式传递数据,但是在处理第一个表时,我没有看到在哪里或如何进行。 The final insert string that fails looks like: 失败的最终插入字符串如下所示:

INSERT INTO Table_Name VALUES ('Text_entry', 'Long_Integer_entry', 'Double_entry', '')

EDIT: I just tried another test and found the problem: any field that is a Double or Single Integer that gets passed the null '' causes the error. 编辑:我刚刚尝试了另一项测试并发现了问题:任何通过Double或Single Integer获得null的字段都将导致错误。 How do I pass a null value to these data types? 如何将空值传递给这些数据类型?

In SQL, you can append and update NULL in any data type column including string, numeric, or datetime values. 在SQL中,您可以在任何数据类型列(包括字符串,数字或日期时间值)中附加和更新NULL So simply pass it in your append query. 因此,只需在您的追加查询中传递它即可。 Be sure not to quote it as it is neither a string or numeric value. 请确保不要引用它,因为它既不是字符串也不是数字值。 The NULL entity means an unknown value. NULL实体表示未知值。

INSERT INTO Table_Name VALUES ('Text_entry', 'Long_Integer_entry', 'Double_entry', NULL)

Specifically, your attempted '' is actually a zero-length string (a non-NULL value) which is why it failed to work in a number column. 具体来说,您尝试的''实际上是一个零长度的字符串(非NULL值),这就是为什么它在数字列中无效的原因。

Should you use parameterized queries, also pass VBA's Null to bind to SQL statement: 如果您使用参数化查询,还应传递VBA的Null绑定到SQL语句:

strSQL = "INSERT INTO Table_Name VALUES (?, ?, ?, ?)"

Set cmd = New ADODB.Command

With cmd
     .ActiveConnection = conn
     .CommandText = strSQL
     .CommandType = adCmdText
     .CommandTimeout = 15
End With

' BINDING PARAMETERS
cmd.Parameters.Append _
      cmd.CreateParameter("textparam", adVarChar, adParamInput, 255, "Text_entry")
cmd.Parameters.Append _
      cmd.CreateParameter("longtxtparam", adVarChar, adParamInput, 255, "Long_Integer_entry")
cmd.Parameters.Append _
      cmd.CreateParameter("doubleparam", adDouble, adParamInput, , "Double_entry")
cmd.Parameters.Append _
      cmd.CreateParameter("nullparam", adDouble, adParamInput, , Null)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM