Link to home
Start Free TrialLog in
Avatar of musgah
musgahFlag for Afghanistan

asked on

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

SOLUTION
Avatar of YohanF
YohanF
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Jez Walters
Jez Walters
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of musgah

ASKER

Thanks a lot.  All answers were very helpful.