473,378 Members | 1,360 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,378 software developers and data experts.

msaccess sql insert command in vb.net

I'm trying to insert text values into a table in a MSAccess DB
the values are stored in string variables
Imports System.Data.OleDb ....
Dim name As String
Dim position As String
Dim site As String
Dim MDBConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source = C:\Test.mdb")
Dim cmd As New OleDbCommand("CREATE TABLE tblTest(site string,namex
string, positionx string)", MDBConn)
MDBConn.Open()
cmd.ExecuteNonQuery()
cmd.Connection = MDBConn
site = 'testsite'
name = 'testname'
position = 'testposition'
cmd.CommandText = "insert into tblTest (site, namex, positionx) VALUES
(site, name, position)"
cmd.ExecuteNonQuery()
which generates an error but if I use
cmd.CommandText = "insert into tblTest (site, namex, positionx) VALUES
('site', 'name', 'position')"
of course it puts the variable names into the table
any help would be appreciated

Dec 16 '05 #1
4 7631
Hi,

I think you are trying to insert the values in the variables into
the table. Try this instead

cmd.CommandText = String.format("insert into tblTest (site, namex,
positionx) VALUES ({0}, {1}, {2})", site, name, position)
Ken
------------------
"rudy" <rm****@umich.edu> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
I'm trying to insert text values into a table in a MSAccess DB
the values are stored in string variables
Imports System.Data.OleDb ....
Dim name As String
Dim position As String
Dim site As String
Dim MDBConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source = C:\Test.mdb")
Dim cmd As New OleDbCommand("CREATE TABLE tblTest(site string,namex
string, positionx string)", MDBConn)
MDBConn.Open()
cmd.ExecuteNonQuery()
cmd.Connection = MDBConn
site = 'testsite'
name = 'testname'
position = 'testposition'
cmd.CommandText = "insert into tblTest (site, namex, positionx) VALUES
(site, name, position)"
cmd.ExecuteNonQuery()
which generates an error but if I use
cmd.CommandText = "insert into tblTest (site, namex, positionx) VALUES
('site', 'name', 'position')"
of course it puts the variable names into the table
any help would be appreciated

Dec 16 '05 #2
hi rudy,
first i would say at least you should paste error so that one can
get an idea.

and now the problem i've seen in your code :

site = 'testsite'
name = 'testname'
position = 'testposition'

how come such assignment worked that i dotnt get it. first a single
quote represents comment in vb so u must get an error with such
statement. another thing is you should assign values like this.

site = "'testsite'"
name = "'testname'"
position = "'testposition'"

now let's c your query string.

cmd.CommandText = "insert into tblTest (site, namex, positionx) VALUES
(site, name, position)"

well here you have directly use the name of the variables . using
direct names of the variables will be considered as value input. u must
have to build query using those variables. like the way Ken has showed.
just check out his posting.

Dec 16 '05 #3
thanks ken this is what finally worked

cmd.CommandText = String.format("insert into tblTest (site, namex,
positionx) VALUES ('{0}', '{1}', '{2}')", site, name, position)

I really appreciate you and others who monitor these groups
an invaluable resource for newbies like me

Dec 16 '05 #4
thanks
the quotes were a typo

Dec 16 '05 #5

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

Similar topics

15
by: (Pete Cresswell) | last post by:
I've got a .BAT file that I use for executing various MS Access apps that I wrote way back in the days of 2.0. It's evolved over time, but it still contains a number of possible paths to...
2
by: Michel R. | last post by:
What`s wrong with this ??? The insert does not add into the database. Using a Winform and Database is sc.mdb Filename is supplier Fields SuppNbr Int64 SuppName String SuppPhone Int64...
1
by: ShailShin | last post by:
Hi All, I have to convert Excel data to MSAcess .MDB file using VB.NET. VB.NET Code read the Excel file and write it to .MDB file. For the same I have below code, but I am stuck at the writing it...
6
by: odbcmsaccess | last post by:
hi i 'm writng a code using msaccess with odbc. my dsn name is htgry i want to insert data eneterd in text fields to a table named qwer on clicking a command button. code as follows ...
2
by: tmarunkumarit | last post by:
I want to insert datetime values into msaccess using asp.net by vb.. Am getting error that Syntax Error in INSERT INTO statement... My query is strSQL="insert into group0...
1
by: vikramrawal | last post by:
hi, I am using C# with MSAccess XP. I need to insert bulk of records around 10000 in MSAccess. So obviously if I inserts each record individually then it will take lot of time. By using...
3
by: Icemokka | last post by:
Hi, I've got a table in MsAccess with 100+ fields. If I fill a tabletable with this table , change some values , get the update-command via commandbuilder , the update fails. This because the...
0
by: bbrewder | last post by:
I am struggling with some MSAccess automation issues. Basically, we have a .Net application that uses MSAccess for reporting (legacy code). We are able to launch MSAccess fine and even work with...
3
by: Tom van Stiphout | last post by:
On Thu, 30 Oct 2008 11:04:44 GMT, "JOHNNY OUTING" <outingjr@att.net> wrote: You managed to violate several netiquette rules in one post. Please review http://www.mvps.org/access/netiquette.htm...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.