Vba OpenRecordset解析查询中的表单引用
这个简单的问题让我在这几天里停滞不前 我的计划的一般说明:Vba OpenRecordset解析查询中的表单引用,vba,forms,ms-access,recordset,Vba,Forms,Ms Access,Recordset,这个简单的问题让我在这几天里停滞不前 我的计划的一般说明: 我有一个交互式访问表单,可以按日期范围(开始日期ex)过滤查询 2017年8月1日和结束日期)ex 8/31/2017) 然后我的查询返回一个 值:ex)12345.23 运行VBA函数 问题:我无法明确提供参数。下一行以红色突出显示 strSQL = strSQL & "AND [dbo_SO_SalesHistory].[InvoiceDate] Between #"_ &[Forms]![RUN]![textBeg
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
您仍然需要两个空间:
strSQL = strSQL & "AND [dbo_SO_SalesHistory].[InvoiceDate] Between #" _
& [Forms]![RUN]![textBeginOrderDate] & "# And #" _
& [Forms]![RUN]![textendorderdate] & "#"
strSQL=strSQL&”和[dbo_SO_SalesHistory].[InvoiceDate]介于#”之间似乎有问题,strSQL
直到此时才被赋值,使得查询sql只是Where
子句的一部分。这是否也会永久性地改变“2_Total”的SQL代码?我不确定。而且,您的sql代码看起来不正确,而
应该是Where
。您可能还想在strSQL
变量中的和前面加一个空格。@ChristopherD。非常感谢您的评论。多亏了你的评论,它才起作用。非常感谢你的帮助