简体   繁体   English

运行时错误“3061”。 参数太少。 预期 3.(访问 2007 和 2010)

[英]Run-time error '3061'. Too few parameters. Expected 3. (Access 2007 and 2010)

I have query like below and I can't add more conditions.我有如下查询,我无法添加更多条件。

Set rst = CurrentDb.OpenRecordset("SELECT [Value1],[Value2] FROM Query1 where DATA_1 >= #" & Me.Tekst22.Value & "# and DATA_2 <= #" & Me.Tekst24.Value & "#", dbOpenSnapshot)

It works great but I need to add two more conditiones :它工作得很好,但我需要添加两个条件:

and ([Value3] = Me.Tekst34 or [Value4] = Me.Tekst34)

If I add code like this如果我添加这样的代码

Set rst = CurrentDb.OpenRecordset("SELECT [Value1],[Value2] FROM Query1 where DATA_1 >= #" & Me.Tekst22.Value & "# and DATA_2 <= #" & Me.Tekst24.Value & "# and ([Value3] = " & Me.Tekst34.Value & " or [Value4] = " & Me.Tekst34.Value & " ", dbOpenSnapshot)

I've got: Run-time error '3061'.我有:运行时错误“3061”。 Too few parameters.参数太少。 Expected 3预计 3

Please help me to correct this error.请帮我纠正这个错误。

additional: Next step after run this Query1 is print values to text file.附加:运行此 Query1 后的下一步是将值打印到文本文件。

    Open "c:\test.txt" For Output As #1
    Set rst = CurrentDb.OpenRecordset("Query1")
    Do While Not rst.EOF

    Print #1, rst!VALUE1 & " " & rst!VALUE2
    rst.MoveNext
    Loop
    rst.Close
    Set rst = Nothing
    Close #1

That is what I need.这就是我需要的。 Run Query1 (now it's with all needed parametrs) and print two values to text file.运行 Query1(现在它具有所有需要的参数)并将两个值打印到文本文件。

You are missing a closing bracket in the SQL, it should finish Me.Tekst34.Value & ")"您在 SQL 中缺少一个右括号,它应该完成Me.Tekst34.Value & ")"

If your Tekst34 is a string, you need to enclose it in quotes in the SQL :如果您的 Tekst34 是一个字符串,则需要在 SQL 中将其括在引号中:

"# and ([Value3] = """ & Me.Tekst34.Value & """ or [Value4] = """ & Me.Tekst34.Value & """)"

(to include " in a string constant one must put "" ). (包括"在一个字符串常量必须放在"" )。

This is what your Code should be, you need to make sure the number of opening parenthesis is equal to the closing ones.这就是您的代码应该是什么,您需要确保左括号的数量等于右括号的数量。 I have taken that the Value3 and Value4 are Text type in the table.我认为 Value3 和 Value4 是表中的 Text 类型。 More importantly;更重要的是; make sure they actually exist in the table.确保它们确实存在于表中。

Set rst = CurrentDb.OpenRecordset("SELECT [Value1],[Value2] FROM Query1 " & _
                                  "WHERE ((DATA_1 >= " & Format(Me.Tekst22, "\#mm\/dd\/yyyy\#") & _
                                  " AND DATA_2 <= " & Format(Me.Tekst24"\#mm\/dd\/yyyy\#") & _
                                  ") AND ([Value3] = " & Chr(34) & Me.Tekst34 & Chr(34) & _
                                  " OR [Value4] = " & Chr(34) & Me.Tekst34.Value & Chr(34) & "))", dbOpenSnapshot)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM