在VB / SQL脚本中引用单元格或UserForm

时间:2014-07-14 14:21:56

标签: vba excel-vba excel

我有一个包含日期范围的VB,SQL脚本......

SQL = SQL & "where dh.actshpdate >= '01-JUN-2013' "
SQL = SQL & "and dh.actshpdate -1 < '31-MAY-2014' "

...我希望能够在我的工作表上包含一个“运行”命令按钮,允许我运行具有不同日期范围的SQL。大概我需要创建一个UserForm - 我没有使用UserForms的经验。在尝试过,我认为这将是一个更容易的解决方案,并使用单元格引用...

SQL = SQL & "where dh.actshpdate >= '" & Sheets("All Data").Range("J3").Value & "' "
SQL = SQL & "and dh.actshpdate -1 < '" & Sheets("All Data").Range("J5").Value & "' "

...但我收到'ORA-01756:引用字符串未正确终止'错误?

Sub SQL()

Application.StatusBar = ">>> >> > Running - Please Wait < << <<<"

    Range("A2:I2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.ClearContents
    Range("A2").Select

Sheets("All Data").Select

Dim SQL As String
Dim orasession As Object
Dim oradatabase As Object
Dim dyprod As Object
Dim Row As Integer

Application.ScreenUpdating = False

Set orasession = CreateObject("OracleInProcServer.XOraSession") 'Create the OraSession Object. ( Oracle )
Set oradatabase = orasession.DbOpenDatabase("sid_cnded", "oes_rep/report", 0&) 'Create the OraDatabase Object by opening a connection to Oracle.

SQL = SQL & "select unique oc.cunr as one, oc.name as two, op.tolgrp as three, th.remarks as four, "
SQL = SQL & "    count(dp.ordnr||''||dp.posnr||''||dp.segnr) as five, "
SQL = SQL & "    sum(op.qty) as six, "
SQL = SQL & "    sum(op.qty)/count(dp.ordnr||''||dp.posnr||''||dp.segnr) as seven, "
SQL = SQL & "    (case when oc.cugrp like 'W1%' then 'UK' else 'AT' end) as eight, "
SQL = SQL & "    (case when sp.pr_typ = 'VD' then 'DVD' else 'CD' end) as nine "
SQL = SQL & "from oes_dhead dh, oes_dpos dp, oes_address ad, oes_opos op, part_description pd, oes_customer oc, oes_tol_head th, scm_prodtyp sp "
SQL = SQL & "where dh.actshpdate >= '01-JUN-2013' "
SQL = SQL & "and dh.actshpdate -1 < '31-MAY-2014' "
SQL = SQL & "and dh.cunr = oc.cunr "
SQL = SQL & "and dh.shpfromloc = 'W' "
SQL = SQL & "and dp.dheadnr = dh.dheadnr "
SQL = SQL & "and ad.key = dh.delnr "
SQL = SQL & "and ad.adr = dh.deladr "
SQL = SQL & "and op.ordnr = dp.ordnr "
SQL = SQL & "and op.posnr = dp.posnr "
SQL = SQL & "and op.prodtyp != 'MTVV' "
SQL = SQL & "and op.ol_typ = 'XX' "
SQL = SQL & "and op.catnr = pd.catnr "
SQL = SQL & "and op.prodtyp = pd.prodtyp "
SQL = SQL & "and op.packtyp = pd.packtyp "
SQL = SQL & "and op.prodtyp = sp.prodtyp "
SQL = SQL & "and sp.pr_typ in ('RX','VD','CD','RD') "
SQL = SQL & "and op.tolgrp = th.tolgrp "
SQL = SQL & "group by oc.cunr, oc.name, op.tolgrp, oc.cugrp, th.remarks, sp.pr_typ "


Set dyprod = oradatabase.CreateDynaset(SQL, 0&)

Sheets("All Data").Select
Row = 2
        If Not dyprod.EOF And Not dyprod.bof Then
                dyprod.movefirst
                       Do Until dyprod.EOF
                            Sheets("All Data").Cells(Row, 1).Select
                            ActiveCell.Value = dyprod.Fields("one").Value
                            Sheets("All Data").Cells(Row, 2).Select
                            ActiveCell.Value = dyprod.Fields("two").Value
                            Sheets("All Data").Cells(Row, 3).Select
                            ActiveCell.Value = dyprod.Fields("three").Value
                            Sheets("All Data").Cells(Row, 4).Select
                            ActiveCell.Value = dyprod.Fields("four").Value
                            Sheets("All Data").Cells(Row, 5).Select
                            ActiveCell.Value = dyprod.Fields("five").Value
                            Sheets("All Data").Cells(Row, 6).Select
                            ActiveCell.Value = dyprod.Fields("six").Value
                            Sheets("All Data").Cells(Row, 7).Select
                            ActiveCell.Value = dyprod.Fields("seven").Value
                            Sheets("All Data").Cells(Row, 8).Select
                            ActiveCell.Value = dyprod.Fields("eight").Value
                            Sheets("All Data").Cells(Row, 9).Select
                            ActiveCell.Value = dyprod.Fields("nine").Value
                            dyprod.movenext
                            Row = Row + 1
                        Loop
                End If

    Cells.Select
    Cells.EntireColumn.AutoFit
    Columns("G:G").Select
    Selection.NumberFormat = "0"
    Range("A2").Select

Application.StatusBar = ">>> >> > Run Complete - Last Run: " & Now() & " < << <<<"

End Sub

有谁知道如何在VB / SQL中引用单元格或添加UserForm日期范围提示符?

由于 SMORF

1 个答案:

答案 0 :(得分:0)

我设法解决了......终于!

SQL = SQL & "where trunc(dh.actshpdate) >= to_date('" & Range("J3").Value & "', 'DD/MM/YYYY') "
SQL = SQL & "and trunc(dh.actshpdate) <= to_date('" & Range("J5").Value & "', 'DD/MM/YYYY') "

我需要使用to_date脚本格式化单元格引用