473,662 Members | 2,704 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Ole Db ....
Dim name As String
Dim position As String
Dim site As String
Dim MDBConn As New OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;
Data Source = C:\Test.mdb")
Dim cmd As New OleDbCommand("C REATE TABLE tblTest(site string,namex
string, positionx string)", MDBConn)
MDBConn.Open()
cmd.ExecuteNonQ uery()
cmd.Connection = MDBConn
site = 'testsite'
name = 'testname'
position = 'testposition'
cmd.CommandText = "insert into tblTest (site, namex, positionx) VALUES
(site, name, position)"
cmd.ExecuteNonQ uery()
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 7644
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.e du> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.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.Ole Db ....
Dim name As String
Dim position As String
Dim site As String
Dim MDBConn As New OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;
Data Source = C:\Test.mdb")
Dim cmd As New OleDbCommand("C REATE TABLE tblTest(site string,namex
string, positionx string)", MDBConn)
MDBConn.Open()
cmd.ExecuteNonQ uery()
cmd.Connection = MDBConn
site = 'testsite'
name = 'testname'
position = 'testposition'
cmd.CommandText = "insert into tblTest (site, namex, positionx) VALUES
(site, name, position)"
cmd.ExecuteNonQ uery()
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
11540
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 MSACCESS.EXE, tries them all, takes the first one it finds, and dies if none is found. Seems to me like at some time or another I was able to clone this .BAT file and implement it with no path. Something about MSACCESS being a registered application.
2
4327
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 Here is the insert Class:
1
11354
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 to .MDB file. which I have already created with Table as Terms which contains 10 Fields and my Excel contains 10 Fields, Here I want to Extract 3 columns data from Excel to Table. Also it shows error of "syntax error in INSERT INTO Command. ...
6
2345
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 Private Sub Command14_Click()
2
3432
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 (name,addressline1,addressline2,landmark,city,state,pincode,telephone1,telephone2,altcontactno,businesscategory,product,category,date) values('" & txtname.text & "','" & txtadds1.text & "','" & txtadds2.text & "','" & txtland.text & "', '" & txtcity.text & "', '" &...
1
2971
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 OleDbDataAdapter and setting its UpdateBatchSize property we can insert bulk of records in single trip to database but this property is not supported for MSAccess because I think MSAccess not support batch update. So is there any other way to achive this? ...
3
2001
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 commandbuilder adds a where clause that contains all the fields in the datatable ( to check whether the record has been changed since the fill ). Because MSAccess does not allow more then 99 fields in the where clause, the update fails offcourse.
0
2018
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 the MSAccess COM objects to run our reports (using GetObject(<MDB Path>) - see http://support.microsoft.com/default.aspx?scid=kb;en-us;317113&Product=vbNET for info on how to do this). The problem that we are running into is that if our Access...
3
2967
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 This is a text-only newsgroup (you could have captured the error message with Ctrl+C), and if you can't be bothered to check back for messages, why would we make the effort to answer in the first place? RunCode is designed to call a public...
0
8435
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8768
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8547
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7368
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
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 we have to send another system
2
1754
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.