Results 1 to 18 of 18

Thread: [RESOLVED] SQL INSERT error

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Resolved [RESOLVED] SQL INSERT error

    I am trying to INSERT new data into an MsACCESS 2003 DB. The field am trying to populate is comments field, with a field type of memo. When I type my comments with an apostrophe (') I get an error when submitting the info but without the apostrophe, it is Ok. How can I code to make sure that the apostrophe does not create an error?

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

    Re: SQL INSERT error

    Replacing each single tick with 2 tics ("escaping" the single quotes) should do it:
    VB Code:
    1. <your text variable here> = Replace(<your text variable here>, "'", "''")
    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

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: SQL INSERT error

    Access doesnt like '

    how are u adding it? (Post code)
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: SQL INSERT error

    You should either replace the apostrophe with two apostrophes using Replace. However, I personally do not recommend that instead you should be using parametrized queries. Take a look at this thread expecially post #5
    http://www.vbforums.com/showthread.p...ighlight=Query
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: SQL INSERT error

    The code is:-

    VB Code:
    1. sSQL = sSQL = "INSERT INTO movieInfo (title, actor, producer, comment) VALUES ("
    2. sSQL = sSQL & Val(txtTitle.Text) & ", "
    3. sSQL = sSQL & "'" & txtactor.Text & "', "
    4. sSQL = sSQL & "'" & txtproducer.Text & "', "
    5. sSQL & "'" & txtComment.Text & "') "

    Thanks

  6. #6
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: SQL INSERT error

    Did you check out the link that I posted above?
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  7. #7
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: SQL INSERT error

    try using " instead of '

    VB Code:
    1. sSQL = sSQL = "INSERT INTO movieInfo (title, actor, producer, comment) VALUES ("
    2. sSQL = sSQL & Val(txtTitle.Text) & ", "
    3. sSQL = sSQL & Chr(34) & txtactor.Text & Chr(34) & ", "
    4. sSQL = sSQL & Chr(34) & txtproducer.Text & Chr(34) & ", "
    5. sSQL = sSQL & Chr(34) & txtComment.Text & Chr(34) & ") "
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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

    Re: SQL INSERT error

    Does the producer or actor name look this: O'Neil? If so then ye single qoute (') need to be replaced byu two single qoutes (''). The same goes for the comment text if Sam's is used then the single qoute again needs to be replaced by two single qoutes.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

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

    Re: SQL INSERT error

    Ugh....

    Parameterized query folks.... Parameterized queries....
    VB Code:
    1. sSQL = "INSERT INTO movieInfo (title, actor, producer, comment) VALUES (?,?,?,?)
    2.  
    3. objCommand.CommandText = sSQL
    4. objCommand.CommandType = adCommandText
    5. objCommand.Parameters.Add objCommand.CreateParameter("title",advarchar,50, txtTitle.Text)
    6. objCommand.Parameters.Add objCommand.CreateParameter("actor",advarchar,50, txtactor.Text)
    7. objCommand.Parameters.Add objCommand.CreateParameter("producer",advarchar,50, txtproducer.Text)
    8. objCommand.Parameters.Add objCommand.CreateParameter("comment",advarchar,50, txtComment.Text)
    9.  
    10. objCommand.Execute
    Just change the 50 to suit your needs (it's the length of the field in the database.

    -tg

    edit: I think I also forgot a parameter or two in the createparameter function, so don't copy paste it directly.... I'm doing this off of memory here.
    * 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??? *

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: SQL INSERT error

    Guys, thanks for your views, I have tried the two types and they work.

    Thanks a million.

  11. #11
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: SQL INSERT error

    Quote Originally Posted by techgnome
    Ugh....

    Parameterized query folks.... Parameterized queries....
    VB Code:
    1. sSQL = "INSERT INTO movieInfo (title, actor, producer, comment) VALUES (?,?,?,?)
    2.  
    3. objCommand.CommandText = sSQL
    4. objCommand.CommandType = adCommandText
    5. objCommand.Parameters.Add objCommand.CreateParameter("title",advarchar,50, txtTitle.Text)
    6. objCommand.Parameters.Add objCommand.CreateParameter("actor",advarchar,50, txtactor.Text)
    7. objCommand.Parameters.Add objCommand.CreateParameter("producer",advarchar,50, txtproducer.Text)
    8. objCommand.Parameters.Add objCommand.CreateParameter("comment",advarchar,50, txtComment.Text)
    9.  
    10. objCommand.Execute
    Just change the 50 to suit your needs (it's the length of the field in the database.

    -tg

    edit: I think I also forgot a parameter or two in the createparameter function, so don't copy paste it directly.... I'm doing this off of memory here.
    Would the syntax for this, following this example, be this
    VB Code:
    1. sSQL = "INSERT INTO movieInfo (title, actor, producer, comment) VALUES ('" & txtTitle.Text & "', '" & txtActor.Text & "', '" & txtProducer.Text & "', '" & txtComment.Text & "')"
    2.  
    3. objCommand.CommandText = sSQL
    4. objCommand.CommandType = adCommandText
    5. objCommand.Parameters.Add objCommand.CreateParameter("title",advarchar,50, txtTitle.Text)
    6. objCommand.Parameters.Add objCommand.CreateParameter("actor",advarchar,50, txtactor.Text)
    7. objCommand.Parameters.Add objCommand.CreateParameter("producer",advarchar,50, txtproducer.Text)
    8. objCommand.Parameters.Add objCommand.CreateParameter("comment",advarchar,50, txtComment.Text)
    9.  
    10. objCommand.Execute sSQL
    Don't you need the sSQL after the .Execute?
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  12. #12
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] SQL INSERT error

    Its already assigned to the command object's CommandText property SeanK.

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

    Re: [RESOLVED] SQL INSERT error

    No, and the sSQL is wrong anyways.... in your example, there isn't any placeholders for the data... you've concatenated them back in... this thread is the perfect example of why string concatenation is not recommended for SQL strings.... as evidence has shown problems arise when string delimeters show up in the data. By using parameterized queries, the problem goes away as ADO will take care of the translation for you. Also prevents the possibility of a SQL Injection attack....

    -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??? *

  14. #14
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: [RESOLVED] SQL INSERT error

    Quote Originally Posted by techgnome
    No, and the sSQL is wrong anyways.... in your example, there isn't any placeholders for the data... you've concatenated them back in... this thread is the perfect example of why string concatenation is not recommended for SQL strings.... as evidence has shown problems arise when string delimeters show up in the data. By using parameterized queries, the problem goes away as ADO will take care of the translation for you. Also prevents the possibility of a SQL Injection attack....

    -tg
    Ok..geez. I have slapped myself.

    However, I don't understand, given all of this, how the VALUES clause would work. You posted
    VB Code:
    1. sSQL = "INSERT INTO movieInfo (title, actor, producer, comment) VALUES (?,?,?,?)
    2.  
    3. objCommand.CommandText = sSQL
    4. objCommand.CommandType = adCommandText
    5. objCommand.Parameters.Add objCommand.CreateParameter("title",advarchar,50, txtTitle.Text)
    6. objCommand.Parameters.Add objCommand.CreateParameter("actor",advarchar,50, txtactor.Text)
    7. objCommand.Parameters.Add objCommand.CreateParameter("producer",advarchar,50, txtproducer.Text)
    8. objCommand.Parameters.Add objCommand.CreateParameter("comment",advarchar,50, txtComment.Text)
    9.  
    10. objCommand.Execute
    Would I actually put question marks in the VALUES clause?
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  15. #15
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] SQL INSERT error

    Yes, and its interpreted as placeholders for parameters... the values of which are provided by the parameters collection of the command object.

    VB Code:
    1. objCommand.Parameters.Add objCommand.CreateParameter("title",advarchar,50, [b]txtTitle.Text[/b])
    2. objCommand.Parameters.Add objCommand.CreateParameter("actor",advarchar,50, [b]txtactor.Text[/b])
    3. objCommand.Parameters.Add objCommand.CreateParameter("producer",advarchar,50, [b]txtproducer.Text[/b])
    4. objCommand.Parameters.Add objCommand.CreateParameter("comment",advarchar,50, [b]txtComment.Text[/b])

    The replacement/update of the values is done by FIFO (first parameter object provides data for the first ?)

    After all parameters have values then the query executes without an error.

  16. #16
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: [RESOLVED] SQL INSERT error

    Quote Originally Posted by leinad31
    Yes, and its interpreted as placeholders for parameters... the values of which are provided by the parameters collection of the command object.

    VB Code:
    1. objCommand.Parameters.Add objCommand.CreateParameter("title",advarchar,50, [b]txtTitle.Text[/b])
    2. objCommand.Parameters.Add objCommand.CreateParameter("actor",advarchar,50, [b]txtactor.Text[/b])
    3. objCommand.Parameters.Add objCommand.CreateParameter("producer",advarchar,50, [b]txtproducer.Text[/b])
    4. objCommand.Parameters.Add objCommand.CreateParameter("comment",advarchar,50, [b]txtComment.Text[/b])

    The replacement/update of the values is done by FIFO (first parameter object provides data for the first ?)

    After all parameters have values then the query executes without an error.
    Ok...cool. I think I'm actually starting to get this (and like it )

    So, I would use advarchar for text. What would I use to insert values into a int field?

    Also, is this something that is confined to INSERTS or would I do the same thing with UPDATES?
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

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

    Re: [RESOLVED] SQL INSERT error

    adinteger .... when you press the , to move from the parameter name to the data type, you *should* be getting intellisense that will give you a list of the ad datatypes....

    you can do this on inserts, deletes, updates, and even selects....

    -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??? *

  18. #18
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: [RESOLVED] SQL INSERT error

    Quote Originally Posted by techgnome
    adinteger .... when you press the , to move from the parameter name to the data type, you *should* be getting intellisense that will give you a list of the ad datatypes....

    you can do this on inserts, deletes, updates, and even selects....

    -tg
    Cool.

    Is it possible for a thread to be closed twice by two different people?

    Thanks techgnome and leinad31. I learned a lot from this.

    And, thank you osemollie for asking the question in the first place.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

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