Skip to content
Advertisement

Excel VBA with SQL : No row returned when Where clause specified

Here below an excerpt of my codes.

This is the SQL command :

   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 & ";"

Where the database located :

   vDataSRC = "C:_projCuTOPsbddGSF_dataWHouse.accdb"

The variable containing the string connection to the DB :

   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"

The wrkSheet for the result :

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

This command Adding QueryTable as a ListObject returns ZERO occurrence because of Where Clause whereas the SQL is OK.

If the Where Clause is removed, it works but I need to add restrictions on Rows.

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

What’s wrong ? I absolutely need to use Where clause.

Advertisement

Answer

The LIKE wildcard behaves differently when running queries between the MS Access GUI (frontend) and any ODBC/OLDEB connection to MS Access (backend). See differences between ANSI-89 and ANSI-92 in MSDN docs.

For ODBC/OLEDB connections as you are doing in Excel, LIKE requires the ANSI-92 wildcard with %:

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

Alternatively, to be compatiable in both use ALIKE (ANSI-Like) in GUI and ODBC/OLEDB:

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

Even better, save the query in MS Access (which is more efficient as the engine caches stats and the best execution plan):

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

Then, reference its name in Excel (avoiding VBA concatenation):

vSQL = "SELECT * FROM mySavedQuery"
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement