Solved

MS Access (ADO): AddNew/Delete/Update vs. SQL statements

Posted on 2004-09-17
14
47,480 Views
Last Modified: 2013-12-25
I have an application that uses a MS Access database via Jet 4.0.

1) Which is better to implement, using the AddNew/Delete/Update way or using SQL statements, e.g. INSERT/DELETE/UPDATE to add, delete and update records in the database?

Please explain why and to give code snippets re:how to use the better implementation.

2) What type of error handling should be implemented when the database functions are unsuccessful?

If possible, please give code snippets.

Thanks!



0
Comment
Question by:halfondj
  • 5
  • 3
  • 2
  • +4
14 Comments
 
LVL 29

Assisted Solution

by:leonstryker
leonstryker earned 100 total points
ID: 12088755
Depends on your application.  I like using SQL, it is more flexiable and does not require a constent conneciton to the database.  in addition you can not .Update a recordset if it was based on a joined tables.

Leon
0
 
LVL 49

Assisted Solution

by:Ryan Chong
Ryan Chong earned 50 total points
ID: 12090386
Definitely is using SQL Statement, more faster (and no need to declare Recordset Object), but if you want to return the recordset object after a record is added, deleted or updated, you can consider to use AddNew/Delete/Update methods
0
 

Author Comment

by:halfondj
ID: 12091187
I tried using SQL, but was unsuccessful - do I have to use .BeginTrans, .CommitTrans, etc.?

I thought you had to declare a record set when doing an add/delete or update.

For the 500 points or a split, please provide code snippets.

Thanks.
0
 
LVL 18

Accepted Solution

by:
Sethi earned 275 total points
ID: 12091412
1. Direct SQL execution will be more optimal anytime as the execution takes place directly on the server and no resources are wasted for the recordset. You can find code snippets at the following links:
http://www.vbcode.com/asp/code.asp?lstCategory=Database
http://www.codeguru.com/resources/Database/

Also search www.planet-source-code.com for more examples on ADO code.

2. One should display an error message and stop further execution of code so that no more errors from that point are enountered. Errors related to database should not be taken lightly. So do not use Resume statements.
0
 

Author Comment

by:halfondj
ID: 12091490
Thanks for the reply.  The samples are great.

One more question: Is it good to use .BeginTrans, .CommitTrans, .Rollback, etc?  I have a very simple application and being new to DB programming, I don't know what's the best way of implmenting things.

If it's good to do it, please show an example.

Thanks again!
0
 
LVL 18

Assisted Solution

by:Sethi
Sethi earned 275 total points
ID: 12091531
I do not use it in my applications with Access :-) and no way with a small application.
0
 
LVL 2

Assisted Solution

by:x50Fade
x50Fade earned 75 total points
ID: 12099942
I sometimes use transactions with routines in my apps....

Only routines that are very delicate, and that must be able to "roll back" if something went wrong.

connection.begintrans
On error goto err_handler

'Code Here..........

connection.committrans
exit sub

err_handler:
connection.rollback
0
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.

 

Author Comment

by:halfondj
ID: 12101291
Thanks everyone for your replies.  They were all great.
0
 
LVL 12

Expert Comment

by:fulscher
ID: 12101323
halfondj,

Wheter you use SQL or VBA depends on your exact requirements.

In a distributed environment, SQL is probably preferred, since the code runs on the server. Other than previous posters, I found that VBA is in many cases faster than SQL since you can optimize the code for a specific task; the SQL engine can't do this since it doesn't know what you want to achieve.

Some Examples (off the top of my head, not verified):

SQL:   UPDATE MyTable SET FieldA = "bla" WHERE FieldB = "anykey"

VBA:   Dim RS as Recordset
          Set RS = CurrentDB.OpenRecordset("SELECT * FROM MyTable")
          RS.FindFirst "FieldB='anykey'"
          RS.Edit
          RS("FieldA") = "bla"
          RS.Update

Obviously, the VBA is longer and in this example, it might even be slower. However, if you need to iterate through a set of records, VBA will be faster:

SQL:
    SQL = "SELECT COUNT (FieldA) FROM MyTable";
    n = DoCmd.ExecSQL(SQL)
   
    For i = 1 to n
       SQL = UPDATE MyTable SET FieldA = "bla" & i WHERE FieldB = "anykey" & i
       DoCmd.ExecSQL SQL
    Next i

VBA:  
   Dim RS as Recordset
   Set RS = CurrentDB.OpenRecordset("SELECT * FROM MyTable ORDER By anykey")
   RS.MoveLast
   n = RS.RecordCount
   RS.MoveFirst
   While NOT RS.EOF
       RS.Edit
       RS("FieldA") = "bla" & i
       RS.Update
       RS.MoveNext
   Wend

Again, the VBA is longer, but I believe it will be significantly faster, because all the overhead of processing SQL is not required.
   
Transactions:
- can be used to increase performance, especially in a distributed environment.
- can be used to make sure that either all or no change on the DB will occur.
You wan't need transactions normally, however, they can come very handy in specific situations.

Hope this helps
Jan

0
 
LVL 18

Expert Comment

by:Sethi
ID: 12101329
Good to hear that...thanks for the points :-)
0
 

Author Comment

by:halfondj
ID: 12104914
To fulscher: Thanks for your explaination.  It was very well done.  I would have certainly given you points too. :).
0
 
LVL 12

Expert Comment

by:fulscher
ID: 12105205
Well, I was a bit late. Hope you can use some of the stuff, anyway.

Jan
0
 

Author Comment

by:halfondj
ID: 12106706
Definitely.  I owe you :).
0
 

Expert Comment

by:valli
ID: 13873436
i am doing one project vb with msaccess 97 ,i am connected all command add,select delete ,
this add commands for example i am add to the name via vb to msaccess that particular names are added ,but i want that name already here recently add a name not checked
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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 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

14 Experts available now in Live!

Get 1:1 Help Now