表达式中缺少值

时间:2017-09-25 13:17:54

标签: excel vba excel-vba

执行下面的代码时,我收到一般的ODBC错误。我还会在错误之前收到一个弹出框,指示为参数1输入参数值。我不知道我的代码中错误引用了哪个范围。

enter image description here

Sub CIABIConnect()
    Dim sConn As String
    Dim sSql As String
    Dim oQt As QueryTable
    Dim part As String
    Dim fromDate As String
    Dim toDate As String
    Dim reportSelect As String
    Dim thisBook As Workbook
    Dim inSheet As Worksheet
    Dim outSheet As Worksheet
    Dim connName As String
    Dim todateString As String
    Dim fromdateString As String
    Dim locString As String
    Dim lastRow As Integer

    Set thisBook = ActiveWorkbook
    Set inSheet = thisBook.Worksheets("Direct Query")
    Set outSheet = thisBook.Worksheets("Direct Data")

    inSheet.Select
    inSheet.Range("B6").Activate

    lastRow = inSheet.UsedRange.Rows.Count

    fromDate = Format(inSheet.Cells(2, 2).Value, "yyyymmdd")
    toDate = Format(inSheet.Cells(3, 2).Value, "yyyymmdd")

    fromdateString = " '" & fromDate & "' "
    todateString = " '" & toDate & "' "

    connName = inSheet.Cells(11, 2).Value

    Call deleteConnections
    outSheet.Cells.Clear

    outSheet.Select

    sConn = "ODBC;DSN=" & connName
    sConn = sConn & ";DBQ=" & connName
    sConn = sConn & ";"

    For i = 2 To lastRow
        Select Case i
            Case 37: sSql = sSql & inSheet.Cells(i, 4).Value & fromdateString
            Case 38: sSql = sSql & inSheet.Cells(i, 4).Value & todateString
            Case Else: sSql = sSql & inSheet.Cells(i, 4).Value
        End Select
    Next i

    Set oQt = ActiveSheet.QueryTables.Add( _
      Connection:=sConn, _
      Destination:=outSheet.Range("a1"), _
      Sql:=sSql)

    oQt.refresh

    CrwOut.Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Columns.AutoFit
End Sub


Sub deleteConnections()
    For Each conn In ThisWorkbook.Connections
        conn.Delete
    Next conn
End Sub

SQL:

SELECT
    'Direct from APIA' as source_nm
    ,itm.merch_cat_desc
    ,case 
         when data.purchase_type='EDGE' then 'Edge' 
        when data.purchase_type='TRADITIONAL' then 'Traditional' 
        else '?' 
    end "EDGE/NON-EDGE"
    ,sum(data.item_qty) as Net_Quantity
    ,sum(data.bams_price) as Net_Cost
    ,sum(data.revenue) as Net_Revenue
    ,sum(DATA.IT_DISC_AMT * SIGN (DATA.ITEM_QTY)) as Net_Discount
    ,sum(  case 
                when data.item_qty > 0 then data.revenue
                else 0
            end
        ) as Gross_Revenue
    ,sum(
            case 
                when data.item_qty > 0 then data.item_qty
                else 0
            end
        ) as Gross_Quantity  
FROM DATA
     --
     JOIN INV.INV_STORE_EXTRACT STORE ON STORE.STORE_ID = DATA.STORE
     --
     JOIN INV.INV_CODA_CHANNEL CODA ON CODA.CODA_DEPT = STORE.CODA_DEPT
     --
     JOIN INV.ITEM_LIST_DRM ITM ON ITM.ITEM_CD = DATA.ITEM_CODE
     --
     LEFT OUTER JOIN INV.PROGRAM_DRM PRGRM
        ON ITM.EQP_ITEM_PROGRAM_RPTKEY = PRGRM.PROGRAM_RPTKEY
--
  WHERE 
data.ord_comp_date between 
**20170426
and
20170427**
--data.acct_prd='201708'
and data.trade_in_ind = 'N'
and CODA.CHANNEL_CATEGORY='DIRECT'
and PRGRM.program_cd in ('APO','CPO','IOT','NEW','USED','VIR')
and ITM.EQP_CLASS_DESC='Wireless Devices'
group by 
itm.merch_cat_desc
,case 
         when data.purchase_type='EDGE' then 'Edge' 
        when data.purchase_type='TRADITIONAL' then 'Traditional' 
        else '?' 
    end

0 个答案:

没有答案