?
Solved

Question on inserting values into Access database using SQL

Posted on 2002-04-02
8
Medium Priority
?
151 Views
Last Modified: 2010-05-02
Hi,

I wish to find out how can I insert a record into Microsoft Access using SQL query string? I've got a record of 3 columns in myTable namely: Name, Address and TelNumber. Name and Address in String and TelNumber in Integer. I'm using Microsoft DAO Object to access the database. I'm able to use the SELECT query string to extract the data but I don't know how to insert data into the database.

This is what I've tried:

rs = db.OpenRecordset("INSERT INTO myTable VALUES(Name,Address,TelNumber)")

and of course it's not working :) I mean I believe there's something wrong with the parameters passed into the VALUES. Any help offered is very much appreciated!

I'm working on Microsoft Windows XP Pro, using Microsoft Access 2002 and Visual Basic 6.
Best Rgds,
Justin
0
Comment
Question by:s6192221
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 6912974
Use something like this instead

rs = db.OpenRecordset("INSERT INTO myTable VALUES('" & Name & "','" & Address & "','" & TelNumber & "')")

You need to actually send the values rather than the names.
0
 
LVL 2

Expert Comment

by:Hyper8
ID: 6912976


Set CONN = CreateObject("ADODB.Connection")
CONN.ConnectionString = Your connection string

SQL = "INSERT INTO myTable VALUES(Name,Address,TelNumber)"
CONN.Execute(SQL)
0
 
LVL 18

Expert Comment

by:mdougan
ID: 6912986
A few things, first, the database object has a method called Execute that is designed for executing "Action" queries that do not return recordsets.  Next, you have the column names in your Values clause, you should really have a separate column list clause, then you need to put valid values in the Value clause.  Let's say that you are getting all of this data off the screen, here is how your code might look:

Dim sSQL as String

sSQL = "INSERT INTO myTable (NAME, ADDRESS, TELNUMBER) VALUES ("
sSQL = sSQL & "'" & txtName.Text & "', "
sSQL = sSQL & "'" & txtAddress.Text & "', "
sSQL = sSQL & txtPhone.Text & ")"

db.Execute sSQL

Notice that the txtPhone.Text is not wrapped with single quotes.  Single quotes are only used for text fields.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:mdougan
ID: 6912987
A few things, first, the database object has a method called Execute that is designed for executing "Action" queries that do not return recordsets.  Next, you have the column names in your Values clause, you should really have a separate column list clause, then you need to put valid values in the Value clause.  Let's say that you are getting all of this data off the screen, here is how your code might look:

Dim sSQL as String

sSQL = "INSERT INTO myTable (NAME, ADDRESS, TELNUMBER) VALUES ("
sSQL = sSQL & "'" & txtName.Text & "', "
sSQL = sSQL & "'" & txtAddress.Text & "', "
sSQL = sSQL & txtPhone.Text & ")"

db.Execute sSQL

Notice that the txtPhone.Text is not wrapped with single quotes.  Single quotes are only used for text fields.
0
 
LVL 18

Accepted Solution

by:
mdougan earned 400 total points
ID: 6912991
A few things, first, the database object has a method called Execute that is designed for executing "Action" queries that do not return recordsets.  Next, you have the column names in your Values clause, you should really have a separate column list clause, then you need to put valid values in the Value clause.  Let's say that you are getting all of this data off the screen, here is how your code might look:

Dim sSQL as String

sSQL = "INSERT INTO myTable (NAME, ADDRESS, TELNUMBER) VALUES ("
sSQL = sSQL & "'" & txtName.Text & "', "
sSQL = sSQL & "'" & txtAddress.Text & "', "
sSQL = sSQL & txtPhone.Text & ")"

db.Execute sSQL

Notice that the txtPhone.Text is not wrapped with single quotes.  Single quotes are only used for text fields.
0
 

Author Comment

by:s6192221
ID: 6912998
Hyper8 , I'm using Microsoft DAO Object.
Thanks all I'll try it out :)
0
 

Author Comment

by:s6192221
ID: 6913042
Well done...... thanks.....!! I'm new to database :)
Glad you helped out :)
0
 
LVL 18

Expert Comment

by:mdougan
ID: 6913109
sorry for the multiple posts, EE did that, not me!  Thanks!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question