SQL String Error

INSERT INTO SQL String Error

Page 1 of 1

5 Replies - 1216 Views - Last Post: 08 May 2009 - 11:55 PM Rate Topic: -----

#1 UnknownCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 137
  • Joined: 22-February 09

SQL String Error

Posted 07 May 2009 - 02:21 PM

I keep getting sql string errors when I try to add records to an access database. I am using the following code:

strSQL = "INSERT INTO Thoughts_Feelings" & "(Date, Time, Situation, Thoughts, Feelings)VALUES " & "('" & DateTimePicker1.Value & "'," & "'" & TextBox1.Text & "','" & TextBox2.Text & "', " & "'" & TextBox3.Text & "','" & TextBox4.Text & "')"


Is This A Good Question/Topic? 0
  • +

Replies To: SQL String Error

#2 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: SQL String Error

Posted 07 May 2009 - 06:37 PM

Try this. You had excess apostrophes and quotes and dates must be delimited by # symbol if saving to a field defined as date/time datatype. Also if any of the textbox values are actually numbers going into numeric datatype fields, remove the apostrophe delimiters.

strSQL = "INSERT INTO Thoughts_Feelings(Date, Time, Situation, Thoughts, Feelings) VALUES (#" & DateTimePicker1.Value & "#, #" & TextBox1.Text & "#, '" & TextBox2.Text & "', '" & TextBox3.Text & "', '" & TextBox4.Text & "')"

This post has been edited by June7: 07 May 2009 - 06:41 PM

Was This Post Helpful? 0
  • +
  • -

#3 UnknownCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 137
  • Joined: 22-February 09

Re: SQL String Error

Posted 08 May 2009 - 04:21 AM

Still won't work mate. This worked fine in my other application but with this it keeps giving me errors on the INSERT INTO command of the SQL string.
Was This Post Helpful? 0
  • +
  • -

#4 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: SQL String Error

Posted 08 May 2009 - 01:51 PM

Post the exact error message verbatim.
Was This Post Helpful? 0
  • +
  • -

#5 rgfirefly24  Icon User is online

  • D.I.C Lover
  • member icon


Reputation: 267
  • View blog
  • Posts: 1,477
  • Joined: 07-April 08

Re: SQL String Error

Posted 08 May 2009 - 03:49 PM

Also to help you out in figuring out whats wrong, try and use paramaters instead of inline like you are.


Try this and see if by putting in a breakpoint at the beginning before the query you can figure out exactly which one is causing the error:


//this is your string
strSQL = "INSERT INTO Thoughts_Feelings(Date, Time, Situation, Thoughts, Feelings) VALUES (#" & DateTimePicker1.Value & "#, #" & TextBox1.Text & "#, '" & TextBox2.Text & "', '" & TextBox3.Text & "', '" & TextBox4.Text & "')"
//change it to this and then use .Parameters.Addwithvalue for each of the parameters
strSQL = "INSERT INTO Thoughts_Feelings(Date, Time, Situation, Thoughts, Feelings) VALUES(@Date,@Time,@Situation,@Thoughts,@Feelings)"




Also i'm going to assume that Time is a Date/Time field in your database since your using the # symbols to send it. What your passing is a string. You'll need to format the string to a date/time like follows:

Format(textbox1.text, "HH:MM:SS")
Was This Post Helpful? 0
  • +
  • -

#6 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: SQL String Error

Posted 08 May 2009 - 11:55 PM

Good point about the Time data. I have never saved times only dates so didn't hit on that. You can format by code as rqfirefly24 suggests or if you use mask in your textbox, use the option to retain mask symbols in the value.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1