简体   繁体   English

VBA / ACE OLEDB / SQL运行时错误-2147217913标准表达式中的数据类型不匹配

[英]VBA/ACE OLEDB/SQL Run-Time error -2147217913 Data type mismatch in criteria expression

I am running a SQL query from VBA using ACE OLEDB without connecting to Access. 我正在使用ACE OLEDB从VBA运行SQL查询,而未连接到Access。 I do this so I can smash together quickly some Excel tables. 我这样做是为了可以快速将一些Excel表粉碎在一起。 I am not looking to transition this to another platform so would prefer to resolve the current issue. 我不打算将其转换到另一个平台,因此希望解决当前问题。

VBA code works without issue for numerous queries but I am running into the mentioned error for the following query: VBA代码对于许多查询都可以正常工作,但是我遇到了以下查询中提到的错误:

SELECT
    bcr.DialCode,
    bcr.Destination,
    (
        SELECT
            TOP 1 cc.CountryCode
        FROM
            table2 AS cc
        WHERE
            bcr.DialCode LIKE cc.CountryCode+'%'
        ORDER BY LEN(cc.CountryCode) DESC
    ) AS CountryCodes
FROM
    table1 AS bcr

Code adapted from: Longest prefix match in SQL Server 2000 代码改编自: SQL Server 2000中最长的前缀匹配

The intention is to match dialed telephone numbers to the destination country by finding the most inclusive match. 目的是通过查找最全面的匹配项来将拨打的电话号码与目标国家/地区匹配。 Reusing the quoted text sample of input and expected results (adapted to this exercise): 重用引用的带引文的输入样本和预期结果样本(适用于本练习):

bcr.DialCode = '0841234567'

cc.CountryCode = {'084',
                 '0841',
                 '08412'}

Expected output = {'0841234567','Destination 1','08412'}

From searching online seems like maybe I'm using a reserved word in the query. 从网上搜索看来,也许我在查询中使用了保留字。 But I have tested different parts of the query individually and don't get any errors. 但是我已经分别测试了查询的不同部分,并且没有得到任何错误。 For example: 例如:

SELECT
    bcr.DialCode,
    bcr.Destination
FROM
    table1 AS bcr

Works as expected. 可以正常工作。 Also: 也:

SELECT
    TOP 1 cc.CountryCode
FROM
    table2 AS cc
WHERE
    cc.CountryCode LIKE '1'+'%'
ORDER BY LEN(cc.CountryCode) DESC

Has no errors and outputs the expected results. 没有错误,并输出预期结果。

The variable used to pass the query to the ADO connection is dimmed as String but I have tried setting as Variant with no change. 用于将查询传递给ADO连接的变量变暗为String,但我尝试将其设置为Variant,且未做任何更改。

Is there perhaps a difference in the implementation of SQL through ACE for which the "Longest prefix match" query doesn't work? 通过ACE实现的SQL中,“最长前缀匹配”查询不起作用是否有所不同? Any guidance will be greatly appreciated. 任何指导将不胜感激。

First of all, please read my comment to the question. 首先,请阅读我对这个问题的评论。

Secondly, please read these articles: 其次,请阅读以下文章:

I get a message about data type mismatch 我收到有关数据类型不匹配的消息

MS Access 2003: LIKE condition (using wildcards) MS Access 2003:类似于条件(使用通配符)

Finally, if you would like to compare one data to another which StartsWith("SomeString") , you can use something like this: 最后,如果您想将一个数据与StartsWith("SomeString")另一个数据进行比较,则可以使用如下代码:

SELECT
    bcr.DialCode,
    bcr.Destination,
    cc.CountryCode
FROM table1 AS bcr 
    INNER JOIN table2 AS cc 
    ON cc.CountryCode = LEFT(bcr.DialCode, LEN(cc.CountryCode))
ORDER BY LEN(cc.CountryCode) DESC;

For further details, please see: MS Access: Left Function 有关更多详细信息,请参见: MS Access:左功能

Note: Do not forget to use [ ; 注意:不要忘记使用[ ; ] at the end of query. ]在查询末尾。 MS Access database likes it ;) MS Access数据库喜欢它;)

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

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