Solved

Filter Continuous form using combo box

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

829 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