Results 1 to 6 of 6

Thread: [RESOLVED] Apostrophe in String Data causes error.

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    4

    Resolved [RESOLVED] Apostrophe in String Data causes error.

    Hi

    I am a noob to VB6 but am supporting some code written by another person a long time ago. Upto now I have been doing OK but I recently hit an issue that is outside of my experience and I was hoping someone could point me in the right direction. The program in question is auto-processing emails (the client is Outlook) and performing actions based upon the content of the email.

    The issue I have hit however is that for the first time we received an email from an sender who's email address contained an apostrophe character > ' < e.g. rosie.o'[email protected] which crashed the email lookup function. The failing code is as follows:

    Private Function FindRecord(EmlDBObj As Object, SQLSearchString As String)
    If EmlDBObj.RecordCount > 0 Then
    EmlDBObj.MoveFirst
    EmlDBObj.Find SQLSearchString, , adSearchForward
    End If

    I would greatly appreciate any suggestions of how to handle the apostrophe issue.

    Thanks in advance .... Colin

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Apostrophe in String Data causes error.

    You need to change the single apostrophe to double. Something like this:

    Code:
    Private Sub Command_Click()
        Dim SQLSearchString As String
        Dim strLastName As String
        
        strSQL = "select * from refuserinfo"
            
        rsTempRecordset.Open strSQL, cnRate, adOpenStatic, adLockReadOnly
        
        
        strLastName = "o'price"
        SQLSearchString = " lastname = '" & Replace(strLastName, "'", "''") & "'"
       
        If Not rsTempRecordset.EOF Then
            rsTempRecordset.Find SQLSearchString, , adSearchForward
            If rsTempRecordsetEOF Then
                MsgBox "No Records Found... Try Again"
            Else
                MsgBox "Records Found"
            End If
        End If
        
    End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    4

    Re: Apostrophe in String Data causes error.

    Thank you for your help with this - I will give it a try

    Cheers

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Apostrophe in String Data causes error.

    This is already covered in the FAQ section: http://www.vbforums.com/showthread.p...-an-SQL-string
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    4

    Re: Apostrophe in String Data causes error.

    Thank you

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    4

    Re: [RESOLVED] Apostrophe in String Data causes error.

    Thank you for your help this worked like a charm I just needed to figure out the correct context for the replace function and where to add it.

    My SQL Search Function was:

    Private Function FindRecord(EmlDBObj As Object, SQLSearchString As String)
    If EmlDBObj.RecordCount > 0 Then
    EmlDBObj.MoveFirst
    EmlDBObj.Find SQLSearchString, , adSearchForward
    End If

    If EmlDBObj.EOF Then
    FindRecord = False
    Else
    FindRecord = True
    End If

    End Function

    Based on your advice I changed tall the Function Calls from:

    If Not FindRecord(DBConn.ESDB_RCP, "[RCP_MAILADDRESS] = '" & xSafe.Sender.Address & "'") Then ......

    To the following:

    Not FindRecord(DBConn.ESDB_RCP, "[RCP_MAILADDRESS] = '" & Replace(xSafe.Sender.Address, "'", "''") & "'") Then .....

    Thank you again for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width