PDA

View Full Version : Run-Time error '3061' Too Few parameters. expected 2 and 3



jediderek
06-14-2017, 10:06 AM
For the life of me I can't figure this out. Can someone explain what I get this error? Here is the exact info of everything below.


Table Name: Customer


ID
FirstName
LastName


1
Derek
Welton


3
Mark
Welton




Here is the code:


strSQL = "SELECT Customer.ID, Customer.FirstName, Customer.LastName FROM Customer;"
Set rs2 = CurrentDb.OpenRecordset(strSQL)


Debug.Print strSQL


Debug.Print txtFirstName.Value
Debug.Print txtLastName.Value


rs2.Filter = "[FirstName] = " & txtFirstName.Value
Set rs2Filtered = rs2.OpenRecordset


customerName = rs2Filtered.Fields("ID")
Debug.Print customerName
rs2.Close
rs2Filtered.Close



The output of the debugs are:


SELECT Customer.ID, Customer.FirstName, Customer.LastName FROM Customer;
Derek
Welton



This is the line that brings up error: Run-Time error '3061' Too Few parameters. expected 2


Set rs2Filtered = rs2.OpenRecordset



What I was trying to do before is filter 2 items, FirstName and LastName, but tried to debug by doing one filter:


'used for debugging by filtering one item, produces "Expected 2"
rs2.Filter = "[FirstName] = " & txtFirstName.Value


'original code, produces "Expected 3"
rs2.Filter = "[FirstName] = " & txtFirstName.Value & " AND [LastName} = " & txtLastName.Value

The original produces Run-Time error '3061' Too Few parameters. expected 3. The point of this code is to filter down to one record in order to grab the ID value and store it into "customerName". Any help would be appreciated!!

jonh
06-14-2017, 04:00 PM
Names are text. Text requires quotes

rs2.Filter = "[FirstName] = '" & txtFirstName.Value & "'"

jediderek
06-15-2017, 07:16 AM
Thats right! I forgot that the format between text and numbers is different when it comes to filtering. Thank you very much. I appreciate your time going through my code.