Solved

Filter Continuous form using combo box

Posted on 2002-07-01
2
771 Views
Last Modified: 2008-03-06
My simple database in Access 2002 contains a list of books, many of which have the same title but different Book_ID. I have a continuous form with all the records. In the form header I have a combo box which filters the records according to the book name. ie, If there are several books with the selected title I want them all on the form. I copied & amended the code from an example in Access Help

Private Sub cboTitleSearch_AfterUpdate()
    Dim strNewRecord As String
    strNewRecord = "SELECT * FROM Library " _
        & " WHERE Title = '" _
        & Me!cboTitleSearch.Value & "'"
    Me.RecordSource = strNewRecord
End Sub

Everything works fine until I select a book which has an apostrophe in the title, eg Samantha's Story
Then I get the following error:

Run-time error '3075';
Syntax error (missing operator) in query expression 'Title = 'Samantha's Story".

Can someone please help me fix it?
Thanks

0
Comment
Question by:Doozy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 28

Accepted Solution

by:
TextReport earned 100 total points
ID: 7123818
The use of the Apostrope in the code can be replace by using double quotes, so on the assumption there are no double quotes in your titles replace the code with

Private Sub cboTitleSearch_AfterUpdate()
   Dim strNewRecord As String
   strNewRecord = "SELECT * FROM Library " _
       & " WHERE Title = """ & _
       & Me!cboTitleSearch.Value & """"
   Me.RecordSource = strNewRecord
End Sub

Personally though I prefer the following using Chr(34) which is the " character as it is easier to read that all those double quotes.

Private Sub cboTitleSearch_AfterUpdate()
   Dim strNewRecord As String
   strNewRecord = "SELECT * FROM Library " _
       & " WHERE Title = " & Chr(34) _
       & Me!cboTitleSearch.Value & Chr(34)
   Me.RecordSource = strNewRecord
End Sub


Chr(34) is the " character but is easier to read than the alternative of entering


0
 

Author Comment

by:Doozy
ID: 7123835
Works a dream - Thanks so much
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question