Results 1 to 11 of 11

Thread: How to exclude ' or chr(39) from sql statement

  1. #1

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Question How to exclude ' or chr(39) from sql statement

    Hi There,

    I have an SQL command which i have build up, and the SQLfieldname are simply strings which I have built up by

    chr(39) & strWhatever & chr(39)

    the problem is when strWhatever contains the ' or chr(39) and that causes an error on my SQL, how do i exclude the ' in the variable besides just replacing it with another character?

    cheers


    str = "INSERT INTO WhateverTable (" & SQLfieldname & ")"
    str &= " VALUES (" & SQLfieldvalue & ")"
    If you find my thread helpful, please remember to rate me

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to exclude ' or chr(39) from sql statement

    Use the Replace function and replace the 39 with a nullstring before passing to your sql statemeent.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: How to exclude ' or chr(39) from sql statement

    is there no way to include a ' character in an access database?
    If you find my thread helpful, please remember to rate me

  4. #4
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: How to exclude ' or chr(39) from sql statement

    of course you can:

    vb Code:
    1. For Each Contrl In Me.Controls
    2.        If (TypeOf Contrl Is OptionButton) Then
    3.               If Contrl.Value = True Then mySQL = mySQL & "'" & Contrl.Name & "',"
    4.        End If
    5. Next

    This is a small section of a code in an Access Database I made. If you wrap a single quote in double quotes as shown above, SQL will build it as 'Contrl.Name', which it is able to process.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to exclude ' or chr(39) from sql statement

    He mentioned that solution already so I think hes looking for some other way.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: How to exclude ' or chr(39) from sql statement

    How do you insert ' into databases then?
    If you find my thread helpful, please remember to rate me

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to exclude ' or chr(39) from sql statement

    Quote Originally Posted by dinosaur_uk
    how do i exclude the ' in the variable besides just replacing it with another character?
    Are you needing to insert the apostrophy or remove it?

    To include it you just double up on the apostrophy. to remove it you replace on the apostrophy and set the replacement as a vbnullstring.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8
    Hyperactive Member Davadvice's Avatar
    Join Date
    Apr 2007
    Location
    Glasgow (Scotland)
    Posts
    440

    Re: How to exclude ' or chr(39) from sql statement

    hi,

    If you add this to your project and call the function for each text Var you are adding to the sql this will allow the original text to be passed through

    vb Code:
    1. Function FormatTextForSQL(str As String) As String
    2. Dim sLeft As String
    3. Dim sRight As String
    4.  
    5. sLeft = vbNullString
    6. sRight = str
    7. Do Until Len(sRight) = 0 Or InStr(1, sRight, "|") = 0
    8.     sLeft = sLeft & Left(sRight, InStr(1, sRight, "|") - 1) '& "|"
    9.     sRight = right(sRight, Len(sRight) - InStr(1, sRight, "|"))
    10. Loop
    11. str = sLeft & sRight
    12.  
    13. sLeft = vbNullString
    14. sRight = str
    15.  
    16. Do Until Len(sRight) = 0 Or InStr(1, sRight, """") = 0
    17.     sLeft = sLeft & Left(sRight, InStr(1, sRight, """")) & """"
    18.     sRight = right(sRight, Len(sRight) - InStr(1, sRight, """"))
    19. Loop
    20. str = sLeft & sRight
    21.  
    22. sLeft = vbNullString
    23. sRight = str
    24.  
    25. Do Until Len(sRight) = 0 Or InStr(1, sRight, "'") = 0
    26.     sLeft = sLeft & Left(sRight, InStr(1, sRight, "'")) & "'"
    27.     sRight = right(sRight, Len(sRight) - InStr(1, sRight, "'"))
    28. Loop
    29. str = sLeft & sRight
    30.  
    31. FormatTextForSQL = str
    32.  
    33. End Function

    I.E Insert Into TblA (Tbla.Afield,Tbla.Bfield,Tbla.Cfield) Values(formatTextforSql(" & str1 & " ),formattextForsql (" & str2 &" ), formattextForsql (" & str3 & "))

    thanks

    Dav
    Last edited by Davadvice; Oct 31st, 2007 at 10:34 AM.

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to exclude ' or chr(39) from sql statement

    I haven't checked exactly what Davadvice's code does, but it looks like it is on the right lines.. For an explanation, see the article How do I put the ' character into an SQL string? from our Database Development FAQs/Tutorials (at the top of this forum)

    Note that the method explained in the article (and even what Davadvice posted) is not a complete solution - there are other characters that you can have issues with too, and they should also be dealt with appropriately.

    The easiest way to deal with them all is to not actually do anything special with them - but to let ADO do it all for you instead, by using the Command object with parameters. For an example, see the FAQ How can I add a record to a database?

  10. #10
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: How to exclude ' or chr(39) from sql statement

    I am not sure in other databases, but in Access you can have Chr$(39) ( apostrophy or single-quote ' ) in field names. Even it also accepts Chr$(34) ( double-quote " ).
    In writting SQL, just surround all fieldnames within [ ] regardless the fieldnames contain these characters or not.
    Code:
       SQL = "INSERT INTO Customer ([" & txtFieldName1 & "], [" & numFieldName2 & "]) " & _
             "VALUES (""" & Replace(txtValue1,Chr$(34),Chr$(34) & Chr$(34)) & """, " & numValue2 & ")"
    or
    Code:
       SQL = "INSERT INTO Customer ([" & txtFieldName1 & "], [" & numFieldName2 & "]) " & _
             "VALUES (""" & Replace(txtValue1,"""","""""") & """, " & numValue2 & ")"
    You should surround a Text value with double-quote instead of single-quote and
    replace any double-quote (if exist) in a Text value with 2 double-quotes
    Last edited by anhn; Nov 1st, 2007 at 05:37 AM.

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to exclude ' or chr(39) from sql statement

    Yes, you can but it all depends on how you are connecting and adding.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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