简体   繁体   English

MS Access 2013 SQL类型不匹配错误

[英]MS Access 2013 SQL Type mismatch error

I have a table called test with a column of type number (Long Integer) and name CHANGEME. 我有一个名为test的表,该表的列类型为数字(长整数),名称为CHANGEME。 This column has values ranging from null to large integers. 此列的值范围从null到大整数。 I'm attempting to select all null values and update them to 0. I'm doing this in VBA code. 我试图选择所有空值并将其更新为0。我正在VBA代码中执行此操作。

SQL = "UPDATE test SET test.CHANGEME = 0 WHERE test.CHANGEME Is Null"
DoCmd.RunCommand SQL

This produces the type mismatch error. 这会产生类型不匹配错误。

However if I create a query in the user interface and copy and paste the SQL statement and add a ; 但是,如果我在用户界面中创建查询并复制并粘贴SQL语句并添加; at the end and remove the "'s it works perfectly. 最后删除“,它的效果很好。

Any thoughts? 有什么想法吗?

I'm not sure that is how you use DoCmd.RunCommand ; 我不确定那是您使用DoCmd.RunCommand it probably expects a constant, not a string. 它可能期望一个常量,而不是字符串。

Instead I prefer to use something like: 相反,我更喜欢使用类似:

Dim strSQL as String
strSQL = "UPDATE test SET test.CHANGEME = 0 WHERE test.CHANGEME Is Null"
With CurrentDb
    .Execute strSQL, dbFailOnError
    Msgbox .RecordsAffected & " records were updated"
End With

The dbFailOnError is important; dbFailOnError很重要。 if you omit it and there is a syntax error etc you won't be alerted. 如果您忽略它,并且出现语法错误等,则不会发出警报。 Using .Execute as a method is good for in your code as it will not prompt you to confirm your action every time you run it. .Execute作为方法在代码中很有用,因为它不会在每次运行时提示您确认操作。

Hope this helps. 希望这可以帮助。

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

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