Append/Update queries that violate Required or AllowZeroLength or PrimaryKey rules do not trigger error messages when executed from VBA. Is there a way to correct this?

This presents a major threat.  Suppose you archive data from TableXYZ to TableABC and then purge from TableXYZ the records you assume have been transferred.  BIG MISTAKE. TableABC will be missing the violating records, and those records will have been deleted from TableXYZ, forever.

For example:

INSERT INTO TableABC ( aPrimaryKeyFieldWithNullsAndEmptyStringsForbidden)
SELECT AFieldContainingNullsOrEmptyStrings
FROM TableXYZ;

will trigger an error message when run from the QBE screen if TableXYX contains nulls or empty strings, or if it attempts to insert a value that violates TableABC's primary key. However, the following VBA code will let you believe everything is OK when, in fact, not all records from TableXYZ were appended:

Sub test()
   Dim sql As String
   Dim rs As DAO.Recordset
   sql = ""
   sql = sql & "INSERT INTO TableABC ( aPrimaryKeyFieldWithNullsAndEmptyStringsForbidden ) "
   sql = sql & "SELECT AFieldContainingNullsOrEmptyStrings "
   sql = sql & "FROM TableXYZ"
   CurrentDb.Execute sql
End Sub

musgahAsked:
Who is Participating?
 
Rey Obrero (Capricorn1)Connect With a Mentor Commented:
if you want to use the .execute method, you need to use the option dbfailonerror

currentdb.execute sql,dbfailonerror

you can also check for records affected

see this link for more info

http://msdn.microsoft.com/en-us/library/ff197654.aspx
0
 
YohanFConnect With a Mentor Commented:
what about trying following:

 DoCmd.SetWarnings true
 DoCmd.RunSQL SQL
0
 
JezWaltersConnect With a Mentor Commented:
Or:

    CurrentDb.Execute sql, dbFailOnError
0
 
musgahAuthor Commented:
Thanks a lot.  All answers were very helpful.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.