2017-10-27 1 views
0

이 간단한 문제로 인해 며칠 동안 내 트랙에서 멈추게되었습니다.쿼리에서 폼 참조를 확인하기위한 OpenRecordset

내 프로그램의

일반 설명 : 나는 (날짜 전을 시작) 날짜 범위에 의한 쿼리를 필터링 할 수있는 대화 형 액세스 양식이

  1. 2017년 8월 1일 및 종료 날짜) 전 2017년 8월 31일)
  2. 그럼 내 쿼리는 단일 값을 반환합니다. 예) 12345.23
  3. VBA 기능을 실행합니다.

문제점 : 매개 변수를 명시 적으로 제공 할 수 없습니다. 다음 행은 빨간색으로 강조 표시됩니다.

strSQL = strSQL & "AND [dbo_SO_SalesHistory].[InvoiceDate] Between #"_ 
&[Forms]![RUN]![textBeginOrderDate] & "#And#"_ 
&[Forms]![RUN]![textendorderdate]&"#" 

내 SQL 코드 :

SELECT Sum(dbo_SO_SalesHistory.DollarsSold) AS SumOfDollarsSold 
FROM dbo_SO_SalesHistory 
While (((dbo_SO_SalesHistory.InvoiceDate) Between [Forms]![RUN]![textBeginOrderDate] And [Forms]![RUN]![textendorderdate])); 

전체 코드 :

Option Compare Database 

Option Explicit 
Public Function TRANS2() 

    Dim xlApp As Excel.Application 
    Dim xlWB As Excel.Workbook 
    Dim xlWS As Excel.Worksheet 
    Dim acRng As Variant 
    Dim xlRow As Integer 

    Dim qry As QueryDef 
    Dim rst As Recordset 
    Dim prm As DAO.Parameter 
    Dim strSQL As String 

    Set xlApp = New Excel.Application 
    Set xlWB = xlApp.Workbooks.Open("C:\Users\April.CAROBAPLASTICS\Desktop\August 2017.xlsx") 
    Set xlWS = xlWB.Worksheets("Totals") 

    xlRow = (xlWS.Columns("K").End(xlDown).Row) 
    Set qry = CurrentDb.QueryDefs("2_Total") 

    strSQL = strSQL & "AND [dbo_SO_SalesHistory].[InvoiceDate] Between #"_ 
    & [Forms]![RUN]![textBeginOrderDate] & "# And #"_ 
    & [Forms]![RUN]![textendorderdate] & "#" 
    qry.SQL = strSQL 

    Set rst = CurrentDb.OpenRecordset("2_Total", dbOpenDynaset) 

    Dim c As Integer 
    c = 11 'C is the one that stores column number, in which c=1 means column A, 11 is for column K, 12 for Column L 
    xlRow = xlRow + 11 

    Do Until rst.EOF 
     For Each acRng In rst.Fields 
      xlWS.Cells(xlRow, c).Formula = acRng 
      c = c + 1 
     Next acRng 
     xlRow = xlRow + 1 
     c = 1 
     rst.MoveNext 
     If xlRow > 25 Then GoTo rq_Exit 
    Loop 


rq_Exit: 
    rst.Close 
    Set rst = Nothing 
    Set xlWS = Nothing 
    xlWB.Close acSaveYes 
    Set xlWB = Nothing 
    xlApp.Quit 
    Set xlApp = Nothing 
    Exit Function 

End Function 
+0

'STRSQL = STRSQL & "AND [dbo_SO_SalesHistory] . [InvoiceDate] # "_"사이에 문제가있는 것 같습니다.이 시점까지는 'strSQL'이 할당되지 않습니다. ma king 쿼리 SQL은'Where' 절의 일부입니다. 이것은 "2_Total"의 SQL 코드를 영구적으로 변경합니까? 나는 잘 모르겠다. 또한 SQL 코드가 올바르지 않습니다. 'While'은 'Where' 여야합니다. 'strSQL' 변수에'AND' 전에 공백을 넣고 싶을 수도 있습니다. – MoondogsMaDawg

+0

@ChristopherD. 귀하의 의견에 진심으로 감사드립니다. 그것은 당신의 의견 덕분에 작동합니다. 높게 당신의 도움을 평가했습니다 –

답변

1

당신은 여전히 ​​두 개의 공간이 필요합니다

strSQL = strSQL & "AND [dbo_SO_SalesHistory].[InvoiceDate] Between #" _ 
& [Forms]![RUN]![textBeginOrderDate] & "# And #" _ 
& [Forms]![RUN]![textendorderdate] & "#" 
+0

고맙습니다. 너는 최고야! –

관련 문제