In MS Access 2010, I have the following query which randomly orders the data and puts it in a new sheet. Before I added in the WHERE, it was working, but now I get an error telling me:
Too few parameters: Expected 2.
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())"
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. 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.