Results 1 to 10 of 10
  1. #1
    timmy is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    62

    Making a keyword search

    I want to create a textbox that filters the records and shows matching ones based on whats typed.

    Access already has on of these at the bottom of a form, i just want to have it on the actual form, just for better usability really.

    Thanks a lot!

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    you can do this by writing sql that uses the keyword LIKE. for instance, something like this behind a form would work:
    Code:
    sql = "select * from table " & _
    "where field LIKE '*' & '" & me.textbox & "' & '*'"
    
    me.formOrSubFormSource.recordsource = sql
    me.formOrSubFormSource.requery

  3. #3
    timmy is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    62
    Thanks Adam, ill slot that code in and play about a bit. Ill let you know what happens.
    thanks again .

  4. #4
    timmy is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    62
    right... i put this in a button next to the textbox:

    Code:
     
    Private Sub Searchbutton_Click()
       SQL = "select * from table " & _
       "where field LIKE '*' & '" & Me.Searchtext & "' & '*'"
       Me.formOrSubFormSource.RecordSource = SQL
       Me.formOrSubFormSource.Requery
    End Sub
    im getting this error on the "SQL =" bit:

    Compile Error:
    Variable not defined

    ive got a feeling im putting the code in the wrong place... could you help me out?

    btw, my textbox is called "searchtext", so i changed the name in the code.

  5. #5
    PrintShopSup is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2010
    Posts
    11
    Should "SQL" be in small case?
    Last edited by PrintShopSup; 03-11-2011 at 04:42 AM. Reason: wrong post

  6. #6
    timmy is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    62
    just tried that, it gives me the same error and changes them back to upper case

    Thanks tho, any other ideas?

    edit: just tried the same code in the on change event of the text box, getting exactly the same error.

  7. #7
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by PrintShopSup View Post
    Should "SQL" be in small case?
    ummmm..NO.
    Quote Originally Posted by timmy View Post
    just tried that, it gives me the same error and changes them back to upper case
    Tim,

    it does that because vba's intellisense stores the strings in memory that have been declared as variables.

    Furthermore, did you realize that part of my code was actually TELLING you what to put in as substitutes for what I wrote? You copied it verbatim. You need to substitute your own control names into my sample, bud.

    also, it could've broken there because you didn't declare the SQL variable... how much experience do you have with vba?

  8. #8
    timmy is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    62
    oh god... slightly embaressing, reading back on the code i cant believe i just copy and pasted... Ill have a fiddle with it when im back in work on monday.

    thanks for your help, hopefully i can sort this out .

  9. #9
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by timmy View Post
    oh god... slightly embaressing, reading back on the code i cant believe i just copy and pasted... Ill have a fiddle with it when im back in work on monday.

    thanks for your help, hopefully i can sort this out .
    let us know if you need more assistance.

  10. #10
    timmy is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    62
    Code:
     
    Private Sub Searchbutton_Click()
       SQL = "select * from Customers " & _
       "where field LIKE '*' & '" & Me.Searchtext & "' & '*'"
     
       Me.formOrSubFormSource.Customers = SQL
       Me.formOrSubFormSource.Requery
    End Sub
    tried that... getting the same error. (customers is the table im using)

    Code:
    it could've broken there because you didn't declare the SQL variable...
    how do i declare SQL as a variable? Also, im not sure where im supposed to put my tables / form names and where the code is supposed to say "field" or "table", sorry im really not very good at vba

    edit: OT but i found a post where you mentioned a FAQ on distributing a database, cant find it now. could you gimme a link ajetrumpet? thanks.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Help with making a calendar
    By slmorgan25 in forum Access
    Replies: 1
    Last Post: 09-15-2010, 10:32 AM
  2. Replies: 2
    Last Post: 08-31-2010, 08:57 PM
  3. Extracting data after a keyword
    By KerryA in forum Queries
    Replies: 1
    Last Post: 02-11-2010, 09:56 PM
  4. making into update query
    By tom4038 in forum Queries
    Replies: 1
    Last Post: 09-23-2009, 11:19 AM
  5. Making tabs in forms
    By cesarone82 in forum Access
    Replies: 0
    Last Post: 06-09-2009, 12:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums