Results 1 to 3 of 3

Thread: Select causes a problem when a record contains " ' "

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Posts
    1

    Exclamation

    When Selecting a record using the Select Statement it returns an error if the record contains an " ' ".

    Ie.

    Select * From [Banks] Where [Banks] ='" & Me.Text1.Text & "'"

    If the bank was called " John's Bank " the system only reads up to the apostophe. so it searches for "John".

    Is there any way around this as it is causing me grief

    Thanks
    Joe

  2. #2
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    Hi Joe

    use the VB6 Replace function, and replace all single quotes with 2 single quotes (NOT a double quote)

    example:

    John's Bank

    should be:

    John''s Bank

    before you send it to be processed.
    So use this:

    Select * From [Banks] Where [Banks] ='" & replace(Me.Text1.Text, "'", "''") & "'"




  3. #3
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Lightbulb Another Option

    If you are not using VB6 or want a different way you could these functions:

    Fix quotes fixes a text field, and fixformquotes fixes all text fields on the form

    Code:
    Public Sub FixFormQuotes(glCallfrm As Form)
    Dim mycontrol As Control
    For Each mycontrol In glCallfrm.Controls
    If TypeOf mycontrol Is TextBox Then
      mycontrol.Text = FixQuotes(mycontrol.Text)
    End If
    Next
    
    End Sub
    Public Function FixQuotes(A As String)
    
    Dim i As Integer
    i = InStr(A, "'")
    
    Do While i > 0
      A = Left(A, i) & Mid(A, i)
      i = InStr(i + 2, A, "'")
    Loop
    
    FixQuotes = A
    
    End Function

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