Im trying to search the database for the first name. and I get an error " Too few parameters. Expected 2". Could someone help me out. Maybe its very simple and ive gone mad looking at the statement for 5 hrs. Thanks in advance.
VB Code:
sqlsearch = "SELECT Contact.FirstName, Contact.LastName From Contact WHERE (((Contact.FirstName) Like '" & frmMain.txtSearch & "*')) ORDER BY Contact.FirstName, Contact.LastName;"
Set db = OpenDatabase("P:\Contact.mdb")
Set rs = db.OpenRecordset(sqlsearch)
Last edited by dawgfather; Jul 20th, 2005 at 10:32 AM.
sqlsearch = "SELECT Contact.FirstName, Contact.LastName From Contact WHERE (((Contact.FirstName) Like '" & frmMain.txtSearch & "*')) ORDER BY Contact.FirstName, Contact.LastName;"
*** Read the sticky in the DB forum about how to get your question answered quickly!! ***
Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".
k I took out all the parentheses and put the text from the textbox to a string, but i still get the same error.
VB Code:
strtest = frmMain.txtSearch.Text
sqlsearch = "SELECT Contact.FirstName, Contact.LastName From Contact WHERE Contact.FirstName Like 'strtest*' ORDER BY Contact.FirstName, Contact.LastName;"
You need to concatenate the string into the SQL string...
Code:
strtest = frmMain.txtSearch.Text
sqlsearch = "SELECT Contact.FirstName, Contact.LastName From Contact WHERE Contact.FirstName Like '" & strtest & "*' ORDER BY Contact.FirstName, Contact.LastName;"
Debug.Print sqlsearch ' Will print the string so you can see it!
Set db = OpenDatabase("P:\TDContact.mdb")
Set rs = db.OpenRecordset(sqlsearch)
*** Read the sticky in the DB forum about how to get your question answered quickly!! ***
Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".
sqlsearch = "SELECT Contact.FirstName, Contact.LastName From Contact WHERE Contact.FirstName=" + Like strtest & "*" + "ORDER BY Contact.FirstName, Contact.LastName;"
That work?
I've nver used like in an SQL query so I am guessing..again
Last edited by kfcSmitty; Jul 19th, 2005 at 02:09 PM.
Here, maybe the problems right there, and i can't see it.
Code:
Private Sub Form_Load()
Dim strSearchBy As String
Dim strtest As String
Dim db As Database
Dim rs As Recordset
strtest = frmMain.txtSearch.Text
strSearchBy = frmMain.strOption
Select Case strSearchBy
Case "First Name"
sqlsearch = "SELECT Contact.FirstName, Contact.LastName From Contact WHERE Contact.FirstName Like '" & strtest & "*' ORDER BY Contact.FirstName, Contact.LastName;"
Case "Last Name"
sqlsearch = "SELECT Contact.FirstName, Contact.LastName From Contact WHERE (((Contact.LastName) Like '" & frmMain.txtSearch & "*')) ORDER BY Contact.FirstName, Contact.LastName;"
End Select
Debug.Print sqlsearch
Set db = OpenDatabase("P:\Contact.mdb")
Set rs = db.OpenRecordset(sqlsearch)
End Sub
Last edited by dawgfather; Jul 19th, 2005 at 02:19 PM.
lol...sorry about that. the las tmesage had a typo. But I still get the error.
Code:
Private Sub Form_Load()
Dim strSearchBy As String
Dim strtest As String
Dim db As Database
Dim rs As Recordset
strtest = frmMain.txtSearch.Text
strSearchBy = frmMain.strOption
Select Case strSearchBy
Case "First Name"
sqlsearch = "SELECT Contact.FirstName, Contact.LastName From Contact WHERE Contact.FirstName Like '" & strtest & "*' ORDER BY Contact.FirstName, Contact.LastName;"
Case "Last Name"
sqlsearch = "SELECT Contact.FirstName, Contact.LastName From Contact WHERE Contact.LastName Like '" & strtest & "*' ORDER BY Contact.FirstName, Contact.LastName;"
End Select
Debug.Print sqlsearch
Set db = OpenDatabase("P:\Contact.mdb")
Set rs = db.OpenRecordset(sqlsearch)
End Sub
Im trying to search the database for the first name. and I get an error " Too few parameters. Expected 2". Could someone help me out. Maybe its very simple and ive gone mad looking at the statement for 5 hrs. Thanks in advance.
Code:
sqlsearch = "SELECT Contact.FirstName, Contact.LastName From Contact WHERE (((Contact.FirstName) Like '" & frmMain.txtSearch & "*')) ORDER BY Contact.FirstName, Contact.LastName;"
Set db = OpenDatabase("P:\Contact.mdb")
Set rs = db.OpenRecordset(sqlsearch)
The error message you got means that either you are not sending two parameters (fields or filters) that are required, or you have the wrong fieldnames.
I find it weird that it is saying two parameters yet you are only passing one external parameter (the txt box or string as you have it now). I would have said it was the Order By part that was erroring, but they are the same fields as you used in the select fields bit. Are the names correct?
In the last post you are debugging the sql statement. What happens when you copy that from the immediates window and put it into a query in Access (northwind says you are trying the tutorials with access? - guessing).
Szlamany:
.OpenRecordset is DAO... probably why you do not use it!
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
Hey I really appreciate all the help. I finally found the problem, I was pointing to a different database..son#$#@#.
But now I get a "Type mismatch error" on "Set rs = db.OpenRecordset(sqlsearch)".
Someone suggested I run the query in access. Im not too good with Access. Is there a way to copy and paste the SQL Query?
I have added my project with this message.
Last edited by dawgfather; Jul 20th, 2005 at 10:03 AM.
Sorry, I assumed as you were using DAO and northwind that you were using access.
If you are using Sql, there is a query builder/runner in that. You should be able to paste the complete sql statement into it and see if it gives you a more useful error.
Type mismatch is usually where you are trying to filter on a number but you use text. Or visa versa. Infact you are using Access
Set db = OpenDatabase("P:\Contact.mdb")
Open access, open that mdb.
On the main database window, change the tab to Queries
Create a new query (do not use the wizard)
On the choose tables pop up, click close (with no tables)
On the top left on the toolbar, there is a drop down that should say SQL, or go via the view menu and change to Sql view.
Paste your sql statement in.
Press the Red Exclamation mark (toolbar middle) to run.
It should error but may highlight or should you where it doesn't like it.
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...