Results 1 to 23 of 23

Thread: how to cast a string value if there is ' in it?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    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

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    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

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    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

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    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:
    1. 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

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    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,

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

    Re: how to cast a string value if there is ' in it?

    Quote 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 , "'", "''")

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    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

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

    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?

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    Re: how to cast a string value if there is ' in it?

    Quote 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.

  12. #12
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    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

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    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

  14. #14
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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!)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  15. #15
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    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

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    Re: how to cast a string value if there is ' in it?

    Quote Originally Posted by GaryMazzone
    What is the actual SQL string being passed?
    it's 6 post's up. Crohn's disease

  17. #17
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    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

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    Re: how to cast a string value if there is ' in it?

    Quote 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

  19. #19
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    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

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    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.

  21. #21
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    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

  22. #22
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: how to cast a string value if there is ' in it?

    If I may make a suggestion (again)....
    VB Code:
    1. Dim conn As ADODB.Connection
    2. Dim rs As ADODB.Recordset
    3. Dim cmdSQL as ADODB.Command
    4. Set conn = New ADODB.Connection
    5. Dim strconn As String
    6. strconn = "PROVIDER=SQLOLEDB;"
    7. strconn = strconn & "Server=mysrvname;database=mydb;uid=user; password=pass;"
    8. conn.Open strconn
    9.  
    10. Set cmdSQL = New ADODB.Command
    11. cmdSQL.CommandText = "SELECT * from tblProj where ProjectName = @ProjName"
    12. cmdSQL.CommandType = adCommandText
    13.  
    14. cmdSQL.Parameters.Append cmdSQL.CreateParameter("@ProjName", adVarChar, adParameterInput, 255, Sheet1.cbox1.Value
    15.  
    16. cmdSQL.ActiveConnection = conn
    17.  
    18. Set rs = cmdSQL.Execute
    19. Set rs.ActiveConnection = Nothing
    20.  
    21. conn.Close
    22. Set rs = Nothing 'Why are you geting RS then setting it to nothing here?
    23. Set cn = Nothing

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    Re: how to cast a string value if there is ' in it?

    Quote Originally Posted by techgnome
    If I may make a suggestion (again)....
    VB Code:
    1. Dim conn As ADODB.Connection
    2. Dim rs As ADODB.Recordset
    3. Dim cmdSQL as ADODB.Command
    4. Set conn = New ADODB.Connection
    5. Dim strconn As String
    6. strconn = "PROVIDER=SQLOLEDB;"
    7. strconn = strconn & "Server=mysrvname;database=mydb;uid=user; password=pass;"
    8. conn.Open strconn
    9.  
    10. Set cmdSQL = New ADODB.Command
    11. cmdSQL.CommandText = "SELECT * from tblProj where ProjectName = @ProjName"
    12. cmdSQL.CommandType = adCommandText
    13.  
    14. cmdSQL.Parameters.Append cmdSQL.CreateParameter("@ProjName", adVarChar, adParameterInput, 255, Sheet1.cbox1.Value
    15.  
    16. cmdSQL.ActiveConnection = conn
    17.  
    18. Set rs = cmdSQL.Execute
    19. Set rs.ActiveConnection = Nothing
    20.  
    21. conn.Close
    22. Set rs = Nothing 'Why are you geting RS then setting it to nothing here?
    23. 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
  •  



Click Here to Expand Forum to Full Width