473,480 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL statement in Access



I am trying to write an insert statement in Access but it is not working
with the OleDBCommand object.
HEre is the code

string sqltxt= "INSERT INTO contacts ( Last_Name, First_Name,
Email_Address, [Note] )" +
" WHERE (Last_Name)='Testlast') AND (First_Name)='TestFirst') AND
(Email_Address)='TestEmail') AND (Note)='TestNote')";
Debug.WriteLine(sqltxt);

OleDbConnection myConnection = new OleDbConnection(connectString);
OleDbCommand myCommand = new OleDbCommand(sqltxt, myConnection);
myCommand.Connection.Open();
int ret = myCommand.ExecuteNonQuery()

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
4 1282
Hi,

I think that the error is in your query, INSERT INTO ... WHERE .. ??

you are mixing INSERT with UPDATE syntax.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"John Sutor" <jo********@cinfin.com> wrote in message
news:O1**************@TK2MSFTNGP15.phx.gbl...


I am trying to write an insert statement in Access but it is not working
with the OleDBCommand object.
HEre is the code

string sqltxt= "INSERT INTO contacts ( Last_Name, First_Name,
Email_Address, [Note] )" +
" WHERE (Last_Name)='Testlast') AND (First_Name)='TestFirst') AND
(Email_Address)='TestEmail') AND (Note)='TestNote')";
Debug.WriteLine(sqltxt);

OleDbConnection myConnection = new OleDbConnection(connectString);
OleDbCommand myCommand = new OleDbCommand(sqltxt, myConnection);
myCommand.Connection.Open();
int ret = myCommand.ExecuteNonQuery()

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
You don't have a values section in your INSERT statement.

INSERT INTO contacts(Last_Name, First_Name) VALUES(lastname, firstname)

--
Gerry O'Brien
Visual Basic .NET MVP
"John Sutor" <jo********@cinfin.com> wrote in message
news:O1**************@TK2MSFTNGP15.phx.gbl...


I am trying to write an insert statement in Access but it is not working
with the OleDBCommand object.
HEre is the code

string sqltxt= "INSERT INTO contacts ( Last_Name, First_Name,
Email_Address, [Note] )" +
" WHERE (Last_Name)='Testlast') AND (First_Name)='TestFirst') AND
(Email_Address)='TestEmail') AND (Note)='TestNote')";
Debug.WriteLine(sqltxt);

OleDbConnection myConnection = new OleDbConnection(connectString);
OleDbCommand myCommand = new OleDbCommand(sqltxt, myConnection);
myCommand.Connection.Open();
int ret = myCommand.ExecuteNonQuery()

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
This did not work.

string sqltxt = "Insert Into contacts (customerid, companyname,
contactname) Values (" + "'" + sLast + "'" + "," + "'" + sFirst + "'" +
"," +
"'" + sEmail + "'" + "," + "'" + sNote + "'" + ")" ;

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
are you sending correct number of values as parameters check your commas
in the prepared statement write your sqltxt as output and check..

--
rajagopal

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
8286
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
7
35657
by: Bob | last post by:
Currently I am using this statement to translate 3 fields in my db thru Visual Basic. I import the data from one table to another then call the IFF statements and the NewDate to translate the...
11
50266
by: Colleyville Alan | last post by:
I posted that I was having trouble with a SQL statement that was working in the SQL window, but not in VBA. I have since discovered that when I create the string in VBA it is over 1023 characters...
14
6075
by: Ryan | last post by:
I want to do the following SQL statement in Access. However, it won't allow me to have the secondary part of my join statement and tells me that this is not supported. OK, so Access doesn't support...
3
4599
by: Andy_Khosravi | last post by:
I have been trying to build a user friendly search engine for a small database I have created. I'm having some particular problems with one of my date fields. Here's the setup: I'm using...
7
6633
by: kosta | last post by:
hello! one of my forms communicates with a database, and is supposed to add a row to a table using an Insert statement... however, I get a 'oledb - syntax error' exception... I have double...
10
2345
by: John Smith | last post by:
Can you do a Select Statement within a Select Statement? I want to build a query similar to queries built in Access which link to other queries but using only SQL Statements. Is it possible? If...
6
2676
by: FayeC | last post by:
I really need help figuring this out. i have a db with mostly text fields but 2. The user_id field is an autonumber (key) and the user_newsletter is a number (1 and 0) field meaning 1 yes the ...
4
3012
by: Dave | last post by:
I have a field with data type of memo in my access database. I run a query from c# to access this field and it cuts off my string to only 255 characters. If i run this query directly in access...
7
2671
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7045
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,...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4483
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2985
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.