Results 1 to 19 of 19

Thread: [Resolved]SQL Statement error

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2004
    Posts
    48

    Resolved [Resolved]SQL Statement error

    Hi

    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:
    1. sqlsearch = "SELECT Contact.FirstName, Contact.LastName From Contact WHERE (((Contact.FirstName) Like '" & frmMain.txtSearch & "*')) ORDER BY Contact.FirstName, Contact.LastName;"
    2.  
    3. Set db = OpenDatabase("P:\Contact.mdb")
    4. Set rs = db.OpenRecordset(sqlsearch)
    Last edited by dawgfather; Jul 20th, 2005 at 10:32 AM.

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Statement error

    Still too many "-quotes...

    Code:
    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".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2004
    Posts
    48

    Re: SQL Statement error

    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:
    1. strtest = frmMain.txtSearch.Text
    2.  
    3. sqlsearch = "SELECT Contact.FirstName, Contact.LastName From Contact WHERE Contact.FirstName Like 'strtest*' ORDER BY Contact.FirstName, Contact.LastName;"
    4.  
    5. Set db = OpenDatabase("P:\TDContact.mdb")
    6. Set rs = db.OpenRecordset(sqlsearch)

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Statement error

    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".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: SQL Statement error

    VB Code:
    1. 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.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Statement error

    kfcSmitty - LIKE is an operator just like =

    The difference is that it takes a wildcard.

    SELECT * FROM SOMETABLE WHERE NAMECOLUMN LIKE 'SMITH*';

    This will return all the rows with SMITH as the starting characters in a column called NAMECOLUMN.

    ACCESS uses * for the wildcard.

    MS SQL Server uses % for the wildcard.

    In MS SQL Server you would say:

    SELECT * FROM SOMETABLE WHERE NAMECOLUMN LIKE '%JOHN%'

    That would return all the rows with JOHN anywhere in the NAMECOLUMN.

    *** 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".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: SQL Statement error

    ahh, i knew about wildcards, but I assumed the select statement needed the "=" and then the Like for the comparison.

    Thanks for clearing that up

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2004
    Posts
    48

    Re: SQL Statement error

    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.

  9. #9
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: SQL Statement error

    dawg, is strtest a variable? if so, youre treating it like a string. szlamany's example above shows how to use it as a variable

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2004
    Posts
    48

    Re: SQL Statement error

    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

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Statement error

    Where exactly is the error occuring?

    It seems to me that the SQL statement is working.

    You can test that by filling sqlSearch with "SELECT 'TEST','HELLO';" and seeing if that query runs.

    *** 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".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12

    Thread Starter
    Member
    Join Date
    Mar 2004
    Posts
    48

    Re: SQL Statement error

    the error occurs here.

    Code:
     Set rs = db.OpenRecordset(sqlsearch)

  13. #13
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Statement error

    We don't use .OPENRECORDSET ...

    But the help/examples I get in VB show this...

    Set rstTemp = dbsNorthwind.OpenRecordset( _
    "SELECT * FROM Employees", dbOpenDynaset, dbReadOnly)
    Are you missing some parameters from the call??

    *** 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".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  14. #14

    Thread Starter
    Member
    Join Date
    Mar 2004
    Posts
    48

    Re: SQL Statement error

    The call statement looks good. I have attached the database file,if that helps.
    Attached Files Attached Files

  15. #15
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: SQL Statement error

    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!

    BOFH Now, BOFH Past, Information on duplicates

    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...

  16. #16

    Thread Starter
    Member
    Join Date
    Mar 2004
    Posts
    48

    Re: SQL Statement error

    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.
    Attached Files Attached Files
    Last edited by dawgfather; Jul 20th, 2005 at 10:03 AM.

  17. #17
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: SQL Statement error

    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.

    BOFH Now, BOFH Past, Information on duplicates

    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...

  18. #18

    Thread Starter
    Member
    Join Date
    Mar 2004
    Posts
    48

    Re: SQL Statement error

    Ok. I tested the SQL Statement and it works fine. I get the field.

    Im still getting the same damn "type mismatch error" ...lol

  19. #19

    Thread Starter
    Member
    Join Date
    Mar 2004
    Posts
    48

    Resolved Re: SQL Statement error

    yeeeeeeeehaaawwwwwwwwwww!!!
    It works, im gonna dance and cry at the same time.

    Apparently I have to reference DAO berfore the ActiveX object.
    http://support.microsoft.com/?kbid=181542

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width