473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

SQL to Access insert into statement

Ok, so I am getting the following error when I run the code at the bottom of this page. I am using Visual Studio 2010 Professional, Access 2000 .mdb database. Any suggestions on why this doesn't work would be great!

Error:

System.Data.OleDb.OleDbException (0x80040E14): Syntax error in INSERT INTO statement.
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextE rrorHandling(OleDbHResult hr)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextF orSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText( Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(Comm andBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderIntern al(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
at Kitchen_Helper.Calendar.btnCalendarSave_Click(Obje ct sender, EventArgs e) in E:\CINS 237\Kitchen Helper\Kitchen Helper\Kitchen Helper\Calendar.vb:line 344

Expand|Select|Wrap|Line Numbers
  1.  Try
  2.             If txtCalendarEntry.Text = "" Then
  3.                 MsgBox("Please enter a message!", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "Error")
  4.                 Exit Sub
  5.             End If
  6.  
  7.             con.Open()
  8.             Dim mySQL As String =
  9.                 "INSERT INTO Calendar (Selected Date, User, Note) VALUES ('" &
  10.                     txtNote.Text & "','" &
  11.                     txtUserName.Text & "','" &
  12.                     txtCalendarEntry.Text & ")"
  13.  
  14.             Dim rs As New OleDb.OleDbCommand(mySQL, con)
  15.             MsgBox(mySQL)
  16.             rs.ExecuteNonQuery()
  17.             con.Close()
  18.             RefreshGrid("")
  19.         Catch ex As Exception
  20.             MsgBox(Convert.ToString(ex))
  21.         Finally
  22.             con.Close()
  23.         End Try
  24.  
Apr 21 '11 #1
8 3081
Rabbit
12,516 Expert Mod 8TB
Your selected date field has a space in its name. Either there is no space or there is one and you need to demarcate it as a field with square brackets. Also, your values that you're inserting do not match the column order that you specified.
Apr 21 '11 #2
Ok, I am new to SQL, so could you please elaborate on "demarcate" the field with brackets? Does that mean this...
{Selected Date}
It does have a space in the access table header. Also, the fields are denoted corrected, i know they are labeled weird, but I had to "fanagle" them to make it work with what I had.
txtNote.Text = (just for testing) Date
txtUserName.Text = User Name
txtCalendarEntry.Text = Note
I know it sounds dumb, but it would make sense if you saw the gui or the way it was setup. I will attempt to edit the "Selected Date" thing. :\
Apr 22 '11 #3
Rabbit
12,516 Expert Mod 8TB
Close, instead of curly brackets, use the square brackets.
Apr 22 '11 #4
Ok, just tried that, same error message, also added an ' at the end for possible syntax... like this :

Expand|Select|Wrap|Line Numbers
  1. txtCalendarEntry.Text & "')"
  2.  
Any other possible ideas by chance? This is driving me loopy! lol.
Apr 22 '11 #5
Rabbit
12,516 Expert Mod 8TB
The line breaks are just for our benefit right? Because you can't have line breaks in a statement unless you use an underscore.

Also, I assume there are no single quotes in the input data because otherwise you would have to escape it.

And have you output the string to verify it? And did you try running that string directly in SQL?
Apr 22 '11 #6
The line breaks are just for our benefit right? Because you can't have line breaks in a statement unless you use an underscore.

The line breaks are there, but they return the same output whether they are on the same line or not (I moved them all onto the same line to double check, but yeah I thought it was weird to that it worked)

Also, I assume there are no single quotes in the input data because otherwise you would have to escape it.

Forgive my ignorance, but can you explain this a little further? Their shouldnt be any quotes in the searchable values, but I don't quite understand the question.

And have you output the string to verify it? And did you try running that string directly in SQL?

That is the msgbox(mySQL) part at line 15. Here is what the output string says...

* - denotes what the user input in the fields, not a wildcard or literal character.
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO Calendar (Occurs, User, Note) VALUES ('*', '*', '*')
  2.  
Apr 22 '11 #7
Rabbit
12,516 Expert Mod 8TB
As far as escaping quotes goes, assume someone has the name O'Connor. What the string would look like then is
'O'Connor'

I'm sure you can see how that would mess up the SQL. So to escape the single quote, you need to do this.
'O''Connor'

The resulting string looks fine. Did you try running it in Access?
Apr 22 '11 #8
It has been resolved! It was something to do with the declaration! Lucky guess (or not so lucky considering it took almost 7 hours of failure!!!) But thank you anyway! See below for final code.

Expand|Select|Wrap|Line Numbers
  1. Dim rs As New OleDb.OleDbCommand(
  2.                 "INSERT INTO tblCalendar ([Occurs], [User], [Note]) VALUES ('" &
  3.                 dtpCalendar.Value & "','" &
  4.                 txtUserName.Text & "','" &
  5.                 txtCalendarEntry.Text & "')", con)
  6.  
Apr 23 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: middletree | last post by:
I am used to SQL Server, no Access, but this one thing has to be done in Access. Can you tell me if this query will work, based on the syntax? I am trying create a new row on the database, in one...
2
by: terpatwork | last post by:
Hi, (1) I have an access form that allows users to enter data, and when they click a button, the OnClick code that I've written uses a SQL INSERT statement to insert the data into the database. I...
3
by: alexmaster_2004 | last post by:
hi i have made an application using C# that access sql2000. this application is just used to insert data to the database. i use something like this in my code: // string colmnA = TextBox1.Text;...
6
by: Kathy Burke | last post by:
Ugh. I'm using the following in an asp.net. I get an Syntax Error in INSERT INTO Statement on line Cmd1.ExecuteNonQuery(). I've made all my database fields text (just to eliminate that as a...
1
by: Lee Holsenbeck | last post by:
hi, i am trying to write records from an ASPX page to a access database, but get an error on my insert statement, but not on a select statement. please let me know if you can see what i'm doing...
2
by: Paul Mason | last post by:
Hi folks, The ado.net stream appears to be not working so I'm here. The following function generates the error "Operation must use an updateable query". There is no identifiable or meaningful...
2
by: Geoffrey KRETZ | last post by:
Hello, I'm wondering if the following behaviour is the correct one for PostGreSQL (7.4 on UNIX). I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching the following request :...
7
by: Cindy H | last post by:
Hi I'm having a problem getting the insert statement correct for an Access table I'm using. The Access table uses an autonumber for the primary key. I have tried this: INSERT INTO Tournaments...
6
by: BaWork | last post by:
I have the following to insert a new record: <% .. Set Conn = Server.CreateObject("ADODB.Connection") Conn.Open "DSN=qqqqq;" SQLStmt = "INSERT INTO tbl_qqqqqq (main_cat, cat_fee, fee_amount)...
3
by: Dave | last post by:
I have an old web app that ues an Access database and ASP 3.0. I need to build an INSERT statement based on the contents of a form. What is the best way to handle blank text boxes that are...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.