Results 1 to 7 of 7
  1. #1
    caddcop is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Feb 2011
    Location
    Upper Marlboro, Maryland, USA
    Posts
    57

    Problems with Recordsets when items contain quotes.


    I have a listbox that reads values from a query. At least one item it returns contains double quotes. I cannot be sure that others won't contain single quotes. And there is also the possibility a record might contain both.

    my code is:
    Code:
    rs.FindFirst "[Name] = " & Chr(34) & Me!
    [List_AssignedStaff] & Chr(34)
    Is there another way of writting this such that single or double quotes will not cause it to fail?

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Try

    rs.FindFirst "[Name] = '" & Me!
    [List_AssignedStaff] & "'"

  3. #3
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Assuming the [Name] field *never* has a double quote in it then this should work:
    rs.FindFirst "[Name] = " & Chr(34) & Replace(Me![List_AssignedStaff],'"','') & Chr(34)
    I might have the syntax a little wrong so play with it.

  4. #4
    caddcop is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Feb 2011
    Location
    Upper Marlboro, Maryland, USA
    Posts
    57
    I'm having trouble reading orange's response.

    What I am doing now is this:
    Code:
    If InStr(1, Me!
    [List_AssignedStaff], Chr(34)) Then
            rs.FindFirst "[Name] = " & Chr(39) & Me!
    [List_AssignedStaff] & Chr(39)
        Else
            rs.FindFirst "[Name] = " & Chr(34) & Me!
    [List_AssignedStaff] & Chr(34)
        End If
    Which only works if there is only " or only ' and not both. Currently, that seems to be true. I may add a message box that warns to not use both.

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Did you try my suggestion?

  6. #6
    caddcop is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Feb 2011
    Location
    Upper Marlboro, Maryland, USA
    Posts
    57
    Currently, it is a name record with double quotes that alerted me to the problem. So I developed my solution. For clarity, I should use:
    Code:
    If InStr(1, Me!
    [List_AssignedStaff], Chr(34)) > 0 Then
            rs.FindFirst "[Name] = " & Chr(39) & Me!
    [List_AssignedStaff] & Chr(39)
    Else
            rs.FindFirst "[Name] = " & Chr(34) & Me!
    [List_AssignedStaff] & Chr(34)
    End If

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    So you have it working, great.

    Are you certain that the incoming name contains a single " ?
    Often, to make a single ", you must use a double "" (2 double quotes)

    I set up a small test to see if I could make it work (it's been a while)
    I could not make a variable with a single contained " (chr(34))

    Sub testQuote()
    Dim list_assignedstaff As String
    list_assisgnedstaff = "O""Leary" ' second version to try "O'Leary"
    Debug.Print list_assisgnedstaff
    Debug.Print InStr(list_assisgnedstaff, Chr(34))
    If InStr(list_assisgnedstaff, """") > 0 Then

    Debug.Print "[Name] = " & Replace(list_assisgnedstaff, Chr(34), Chr(39))
    Else
    Debug.Print "[Name] = " & list_assisgnedstaff
    End If
    End Sub

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

Similar Threads

  1. Replies: 13
    Last Post: 10-12-2011, 11:08 AM
  2. DAO.Recordsets - When to close and open
    By jgelpi16 in forum Programming
    Replies: 11
    Last Post: 02-08-2011, 09:16 AM
  3. Counting large ADODB recordsets
    By harpyopsis in forum Programming
    Replies: 3
    Last Post: 10-16-2009, 10:05 AM
  4. Remove Quotes within Data Values
    By kfschaefer in forum Programming
    Replies: 0
    Last Post: 02-26-2009, 01:15 PM
  5. Recordsets, add new, DAO and ADO
    By Arca in forum Programming
    Replies: 1
    Last Post: 04-07-2008, 03:57 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