具有SQL的Excel VBA:指定Where子句时不返回任何行

时间:2018-12-19 16:27:12

标签: sql excel excel-vba where-clause listobject

下面是我的代码摘录。

这是SQL命令:

   vSQL = "SELECT OLIN.fk_cCUST as CustomerKey, Sum(nOLINselTota) AS ResultSumV "
   vSQL = vSQL & " FROM (OLIN Inner Join TYOR on OLIN.fk_cTYOR = TYOR.cTYOR) INNER Join MTYP On TYOR.fk_cMTYP = MTYP.cMTYP "
   vSQL = vSQL & " Where OLIN.fk_cOHEAkey Like 'T180*' "
   vSQL = vSQL & " GROUP BY OLIN.fk_cCUST "
   vSQL = vSQL & ";"

数据库所在的位置:

   vDataSRC = "C:\_projCuTOPs\bdd\GSF_dataWHouse.accdb"

包含与数据库的字符串连接的变量:

   vArrSRC = "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;"
   vArrSRC = vArrSRC & "Data Source= " & vDataSRC & ";"
   vArrSRC = vArrSRC & "Mode=Share Deny Write;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";"
   vArrSRC = vArrSRC & "Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;"
   vArrSRC = vArrSRC & "Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;"
   vArrSRC = vArrSRC & "Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False;Jet OLEDB:Bypass UserInfo Validation=False;"
   vArrSRC = vArrSRC & "Jet OLEDB:Limited DB Caching=False;Jet OLEDB:Bypass ChoiceField Validation=False"

结果的wrkSheet:

  ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
  ActiveWorkbook.Sheets(Worksheets.Count).Name = "RawDatas"

此命令将QueryTable添加为ListObject会由于Where子句而SQL正常的情况下返回零。

如果删除了where子句,则可以,但是我需要对行添加限制。

With ActiveSheet.ListObjects.Add(SourceType:=xlSrcExternal, Source:=vArrSRC, Destination:=Range("$A$1")).QueryTable
  '>>
  .CommandType = xlCmdSql
  .CommandText = vSQL
  .RowNumbers = False
  .FillAdjacentFormulas = False
  .PreserveFormatting = True
  .RefreshOnFileOpen = False
  .BackgroundQuery = True
  .RefreshStyle = xlInsertDeleteCells
  .SavePassword = False
  .SaveData = True
  .AdjustColumnWidth = True
  .RefreshPeriod = 0
  .PreserveColumnInfo = True
  '**
  .SourceDataFile = vDataSRC
  '**
  .ListObject.DisplayName = "tbl_SQL_SumTYOR"
  .Refresh BackgroundQuery:=False
  '>>
End With

怎么了?我绝对需要使用Where子句。

1 个答案:

答案 0 :(得分:2)

在MS Access GUI(前端)和与MS Access(后端)的任何ODBC / OLDEB连接之间运行查询时,LIKE通配符的行为有所不同。请参见MSDN docs中的ANSI-89和ANSI-92之间的区别。

对于Excel中的ODBC / OLEDB连接,LIKE要求使用带有%的ANSI-92通配符:

vSQL = vSQL & " Where OLIN.fk_cOHEAkey Like 'T180%' "

或者,为了兼容,在GUI和ODBC / OLEDB中都使用ALIKE(类似于ANSI):

vSQL = vSQL & " Where OLIN.fk_cOHEAkey ALike 'T180%' "

更好的是,将查询保存在MS Access中(由于引擎缓存统计信息和最佳执行计划,效率更高):

SELECT OLIN.fk_cCUST as CustomerKey, 
       SUM(nOLINselTota) AS ResultSumV
FROM (OLIN INNER JOIN TYOR ON OLIN.fk_cTYOR = TYOR.cTYOR) 
INNER Join MTYP On TYOR.fk_cMTYP = MTYP.cMTYP
WHERE OLIN.fk_cOHEAkey LIKE 'T180*'
GROUP BY OLIN.fk_cCUST

然后,在Excel中引用其名称(避免VBA串联):

vSQL = "SELECT * FROM mySavedQuery"