[英]MS Access OpenRecordset and too few parameters issue
This is a follow-up question of MS Access OpenRedcordset reading wrong string . 这是MS Access OpenRedcordset读取错误字符串的后续问题。 I'm certain that I have enough parameters, fields declaration to pass values to
OpenRecordSet
method but I am still stuck. 我确定我有足够的参数,字段声明来将值传递给
OpenRecordSet
方法,但我仍然遇到问题。
General explanation: 一般说明:
Problem : Too few parameters. 问题 : 参数太少。 Expected 4 error on
Set rst = qry.OpenRecordset(dbOpenDynaset)
Set rst = qry.OpenRecordset(dbOpenDynaset)
出现预期的4错误
SQL Design View SQL 设计视图
PARAMETERS [BeginDate] DateTime, [EndDate] DateTime;
SELECT Sum(dbo_SO_SalesHistory.DollarsSold) AS SumOfDollarsSold
FROM dbo_SO_SalesHistory
WHERE (((dbo_SO_SalesHistory.InvoiceDate) Between [Forms]![RUN]![textBeginOrderDate] And [Forms]![RUN]![textendorderdate]));
VBA VBA
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 db As DAO.Database
Dim qry As QueryDef
Dim rst As Recordset
Dim prm As DAO.Parameter
Dim strSQL As String
Set db = CurrentDb
Set xlApp = New Excel.Application
Set xlWB = xlApp.Workbooks.Open("C:\Users\J\Desktop\August 2017.xlsx")
Set xlWS = xlWB.Worksheets("Totals")
xlRow = (xlWS.Columns("K").End(xlDown).Row)
Set qry = db.QueryDefs("2_Total")
qry![BeginDate] = [Forms]![Run]![textBeginOrderDate]
qry![EndDate] = [Forms]![Run]![textendorderdate]
Set rst = qry.OpenRecordset(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
The following didn't solve the issue. 以下内容无法解决问题。 I still have Too few parameters.
我的参数仍然太少。 Expected 4 error.
预期的4错误。
qry.Parameters("BeginDate").Value = [Forms]![Run]![textBeginOrderDate]
qry.Parameters("EndDate").Value = [Forms]![Run]![textendorderdate]
try the syntax: qry.Parameters("BeginDate").Value = Forms![Run]![textBeginOrderDate]
试试语法:
qry.Parameters("BeginDate").Value = Forms![Run]![textBeginOrderDate]
You can also add a line with debug.print Forms![Run]![textBeginOrderDate]
to make sure it is the value you expect. 您还可以使用
debug.print Forms![Run]![textBeginOrderDate]
添加一行,以确保它是您期望的值。
https://stackoverflow.com/a/24535025/78522 https://stackoverflow.com/a/24535025/78522
Another possibility is to modify the query and use for example 另一种可能性是修改查询并使用例如
[Forms]![Run]![textBeginOrderDate]
as a criteria [Forms]![Run]![textBeginOrderDate]
作为条件
Set Where clause to BeginDate
and EndDate
in SQL : 在SQL中将Where子句设置为
BeginDate
和EndDate
:
PARAMETERS [BeginDate] DateTime, [EndDate] DateTime;
SELECT Sum(dbo_SO_SalesHistory.DollarsSold) AS SumOfDollarsSold
FROM dbo_SO_SalesHistory
WHERE dbo_SO_SalesHistory.InvoiceDate Between [BeginDate] And [EndDate];
Try this: 尝试这个:
PARAMETERS
[Forms]![RUN]![textBeginOrderDate] DateTime,
[Forms]![RUN]![textendorderdate] DateTime;
SELECT
Sum(dbo_SO_SalesHistory.DollarsSold) AS SumOfDollarsSold
FROM
dbo_SO_SalesHistory
WHERE
(((dbo_SO_SalesHistory.InvoiceDate)
Between [Forms]![RUN]![textBeginOrderDate]
And [Forms]![RUN]![textendorderdate]));
try this SQL in your query: 在查询中尝试以下SQL:
SELECT
Sum(dbo_so_salesHistory.DollarsSold) AS SumOforderamt,
Min(dbo_so_salesHistory.InvoiceDate) AS MinOforderdate,
Max(dbo_so_salesHistory.InvoiceDate) AS MaxOforderdate,
Count(dbo_so_salesHistory.InvoiceDate) AS CountOforderdate,
forms!run![textBeginOrderDate] AS tbegin,
forms!run![textendorderdate] AS tend
FROM dbo_so_salesHistory
WHERE
(((dbo_so_salesHistory.InvoiceDate) Between
[forms]![run]![textBeginOrderDate] And
[forms]![run]![textendorderdate]))
GROUP BY
forms!run![textBeginOrderDate],
forms!run![textendorderdate];
and this VBA code: 和此VBA代码:
Private Sub cmdRun_Click()
Dim db As DAO.Database
Dim qry As QueryDef
Dim rst As Recordset
Set db = CurrentDb
Set qry = db.QueryDefs("2_Total")
qry.Parameters(0).Value = Forms!Run![textBeginOrderDate]
qry.Parameters(1).Value = Forms!Run![textendorderdate]
Set rst = qry.OpenRecordset(dbOpenDynaset)
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.