ACCESS2013 - 运行时错误'3061'。参数太少。预期1

时间:2017-02-07 08:24:09

标签: vba ms-access access-vba

我在访问时打开表单时遇到错误 此代码应使用该表单的“OnOpen事件”执行。 但我的脚本中出现错误。

Dim ThisDB As DAO.Database
    Set ThisDB = CurrentDb
    Dim d As DAO.Recordset
    Dim q As String
    q = "SELECT [tbl-apartner].[EMail] FROM [tbl-apartner] WHERE [tbl-apartner].[SID] = " & sid2 'sql query
    Set d = ThisDB.OpenRecordset(q, dbOpenDynaset)
    Dim Result As String
    Result = ""
    If d.EOF = False Or d.BOF = False Then 'if-else clause
        d.MoveFirst
        Do While Not d.EOF
            If Result <> "" Then Result = Result & "; "
            Result = Result & d!EMail
            d.MoveNext
          Loop
      End If
    d.Close

错误的一行是:

Set d = ThisDB.OpenRecordset(q, dbOpenDynaset)

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。

The Line

q = "SELECT [tbl-apartner].[EMail] FROM [tbl-apartner] WHERE [tbl-apartner].[SID] = " & sid2

不正确。

q = "SELECT [tbl-apartner].[EMail] FROM [tbl-apartner] WHERE [tbl-apartner].[SID] = " & "'" & sid2 & "'"

区别在于:= " & "'" & sid2 & "'"