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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

YohanFCommented:
what about trying following:

 DoCmd.SetWarnings true
 DoCmd.RunSQL SQL
0
JezWaltersCommented:
Or:

    CurrentDb.Execute sql, dbFailOnError
0
Rey Obrero (Capricorn1)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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
musgahAuthor Commented:
Thanks a lot.  All answers were very helpful.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.