我有一个非常奇怪的问题。我有一个Access DB(2013 64位.accdb) 基于其他查询的那些查询以及#34; Q_ManifestedByStatus"
所有查询都在Access中按预期工作,因此这里没有问题。
问题是我使用ADODB连接将数据提取到Excel(2013 64位)。其中一个查询有一个" WHERE"条款[PO No] LIKE' 8 *'。 当sql代码嵌入到VBA中时,它工作正常,当sql被来自访问的查询名称替换时,它不会。
当我从访问中删除WHERE子句时,它可以正常工作...... 当我将LIKE子句更改为NOT LIKE时,它可以正常工作......
欢迎任何建议!
请参阅以下代码。
访问SQL:
SELECT
Int([Collection date]) AS [Date],
Sum(IIf([Status]="Early",1,0)) AS Early,
Sum(IIf([Status]="On Time",1,0)) AS [On Time],
Sum(IIf([Status]="Late",1,0)) AS Late,
Sum(IIf([Status]="Not Manifested",1,0)) AS [Not Manifested],
[Early]+[on time]+[late]+[not manifested] AS [Sum],
Round([Early]/[Sum],2)*100 & "%" AS [Early%],
Round([On Time]/[Sum],2)*100 & "%" AS [On Time%],
Round([Late]/[Sum],2)*100 & "%" AS [Late%],
Round([Not Manifested]/[Sum],2)*100 & "%" AS [Not Manifested%]
FROM Q_ManifestedByStatus
WHERE [PO No] Like '8*' Or [PO No] Like '9*'
GROUP BY Int([Collection date]);
VBA - SQL Embedded - WORKS
Sub tt()
Dim objAdoCon As Object
Dim rst As Object
Dim provider As String
Dim sql As String
sql = "SELECT Int([Collection date]) AS [Date], Sum(IIf([Status]=""Early"",1,0)) AS Early," & _
"Sum(IIf([Status]=""On Time"",1,0)) AS [On Time], Sum(IIf([Status]=""Late"",1,0)) AS Late," & _
"Sum(IIf([Status]=""Not Manifested"",1,0)) AS [Not Manifested], [Early]+[on time]+[late]+[not manifested] AS [Sum]," & _
"Round([Early]/[Sum],2)*100 & ""%"" AS [Early%], Round([On Time]/[Sum],2)*100 & ""%"" AS [On Time%], Round([Late]/[Sum],2)*100 & ""%"" AS [Late%]," & _
"Round([Not Manifested]/[Sum],2)*100 & ""%"" AS [Not Manifested%] FROM Q_ManifestedByStatus WHERE [PO No] NOT Like '8*' GROUP BY Int([Collection date]);"
Set objAdoCon = CreateObject("ADODB.Connection")
Set rst = CreateObject("ADODB.Recordset")
If Application.Version = 15# Then provider = "Provider = Microsoft.ACE.oledb.12.0;" Else provider = "Provider = Microsoft.ACE.oledb.4.0;"
objAdoCon.Open provider & "Data Source = C:\Users\pplusa\Documents\SupplierCompliace.accdb"
'Get 81 POs data
'Daily
Worksheets("81 POs Daily").Select
Cells.Delete
rst.Open sql, objAdoCon
Range("A2").CopyFromRecordset rst
For i = 0 To rst.Fields.Count - 1
Range(Cells(1, i + 1).Address).Value = rst.Fields.Item(i).Name
Next i
Cells.EntireColumn.AutoFit
rst.Close
End Sub
VBA - 按查询名称 - 不工作
Sub tt()
Dim objAdoCon As Object
Dim rst As Object
Dim provider As String
Set objAdoCon = CreateObject("ADODB.Connection")
Set rst = CreateObject("ADODB.Recordset")
If Application.Version = 15# Then provider = "Provider = Microsoft.ACE.oledb.12.0;" Else provider = "Provider = Microsoft.ACE.oledb.4.0;"
objAdoCon.Open provider & "Data Source = C:\Users\pplusa\Documents\SupplierCompliace.accdb"
'Get 81 POs data
'Daily
Worksheets("81 POs Daily").Select
Cells.Delete
rst.Open "Q_ManifestedByDate81", objAdoCon
Range("A2").CopyFromRecordset rst
For i = 0 To rst.Fields.Count - 1
Range(Cells(1, i + 1).Address).Value = rst.Fields.Item(i).Name
Next i
Cells.EntireColumn.AutoFit
rst.Close
End Sub
答案 0 :(得分:2)
SQL不会将*
用于多字符匹配。它使用%
。
答案 1 :(得分:0)
这是在MS Access中运行LIKE
与通过OLEDB / ODBC运行外部之间的已知问题。召回MSAccess既是GUI程序又是后端数据库。
MS Access通过其GUI .exe接口默认使用ANSI-89语法,*
作为通配符运算符。通过OLEDB进行的MS Access与Excel一样使用ANSI-92语法,%
作为通配符运算符。请参阅有关Access wildcard character reference的MS Office支持文档。
考虑两种选择中的一种,使两者保持一致:
ALIKE
运营商访问查询中的LIKE
运营商{/ 1}}。%
将遵守LIKE
而不是%
。