Results 1 to 4 of 4
  1. #1
    wackywoo105 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2014
    Posts
    34

    me.filter using 2 strings - problem with apostrophe.


    I’m using the following code to use one text box input to search for both first and surname. For say “Paul Davidson” I can type “da pa” or “d pau” etc.

    The problem I have encountered is with apostrophes. If I type “o’c pa” for say “Paul O’Callaghan” it gives me a syntax error.

    I have been looking into double-double quotes and char(34) but have been unable to implement them so that they work.

    Can anyone help with this and get the code working for names with apostrophes?

    Code:
    Private Sub FilterON_Click()
        Dim nSpace As Integer, lastStr As String, firstStr As String
        
        Me.FilterON = False
        
        nSpace = InStrRev(Me![Filter], " ")
        
        If nSpace = 0 Then
            Me.Filter = "[surname] LIKE '" & Me![Filter] & "*'"
        Else
            lastStr = Trim(Mid(Me![Filter], 1, InStr(Me![Filter], " ")))
            firstStr = Trim(Mid(Me![Filter], InStr(Me![Filter], " ")))
            
            Debug.Print lastStr
            Debug.Print firstStr
            
            Me.Filter = "[Surname] LIKE '" & lastStr & "*' And [First Names] LIKE '" & firstStr & "*'"
        End If
        
        Me.FilterON = True
    End Sub
    Last edited by wackywoo105; 05-07-2014 at 03:55 AM. Reason: Solved

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Was the solution to use Replace function to double the apostrophe?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    wackywoo105 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2014
    Posts
    34
    Started using char(34) but then switched to double-double quotes.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    What I do:

    Replace([fieldname],"'", "''")
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 2
    Last Post: 04-14-2014, 10:42 PM
  2. Apostrophe in name
    By NISMOJim in forum Programming
    Replies: 1
    Last Post: 04-04-2013, 10:14 PM
  3. Replies: 2
    Last Post: 02-16-2012, 04:29 PM
  4. Replies: 6
    Last Post: 11-19-2011, 09:47 PM
  5. Syntax problem? Strings
    By axess_nab in forum Forms
    Replies: 5
    Last Post: 06-01-2011, 03:21 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