Solved

Add record to Access DB

Posted on 2006-11-28
3
277 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
  • 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
This is an explanation of a simple data model to help parse a JSON feed
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now