Wrox Programmer Forums
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 6th, 2003, 10:50 AM
Registered User
 
Join Date: Jun 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with Search Form

Records with from Database that have single quotes are still not working correctly for me is this a know topic or am I just missing something in my code. Any feedback would be welcomed. Thanks.
 
Old June 6th, 2003, 11:00 AM
Registered User
 
Join Date: Jun 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mark.roworth
Default

Been using this for years. It's a bit bulky but well tested. Instead of:

    cSQL = "SELECT * FROM Customer WHERE Name='" & cCustomer & "'"

Use:

    cSQL = "SELECT * FROM Customer WHERE Name='" & Str4DB(cCustomer) & "'"

Where:

Public Function Str4DB(strOriginalString As String) As String
    Dim str As String, iPos As Integer
    If IsNull(strOriginalString) Then
        str = "'" & Null2Str(strOriginalString) & "'"
    Else
        str = "'" & ReplaceAll(strOriginalString, "'", "''") & "'"
    End If
    Str4DB = RemoveThisChar("|", str)
End Function

Public Function Null2Str(varVal As Variant, Optional default As String) As String
    On Error GoTo Err_Null2str

    If IsNull(varVal) Then
        If IsMissing(default) Then
            Null2Str = ""
        Else
            Null2Str = default
        End If
    Else
        Null2Str = CStr(varVal)
    End If
    Exit Function

Err_Null2str:
    Null2Str = ""
    Exit Function
End Function

Public Function ReplaceAll(strMainString As String, strFindString As String, strReplaceString As String) As String
  Dim lngLenMain As Long
  Dim lngStartPos As Long
  Dim lngFoundPos As Long
  Dim strNew As String ' string to build with replacements (initialy blank)

  lngLenMain = Len(strMainString)
  lngStartPos = 1

  Do While lngStartPos <= lngLenMain
    lngFoundPos = InStr(lngStartPos, strMainString, strFindString, 0) ' 0 = case sensitive
    If lngFoundPos = 0 Then ' No (more) instances
      ' Append remainder of strMainString to strNew
      strNew = strNew & Right$(strMainString, lngLenMain - (lngStartPos - 1))
      Exit Do
    Else
      ' Found a (new) instance. Replace it.
      ' ie. add the part of strMainString JUST scanned prior to instance
      ' then replacement string
      strNew = strNew & Mid$(strMainString, lngStartPos, lngFoundPos - lngStartPos) & strReplaceString
      lngStartPos = lngFoundPos + Len(strFindString)
    End If
  Loop

  ReplaceAll = strNew
End Function

Public Function RemoveThisChar(strChar As String, str As String) As String
    Dim strNew As String, strTempChar As String
    Dim iX As Integer
    For iX = 1 To Len(str)
        strTempChar = Mid(str, iX, 1)
        If strTempChar <> strChar Then strNew = strNew + strTempChar
    Next iX
    RemoveThisChar = strNew
End Function



Mark Roworth
 
Old June 6th, 2003, 11:10 AM
Registered User
 
Join Date: Jun 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mlea
Default

If I am not mistaken (having had this problem numerous times over the years) single quotes need to be removed and replaced with the proper escape sequence which I don't have at hand, or as a quick test just remove them to see if this corrects the problem. The deal is that the single quotes cause the VB to misinterpret them as syntax. I have had this exact problem using ASP and by removing the quotes the problem is corrected.
 
Old June 6th, 2003, 11:16 AM
Registered User
 
Join Date: Jun 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mark.roworth
Default

In summary, replace any ' with ''.

Mark Roworth





Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with search-form Hylsan PHP Databases 0 May 4th, 2007 08:36 AM
Search Form? scandalous Access 8 February 8th, 2007 09:01 AM
Problem opening Form from search sprocket Access 5 July 7th, 2006 01:17 PM
Search in a sub form smang Access 1 January 7th, 2005 08:30 PM
Search Form ksa266 Access VBA 2 December 29th, 2003 06:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.