Solved

Filter Continuous form using combo box

Posted on 2002-07-01
2
751 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
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

822 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