|
-
Oct 31st, 2007, 05:46 AM
#1
Thread Starter
Frenzied Member
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 
-
Oct 31st, 2007, 05:48 AM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Oct 31st, 2007, 06:01 AM
#3
Thread Starter
Frenzied Member
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 
-
Oct 31st, 2007, 09:49 AM
#4
Re: How to exclude ' or chr(39) from sql statement
of course you can:
vb Code:
For Each Contrl In Me.Controls
If (TypeOf Contrl Is OptionButton) Then
If Contrl.Value = True Then mySQL = mySQL & "'" & Contrl.Name & "',"
End If
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.
-
Oct 31st, 2007, 10:01 AM
#5
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Oct 31st, 2007, 10:09 AM
#6
Thread Starter
Frenzied Member
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 
-
Oct 31st, 2007, 10:11 AM
#7
Re: How to exclude ' or chr(39) from sql statement
 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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Oct 31st, 2007, 10:30 AM
#8
Hyperactive Member
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:
Function FormatTextForSQL(str As String) As String
Dim sLeft As String
Dim sRight As String
sLeft = vbNullString
sRight = str
Do Until Len(sRight) = 0 Or InStr(1, sRight, "|") = 0
sLeft = sLeft & Left(sRight, InStr(1, sRight, "|") - 1) '& "|"
sRight = right(sRight, Len(sRight) - InStr(1, sRight, "|"))
Loop
str = sLeft & sRight
sLeft = vbNullString
sRight = str
Do Until Len(sRight) = 0 Or InStr(1, sRight, """") = 0
sLeft = sLeft & Left(sRight, InStr(1, sRight, """")) & """"
sRight = right(sRight, Len(sRight) - InStr(1, sRight, """"))
Loop
str = sLeft & sRight
sLeft = vbNullString
sRight = str
Do Until Len(sRight) = 0 Or InStr(1, sRight, "'") = 0
sLeft = sLeft & Left(sRight, InStr(1, sRight, "'")) & "'"
sRight = right(sRight, Len(sRight) - InStr(1, sRight, "'"))
Loop
str = sLeft & sRight
FormatTextForSQL = str
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.
-
Oct 31st, 2007, 04:17 PM
#9
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?
-
Nov 1st, 2007, 05:25 AM
#10
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.
-
Nov 1st, 2007, 02:29 PM
#11
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|