【问题标题】:MS Access OpenRecordset and too few parameters issueMS Access OpenRecordset 和参数太少问题
【发布时间】:2018-04-11 14:05:50
【问题描述】:

这是MS Access OpenRedcordset reading wrong string 的后续问题。我确定我有足够的参数和字段声明来将值传递给OpenRecordSet 方法,但我仍然卡住了。

一般说明:

  1. 用户在过滤我的查询的表单上输入开始日期和结束日期 2_Total(单值)My query
  2. 运行将查询导出到 Excel 文件的 VBA 函数

问题参数太少。 Set rst = qry.OpenRecordset(dbOpenDynaset) 上预计会出现 4 个错误

SQL Design View

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

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

以下没有解决问题。我的参数仍然太少。预期 4 错误。

qry.Parameters("BeginDate").Value = [Forms]![Run]![textBeginOrderDate]
qry.Parameters("EndDate").Value = [Forms]![Run]![textendorderdate] 

【问题讨论】:

标签: sql vba ms-access recordset


【解决方案1】:

试试语法:qry.Parameters("BeginDate").Value = Forms![Run]![textBeginOrderDate]

您还可以添加一行 debug.print Forms![Run]![textBeginOrderDate] 以确保它是您期望的值。

https://stackoverflow.com/a/24535025/78522

另一种可能性是修改查询并使用例如
[Forms]![Run]![textBeginOrderDate] 作为条件

【讨论】:

  • 非常感谢您的回复。但我仍然得到同样的错误,参数太少。预计 4.
【解决方案2】:

在 SQL 中将 Where 子句设置为 BeginDateEndDate

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];

【讨论】:

  • 在回答您在此处链接的最后一个问题时,my solution 底部使用了这个确切的 SQL 语句。在上面的帖子中,您声明了参数但从未使用过它们。因此错误。这个想法是将表单控件值绑定到在VBA中的SQL之外的参数。
【解决方案3】:

试试这个:

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]));

【讨论】:

    【解决方案4】:

    在您的查询中尝试此 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];
    

    还有这个 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
    

    【讨论】:

      猜你喜欢
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多