Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Add record to Access DB

Posted on 2006-11-28
3
Medium Priority
?
304 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 150 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
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 …
Starting up a Project

618 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