Solved

Add record to Access DB

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.

809 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