简体   繁体   English

MS Access SQL太少参数:预期2

[英]MS Access SQL Too Few Parameters: Expected 2

In MS Access 2010, I have the following query which randomly orders the data and puts it in a new sheet. 在MS Access 2010中,我有以下查询,该查询随机地对数据进行排序并将其放入新的工作表中。 Before I added in the WHERE, it was working, but now I get an error telling me: 在添加WHERE之前,它已经可以工作了,但是现在我收到一个错误消息:

Too few parameters: Expected 2. 参数太少:预期2。

Does anybody know how I could fix this? 有人知道我该如何解决吗?

sqlQuery = "SELECT [My_Sheet].* " & _
             " INTO My_New_Sheet" & _
             " FROM [My_Sheet] " & _
             " WHERE [Some_Field] = [Some_Possible_Value_For_The_Field] " & _
             " ORDER BY Rnd(-(100000*[Some_Other_Field])*Time())"

Debug.Print sqlQuery
CurrentDb.Execute sqlQuery

Where [Some_Possible_Value_For_The_Field] is comes from [My_Sheet] [Some_Possible_Value_For_The_Field]来自[My_Sheet]

Note that this is Access SQL 请注意,这是Access SQL

sqlQuery = "SELECT [My_Sheet].* " & _
             " INTO My_New_Sheet" & _
             " FROM [My_Sheet] " & _
             " WHERE [Some_Field] = '" & [Some_Possible_Value_For_The_Field] & "'"  & _
             " ORDER BY Rnd(-(100000*" & [Some_Other_Field] & ")*Time())"

Debug.Print sqlQuery
CurrentDb.Execute sqlQuery

When you use a form variable, the value has to be read from outside of the SQL statement. 使用表单变量时,必须从SQL语句外部读取该值。 Hence why we close the statement with double quote, add the field value, and then continue by opening the with a double quotes again. 因此,为什么我们用双引号关闭语句,添加字段值,然后再次用双引号打开,所以继续。

Notice that you need to keep the field qualifiers. 请注意,您需要保留字段限定符。 In this case I assumed your first field was a string which requires the single quote qualifiers and the second variable as an integer which doesn't require qualifiers. 在这种情况下,我假设您的第一个字段是需要单引号限定符的字符串,而第二个变量是不需要限定符的整数。

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

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