Solved

Add record to Access DB

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This is an explanation of a simple data model to help parse a JSON feed
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

863 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

19 Experts available now in Live!

Get 1:1 Help Now