473,473 Members | 1,843 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 1281
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
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
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
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
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
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
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
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
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
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
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
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
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
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
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.