Solved

Add record to Access DB

Posted on 2006-11-28
3
285 Views
Last Modified: 2010-04-16
Trying to insert a new record into an Access 2003 DB.   I have added a reference to M$ ActiveX Data Objects 2.5 Library

Using Code below that I got from another post here:
Dim conn As New ADODB.Connection

conn.ConnectionString = "Provider=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & App.Path & "\logger.mdb"
conn.Open

SQLstr = "INSERT INTO Log (Direction, Local) values ('1','2')"

conn.Execute SQLstr

I get no errors but no data is added.

0
Comment
Question by:MERCOMMS
[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
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
A_Ridiculously_Long_Unique_Name earned 50 total points
ID: 18033983
There are two methods to insert data into an access database.

One method is using the SQL Execute command as described in your post

The second is using DAO or ADO.

CurrentDb is an alias for the active access database. Assuming you are developing a single mdb, CurrentDb is good for you.

Eg: If you want to add a record to the table "mytable", which contains a field "FieldName" you may use the following syntax



public sub AddRecord()

dim db as database
dim rs as recordset
dim sql as string

sql = "select * from mytable"

set db = CurrentDb.OpenRecordset(sql)
Set rs = CurrentDb.OpenRecordset(sql)
rs.addnew
rs!fieldname = "Cat"
rs.update

end sub



Alternatively the batch mode "execute" has it's pros and cons. In my opinion the syntax is more difficult for this method if you are passing complex string values to the fields.

dim fieldvalue as string
fieldValue = "Cat"
CurrentDb.Execute "INSERT INTO mytable (fieldname) VALUES (" & Chr(34) & fieldValue & Chr(34) & ")"



PS: I assume you don't have open transactions which you have neglected to commit???



0
 

Author Comment

by:MERCOMMS
ID: 18037182
Thanks A_Ridiculously_Long_Unique_Name

I figured it out yesterday.  I had previously added an ADODC control to the form and tied the text boxes to it.  I was then pulling the .text value from those text boxes to build the SQL statement.  Well with the database empty the first run, it pulled null values and therefore populated the first record with null values.  On each subsequent test the text boxes were populated with null values and therefore inserting additional blank records into the DB.  I just never paid attention to the fact that after each test additional empty records were added.

I removed the ADODC control from the form and unbound the text boxes and all is working fine.

0
 
LVL 1
ID: 18040575
Also for anyone else reading, the above line in the sample code should have read

set db = CurrentDb

NOT

set db = CurrentDb.OpenRecordset(sql)


Also - there is a third method, which is using a "bound" form like Mercomms did.

0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

This is about my first experience with programming Arduino.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

738 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