【问题标题】:Run time Error too few parameters Expected 2运行时错误 参数太少 预期 2
【发布时间】:2015-06-09 06:53:42
【问题描述】:

我正在尝试从 Access Query 设计器运行查询,该设计器在 Access 中运行良好,但是当我尝试将语句带到 VBA 时,它给了我以下错误消息:

运行时错误参数太少。预计2。

我已经在即时窗口中打印了该语句并在 Access 中运行它,它正在运行而无需询问参数。我做了一些网络搜索,普遍的共识似乎是在 VBA 中声明它,包括参数 -

Private Sub CmdAppend_Click()
    Dim db1 As Database
    Dim mystr As Recordset2
    Dim UserName As String
    Dim UpdateSQL As String
    Dim SelectIDSQL As String
    Dim checkstr As String

    If Validate_Data = True Then

        UserName = Environ$("Username")

        SelectIDSQL = "Select Distinct ChecklistResults.[StaffID]" _
            & " From ChecklistResults" _
            & " Where (((ChecklistResults.[ClientID])=[Forms]![TeamLeader]![ComClientNotFin])" _
            & " And ((ChecklistResults.[DateofChecklist])=[Forms]![TeamLeader]![ComDateSelect])" _
            & " AND ((ChecklistResults.[ManagerID]) Is Null));"

        Debug.Print SelectIDSQL

        Set db1 = CurrentDb
        Set mystr = db1.OpenRecordset(SelectIDSQL)
        checkstr = mystr!StaffID

        If checkstr <> UserName Then

当我尝试将mystr 设置为记录集时,我收到了上述错误消息。我想我可以按照以下格式获取记录集,但是有没有办法让上述 SQL 语句/分配工作?

Dim qdf1 As DAO.QueryDef

Set qdf1 = db1.QueryDefs("Get_StaffID")
qdf1.Parameters(0) = [Forms]![TeamLeader]![ComClientNotFin]
qdf1.Parameters(1) = [Forms]![TeamLeader]![ComDateSelect]
Set rst1 = qdf1.OpenRecordset(dbOpenDynaset)

【问题讨论】:

    标签: sql ms-access vba


    【解决方案1】:

    当我查看this page 时,我看到了 OpenRecordSet 方法采用两个参数的示例。您收到一条错误消息,指出某些东西需要 2 个参数。尝试改变这个:

    Set mystr = db1.OpenRecordset(SelectIDSQL)
    

    到这里:

    Set mystr = db1.OpenRecordset(SelectIDSQL, dbOpenDynaset)
    

    【讨论】:

    • 谢谢,我试过了,但似乎没有什么不同。我仍然遇到同样的错误。我认为这与 SQL 语句有关,尽管 ChecklistResults 是一个表,它正在从表单中提取信息,这可能是导致错误的原因
    • 尝试更简单的查询。类似select count(*) records from checklistresults where 1 = 2
    【解决方案2】:

    感谢您的输入,我使用以下代码获得了我正在寻找的结果。它使用查询 SelectClientID 来返回完成清单第一阶段的人员的 ID。然后它会检查进行第二次检查的人,如果他们匹配,则返回错误消息。如果两个不同的人完成了它,它会使用 SQL 语句将之前的记录更新为第二个检查者的 ID -

    Private Sub CmdAppend_Click()
    Dim rst1 As Recordset2
    Dim db1 As Database
    Dim mystr As Recordset2
    Dim UserName As String
    Dim UpdateSQL As String
    Dim SelectIDSQL As String
    Dim checkstr As String
    Dim qdf1 As DAO.QueryDef
    
    Set db1 = CurrentDb
    Set qdf1 = db1.QueryDefs("SelectClientID")
    qdf1.Parameters(0) = [Forms]![TeamLeader]![ComClientNotFin]
    qdf1.Parameters(1) = [Forms]![TeamLeader]![ComDateSelect]
    Set rst1 = qdf1.OpenRecordset(dbOpenDynaset)
    
    If Validate_Data = True Then
    
        UserName = Environ$("Username")
    
       UpdateSQL = "UPDATE ChecklistResults" _
        & " SET ChecklistResults.[ManagerID] = '" & UserName & "'" _
        & " WHERE (((ChecklistResults.[ClientID])=[Forms]![TeamLeader]![ComClientNotFin])" _
        & " AND ((ChecklistResults.[DateofChecklist])=[Forms]![TeamLeader]![ComDateSelect])" _
        & " AND ((ChecklistResults.[ManagerID]) Is Null));"
    
    
        checkstr = rst1!StaffID
            If checkstr <> UserName Then
            DoCmd.SetWarnings False
            DoCmd.RunSQL UpdateSQL
            DoCmd.SetWarnings True
            DoCmd.Close
            Else
            MsgBox ("This Checklist was created by you and cannot therefore Checked by you")
            End If
    Else
    Exit Sub
    End If
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2018-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      • 2022-01-07
      相关资源
      最近更新 更多