Results 1 to 9 of 9

Thread: Searching Database ?

  1. #1

    Thread Starter
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535
    I am using the following statement to search through a database.
    Set RS = DB.OpenRecordset("SELECT * FROM Table1 WHERE '" & SearchField & "' = '" & KeyWord & "'", dbOpenDynaset)

    Where "SearchField" and "Keyword" are the variables entered by the program user. But the problem is that the above statement does not result in any records relating to user specified keyword but always displays the first record in the database.
    No errors are shown at runtime.

    Please help.


    Kinjal

    [Edited by kinjalgp on 06-27-2000 at 01:32 PM]

  2. #2
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    Misread it the first time, let's try again. Remove the single quotes that are bracketing the Searchfield.

    Set RS = DB.OpenRecordset("SELECT * FROM Table1 WHERE " & SearchField & " = '" & KeyWord & "'", dbOpenDynaset)

    [Edited by JHausmann on 06-27-2000 at 01:47 PM]

  3. #3

    Thread Starter
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535
    The problem actually is if I enter the field name from a variable, the search does not work. But is I keep the field name fixed like in the following statement then the search function works fine.
    Set RS = DB.OpenRecordset("SELECT * FROM Table1 WHERE Name = '" & KeyWord & "'", dbOpenDynaset)



  4. #4
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    if you set the SQL to a string variable and then examine the results in the immediate window (in debug mode), using Name as the keyword, the values for sSQL should be the same for:

    dim sSQL as string

    sSQL="SELECT * FROM Table1 WHERE Name = '" & KeyWord & "'"

    Set RS = DB.OpenRecordset(sSQL , dbOpenDynaset)



    and
    dim sSQL as string

    sSQL ="SELECT * FROM Table1 WHERE " & SearchField & " = '" & KeyWord & "'"

    Set RS = DB.OpenRecordset(sSQL, dbOpenDynaset)

  5. #5

    Thread Starter
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535
    No I think you misunderstood my question. I want both the field name and the keyword to be searched to be variales and not constants. That means the user can select from which field he wants to search and that is not happening in my statement. Maybe it's an syntax error.


  6. #6
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    I understand what you want. My last post attempted to show you how to determine if the SQL between a fixed column query was the same as the SQL for one that's variable. Your original SQL statement is wrapping the column name in quotes when it cannot be. Anything wrapped in quotes is treated as DATA in SQL, you need the column to be recognized as a column.

    Have you tried the second part of the last post (variable column name, below)?

    dim sSQL as string

    sSQL ="SELECT * FROM Table1 WHERE " & SearchField & " = '" & KeyWord & "'"

    Set RS = DB.OpenRecordset(sSQL, dbOpenDynaset)

    Ideally you should be able to cut-n-paste the query, as it's returned in the immediate window, into whatever you're using to run stand-alone queries (Access, SQL Server query window) and get your results

  7. #7

    Thread Starter
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535
    Hey man you are great. . Genius.

    It worked. whee..pee

    I was trying to solve it since last three days.


    Thank you very very much.


    Kinjal

  8. #8
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    From your post it looks like the user is entering table names, you might want to consider using a combo or list box for them to select from to avoid errors caused by typo's... HTH

  9. #9

    Thread Starter
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535
    Yes you are right. I am done the same thing to avoid errors.

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