How do I deal with single apostrophes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PeterG
    New Member
    • Feb 2010
    • 2

    How do I deal with single apostrophes

    I have inherited some code that has worked well until an Irish member joined us.

    His name "O'Riordan" has caused problems

    Part of our basic code looks like this
    Code:
    Public Sub Check_name()
    
    fieldmanager = Name
    manager = "O'Riordan Patrick"
    filterReport = "qry_staff"
    
    Set rstManager = DBEngine(0)(0).OpenRecordset("SELECT distinct name FROM " _
                    & filterReport & " where name = " & "'" & manager & "'")
                    
      records = rstManager.RecordCount
      MsgBox ("No of records is " & records)
      rstManager.MoveFirst
      manager_name = rstManager.Fields("name")
      
      MsgBox ("Name is " & manager_name)
    
    
    End Sub
    but the query fails because his name has the single apostrophe.

    Can anyone advise how I correct this programmaticall y as there are likely to be others in the corporate staff table.
    Last edited by NeoPa; Feb 5 '10, 01:18 AM. Reason: Please use the [CODE] tags provided
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32656

    #2
    You should find the code in SQL Injection Attack to be helpful here.

    Further info can be found at Quotes (') and Double-Quotes (") - Where and When to use them.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32656

      #3
      Sorry.

      I just reread the first article and the code I expected (which simply doubles up all instances of these characters) doesn't appear to be there.

      Your solution is as simple as that though. Does that help?

      Comment

      • missinglinq
        Recognized Expert Specialist
        • Nov 2006
        • 3533

        #4
        Also you appear to have a field called Name! Name is a Reserved Word in Access, and sooner or later this is going jump up and bite you in the rump! You really need to change it to something else.

        Welcome to Bytes!

        Linq ;0)>

        Comment

        • PeterG
          New Member
          • Feb 2010
          • 2

          #5
          Thanks for the references. It's pointing me in the right direction so I'll spend today playing with it.

          As a newbie to Access vba it is helpful having this resource

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32656

            #6
            We're always happy to help those who are willing to learn :)

            Welcome to Bytes!

            Comment

            Working...