ADODB记录集SQL在Excel VBA中返回错误

时间:2017-04-27 18:13:46

标签: sql vba csv ado

当我到达试图打开记录集的语句时,以下代码产生错误(对象'_Recordset'的方法'打开'失败):

Set objClubSalesSourceConn = CreateObject("ADODB.COnnection")
objClubSalesSourceConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=" & strDirectory & ";" & _
          "Extended Properties=""text;HDR=YES;FMT=Delimited"""
Set rsClubOrders = CreateObject("ADODB.Recordset")

strSQL = "SELECT"
strSQL = strSQL & vbCrLf & "    ClubSales.[Order Number] AS OrderNum,"
strSQL = strSQL & vbCrLf & "    ClubSales.[Submitted Date] AS SaleDate,"
strSQL = strSQL & vbCrLf & "    ClubSales.[Product SKU] AS SKU,"
strSQL = strSQL & vbCrLf & "    ClubSales.[Product Name] AS ItemDesc,"
strSQL = strSQL & vbCrLf & "    ClubSales.[Ext Item Price] AS SaleAmt,"
strSQL = strSQL & vbCrLf & "    ClubSales.[Ext Item Shipping] AS ShipAmt,"
strSQL = strSQL & vbCrLf & "    CASE WHEN ISNULL(ClubSales.[Ship Date])"
strSQL = strSQL & vbCrLf & "        THEN 1"
strSQL = strSQL & vbCrLf & "        ELSE 0 END AS ShipDateNull,"
strSQL = strSQL & vbCrLf & "    ClubSales.[Ship Date] AS ShipDate,"
strSQL = strSQL & vbCrLf & "    ClubSales.[Pickup Date] AS PickupDate,"
strSQL = strSQL & vbCrLf & "    ClubSales.[Quantity Sold] * ClubSales.[Cost Of Goods] AS COGSAmt"
strSQL = strSQL & vbCrLf & "FROM"
strSQL = strSQL & vbCrLf & "    ClubSalesSource.csv AS ClubSales"

rsClubOrders.Open strSQL, objClubSalesSourceConn, adOpenDynamic*

问题在于SQL中的CASE WHEN语句。如果我把它留下来,记录集打开就可以了。我在Excel VBA中,查询csv文件。我需要知道如何解决这个问题。我也尝试使用像我在Access中完成的IIF功能一样失败。

1 个答案:

答案 0 :(得分:0)

strSQL = "SELECT" & _
"    ClubSales.[Order Number] AS OrderNum," & _
"    ClubSales.[Submitted Date] AS SaleDate," & _
"    ClubSales.[Product SKU] AS SKU," & _
"    ClubSales.[Product Name] AS ItemDesc," & _
"    ClubSales.[Ext Item Price] AS SaleAmt," & _
"    ClubSales.[Ext Item Shipping] AS ShipAmt," & _
"    CASE WHEN ISNULL(ClubSales.[Ship Date])" & _
"        THEN 1" & _
"        ELSE 0 END AS ShipDateNull," & _
"    ClubSales.[Ship Date] AS ShipDate," & _
"    ClubSales.[Pickup Date] AS PickupDate," & _
"    ClubSales.[Quantity Sold] * ClubSales.[Cost Of Goods] AS COGSAmt"  & _
" FROM" & _
"    ClubSalesSource.csv AS ClubSales"