2014-07-14 3 views
0

...내가 날짜 범위를 포함하는 VB, SQL 스크립트를

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

... 나는 'RUN'명령을 포함 할 수 있도록하고 싶습니다 버튼을 사용하여 다른 날짜 범위의 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에서 셀을 참조하거나 사용자 정의 날짜 범위 프롬프트를 추가하는 방법을 알고 있습니까?

감사 SMORF

답변

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') " 

는 I는 TO_DATE 스크립트

를 사용하여 셀 참조 포맷 neded
관련 문제