Solved

Filter Continuous form using combo box

Posted on 2002-07-01
2
728 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
Comment Utility
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
Comment Utility
Works a dream - Thanks so much
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now