Results 1 to 8 of 8
  1. #1
    VSCurtis is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Dec 2014
    Posts
    65

    Query Throws Error Because Of Punctuation In Search Arguement

    Hi Guys,

    Could use a little insight here. I have a procedure that is called in my Card Name After Update Event. The procedure attempts to check to see if the card name that I just typed in is already in the database, in other words, a duplicate card. If the procedure gets a hit then it throws up a message box telling me that a card already exists with that name and gives me the chance to delete the record and the card. I just threw something at the query that it had trouble processing probably because of punctuation in the card name. The card name is The High Priestess' Staff. My question is how to code my SQL statement to account for stuff like this. My guess is that the query is looking for the closing single quote and there isn't one, so it throws an error.

    Here's the code

    Code:
    Private Sub CheckForDuplicates()
    Dim sSQL As String
    Dim rsCardsDuplicates As Recordset
    sSQL = "SELECT tblCards.[Card Name]FROM tblCards WHERE tblCards.[Card Name] = '" & txtCardName.Value & "';"
    If rsCards.EditMode = dbEditAdd Or rsCards.EditMode = dbEditInProgress Then
       Set rsCardsDuplicates = CurrentDb.OpenRecordset(sSQL, dbOpenDynaset)
       If rsCardsDuplicates.RecordCount > 0 Then
          DeleteRecord (True)
       End If
       rsCardsDuplicates.Close
       If Not rsCardsDuplicates Is Nothing Then
          Set rsCardsDuplicates = Nothing
       End If
    End If
    End Sub
    Any help you can provide is appreciated

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    if you are referring to the single quote, you need to replace it with two single quotes e.g.

    WHERE tblCards.[Card Name] = '" & replace(txtCardName,"'","''") & "';"

    Note you do not need .value which is the default property

  3. #3
    VSCurtis is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Dec 2014
    Posts
    65
    Thanks for the assist. I'll give it a shot. As for my use of .value, I like to fully qualify my code. I'm aware that it's not needed

  4. #4
    VSCurtis is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Dec 2014
    Posts
    65
    I tried what you suggested. The query string still throws the same error. Here's the modified code.

    Code:
    Private Sub CheckForDuplicates()
    Dim sSQL As String
    Dim rsCardsDuplicates As Recordset
    sSQL = "SELECT tblCards.[Card Name]FROM tblCards WHERE tblCards.[Card Name] = " & Replace(txtCardName, "'", "''") & "';"
    If rsCards.EditMode = dbEditAdd Or rsCards.EditMode = dbEditInProgress Then
       Set rsCardsDuplicates = CurrentDb.OpenRecordset(sSQL, dbOpenDynaset)
       If rsCardsDuplicates.RecordCount > 0 Then
          DeleteRecord (True)
       End If
       rsCardsDuplicates.Close
       If Not rsCardsDuplicates Is Nothing Then
          Set rsCardsDuplicates = Nothing
       End If
    End If
    End Sub

  5. #5
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,740
    Code:
    Private Sub CheckForDuplicates()
    Dim sSQL As String
    Dim rsCardsDuplicates As DAO.Recordset
    dim sWork as string
    sWork = replace(txtCardName,"'","''") 
    sSQL = "SELECT tblCards.[Card Name]FROM tblCards WHERE tblCards.[Card Name] = '" & sWork & "';"
    debug.print sSQL
    If rsCards.EditMode = dbEditAdd Or rsCards.EditMode = dbEditInProgress Then
       Set rsCardsDuplicates = CurrentDb.OpenRecordset(sSQL, dbOpenDynaset)
       If rsCardsDuplicates.RecordCount > 0 Then
          DeleteRecord (True)
       End If
       rsCardsDuplicates.Close
       If Not rsCardsDuplicates Is Nothing Then
          Set rsCardsDuplicates = Nothing
       End If
    End If
    End Sub
    If this doesn't work, tell us what the error message is, and what line the code stops on.

  6. #6
    VSCurtis is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Dec 2014
    Posts
    65
    I think I got it figured out. I used the multiple quotes method to avoid having the apostrophe treated as a delimiter. This is my fix. It should work. Haven't tested it yet. I had to rollback to a backup database because the error caused some other issues that were just easier to deal with by starting from scratch.

    Code:
    Private Sub CheckForDuplicates()
    Dim sSQL As String
    Dim rsCardsDuplicates As Recordset
    sSQL = "SELECT tblCards.[Card Name]FROM tblCards WHERE tblCards.[Card Name] = """ & txtCardName.Value & """" & ";"
    If rsCards.EditMode = dbEditAdd Or rsCards.EditMode = dbEditInProgress Then
       Set rsCardsDuplicates = CurrentDb.OpenRecordset(sSQL, dbOpenDynaset)
       If rsCardsDuplicates.RecordCount > 0 Then
          DeleteRecord (True)
       End If
       rsCardsDuplicates.Close
       If Not rsCardsDuplicates Is Nothing Then
          Set rsCardsDuplicates = Nothing
       End If
    End If
    End Sub

  7. #7
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    As davegri pointed out, you should disambiguate which object model you are using:
    Code:
    Dim rsCardsDuplicates As DAO.Recordset

  8. #8
    VSCurtis is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Dec 2014
    Posts
    65
    My fix worked. The apostrophe was handled correctly. Not a problem that happens very often and most of the time using single ticks works just fine, but given the nature of some of these card names I needed to be able to enclose my search string in quotes not single ticks because punctuation does appear in some of the card names and should be treated as such. I'm a little rusty in dealing with some of this stuff and I appreciate your patience and assistance.

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

Similar Threads

  1. My code throws error 424. How do i work around this?
    By ThornofSouls in forum Programming
    Replies: 2
    Last Post: 09-11-2015, 06:30 AM
  2. Replies: 6
    Last Post: 01-29-2014, 08:03 AM
  3. Arguement not optional - INSERT INTO sql command
    By shabbaranks in forum Programming
    Replies: 5
    Last Post: 04-24-2013, 09:42 AM
  4. Replies: 13
    Last Post: 07-05-2012, 10:33 AM
  5. Yes/No Box based on arguement
    By wfeandsig in forum Database Design
    Replies: 4
    Last Post: 12-10-2011, 07:42 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