|
-
Jun 23rd, 2006, 11:44 AM
#1
Thread Starter
Addicted Member
how to cast a string value if there is ' in it?
hi,
I'm trying to pass a value to a sql query and it heppened to have ' in it and sql is complaining. I casted to string type but still does. here is my sql string:
---------------------------------
dim proname as string
proname = cStr(sheet1.cbox1.value)
SELECT *
FROM tblEmpl
Where ProjName = '" & proname & "'
----------------------------------
this works perfect if there is no ' in the value passed.
FYI. I'm using VBA with Sql server
thanks for any sugetsions
-
Jun 23rd, 2006, 11:56 AM
#2
Re: how to cast a string value if there is ' in it?
read through this thread: http://vbforums.com/showthread.php?t=408670 see if it helps you any.
-tg
-
Jun 23rd, 2006, 12:47 PM
#3
Thread Starter
Addicted Member
Re: how to cast a string value if there is ' in it?
not very helpful. I tried all kind of cencatenation and didn't work :-(
thanks
-
Jun 23rd, 2006, 01:05 PM
#4
Re: how to cast a string value if there is ' in it?
DON'T USE concatenation... that's what that thread was all about... avoiding concatenation and using parameterized queries instead.... in SQL the ' is a string delimiter... so when it appears in your text, the SQL thinks that the string has ended and expects what follows to be more SQL stuf, which it isn't....
-tg
-
Jun 23rd, 2006, 01:25 PM
#5
Thread Starter
Addicted Member
Re: how to cast a string value if there is ' in it?
I'm using vba where the example uses vb.net. I'm not sure how to translate that into vba !!! any
thanks tg
-
Jun 23rd, 2006, 01:33 PM
#6
Re: how to cast a string value if there is ' in it?
I'm not sure if VBA has the Replace function. If it does, you can
VB Code:
yourString = Replace(yourString, "'", "''")
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Jun 23rd, 2006, 01:58 PM
#7
Thread Starter
Addicted Member
Re: how to cast a string value if there is ' in it?
that will not work Al42 because I'm not inserting but selecting besed on a condition and the value that I'm comparing to has ' in it.
thanks anyways,
-
Jun 23rd, 2006, 03:19 PM
#8
Re: how to cast a string value if there is ' in it?
 Originally Posted by waely
I'm using vba where the example uses vb.net. I'm not sure how to translate that into vba !!!
Erm.. that was not VB.Net, it was just VB.
that will not work Al42 because I'm not inserting but selecting besed on a condition and the value that I'm comparing to has ' in it.
It will work. If I change the variable name for you, does that help?
Code:
proname = Replace(proname , "'", "''")
-
Jun 26th, 2006, 09:25 AM
#9
Thread Starter
Addicted Member
Re: how to cast a string value if there is ' in it?
Thank you si_the_geek. I trie that also but I still get an error that says:
Incorrect Syntax near 's'.
here is what I have:
proname = "Crohn's Disease"
proname = Replace(proname , " ' ", " ' ' ")
strsql = "SELECT * from tblProj where ProjectName = ' " & proname & " ' " "
this will work just fine if my proname value doesn't have ' in it.
thank you,
waely
-
Jun 26th, 2006, 09:28 AM
#10
Re: how to cast a string value if there is ' in it?
Do you actually have those spaces in your code (you shouldnt), or did you just add them for "effect" here?
-
Jun 26th, 2006, 09:44 AM
#11
Thread Starter
Addicted Member
Re: how to cast a string value if there is ' in it?
 Originally Posted by si_the_geek
Do you actually have those spaces in your code (you shouldnt), or did you just add them for "effect" here?
I put them there for clarity only. one thing I don't understand. if I'm replacing the ' with '' in the Replace funtion so my proname becomes Crohn''s desease in this case and even if my code works I will not get anything back becasue the name of my project is with one ' .
thanks
Last edited by waely; Jun 26th, 2006 at 09:49 AM.
-
Jun 26th, 2006, 10:07 AM
#12
Re: how to cast a string value if there is ' in it?
There is a problem in the SQL string:
strsql = "SELECT * from tblProj where ProjectName = ' " & proname & " ' " "
There is an extra double qoute in there at the end. It should be:
strsql = "SELECT * from tblProj where ProjectName = ' " & proname & " ' "
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jun 26th, 2006, 01:40 PM
#13
Thread Starter
Addicted Member
Re: how to cast a string value if there is ' in it?
Thanks Gary. but this is not the issue. it's actually type there was a group by statement that I removed after the where clause and forgot to remove the ".
thank you the error is still " Syntax Error near 's' "
waely
-
Jun 26th, 2006, 01:48 PM
#14
Re: how to cast a string value if there is ' in it?
Are you replacing it with double quotes or 2 single quotes? (It should be 2 single quotes!)
-
Jun 26th, 2006, 02:37 PM
#15
Re: how to cast a string value if there is ' in it?
What is the actual SQL string being passed?
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jun 26th, 2006, 02:44 PM
#16
Thread Starter
Addicted Member
Re: how to cast a string value if there is ' in it?
 Originally Posted by GaryMazzone
What is the actual SQL string being passed?
it's 6 post's up. Crohn's disease
-
Jun 26th, 2006, 03:31 PM
#17
Re: how to cast a string value if there is ' in it?
What is the value that is in the varable? Not how you are coding it but what is actually in it.
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jun 26th, 2006, 04:34 PM
#18
Thread Starter
Addicted Member
Re: how to cast a string value if there is ' in it?
 Originally Posted by GaryMazzone
What is the value that is in the varable? Not how you are coding it but what is actually in it.
the value is "crohn's disease" as I mentioned above
-
Jun 26th, 2006, 06:53 PM
#19
Re: how to cast a string value if there is ' in it?
That is not what I asked. What is the complete and full string that is being passed to the database. Not what the variable contains that you are looking for.
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jun 26th, 2006, 08:21 PM
#20
Thread Starter
Addicted Member
Re: how to cast a string value if there is ' in it?
here is the whole block of code that is contains my sql string
-------------------------------------------------------------------
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
Dim strconn As String
strconn = "PROVIDER=SQLOLEDB;"
strconn = strconn & "Server=mysrvname;database=mydb;uid=user; password=pass;"
conn.Open strconn
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim proname As String
proname = Replace(Sheet1.cbox1.Value, " ' ", " ' ' ") ' get my first combobox value
With rs
.ActiveConnection = conn
.Open "SELECT * from tblProj where ProjectName = ' " & proname & " ' "
Sheet1.Range("B13").CopyFromRecordset rsclinlog
.Close
End With
conn.Close
Set rs = Nothing
Set cn = Nothing
----------------------------------------------------------------------------
hope this what you're looking for.
-
Jun 27th, 2006, 08:11 AM
#21
Re: how to cast a string value if there is ' in it?
No that is not it. Place a debug.print strSQL statement in the code after the SQL string is built and run it. This will place the SQL statement that is actually being built and passed in the degub window and then copy and paste that back here.
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jun 27th, 2006, 08:27 AM
#22
Re: how to cast a string value if there is ' in it?
If I may make a suggestion (again)....
VB Code:
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim cmdSQL as ADODB.Command
Set conn = New ADODB.Connection
Dim strconn As String
strconn = "PROVIDER=SQLOLEDB;"
strconn = strconn & "Server=mysrvname;database=mydb;uid=user; password=pass;"
conn.Open strconn
Set cmdSQL = New ADODB.Command
cmdSQL.CommandText = "SELECT * from tblProj where ProjectName = @ProjName"
cmdSQL.CommandType = adCommandText
cmdSQL.Parameters.Append cmdSQL.CreateParameter("@ProjName", adVarChar, adParameterInput, 255, Sheet1.cbox1.Value
cmdSQL.ActiveConnection = conn
Set rs = cmdSQL.Execute
Set rs.ActiveConnection = Nothing
conn.Close
Set rs = Nothing 'Why are you geting RS then setting it to nothing here?
Set cn = Nothing
-tg
-
Jun 27th, 2006, 09:57 AM
#23
Thread Starter
Addicted Member
Re: how to cast a string value if there is ' in it?
 Originally Posted by techgnome
If I may make a suggestion (again)....
VB Code:
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim cmdSQL as ADODB.Command
Set conn = New ADODB.Connection
Dim strconn As String
strconn = "PROVIDER=SQLOLEDB;"
strconn = strconn & "Server=mysrvname;database=mydb;uid=user; password=pass;"
conn.Open strconn
Set cmdSQL = New ADODB.Command
cmdSQL.CommandText = "SELECT * from tblProj where ProjectName = @ProjName"
cmdSQL.CommandType = adCommandText
cmdSQL.Parameters.Append cmdSQL.CreateParameter("@ProjName", adVarChar, adParameterInput, 255, Sheet1.cbox1.Value
cmdSQL.ActiveConnection = conn
Set rs = cmdSQL.Execute
Set rs.ActiveConnection = Nothing
conn.Close
Set rs = Nothing 'Why are you geting RS then setting it to nothing here?
Set cn = Nothing
-tg
Thanks Tg. I get an error saying that I must delcare the variable @ProjName when it tries to execute the command.
thank you
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
|