简体   繁体   中英

In MS-Access is there any way to bypass/override/easily temporarily disable data validation of an INSERT SQL statement

We have many tables with lots of fields that have data validation on them. When running an SQL insert statement to append imported data to these tables in the event of a data validation error they fail with the extremely helpful error message "27 records could not be added do to validation rule violations".

Tracking down these violations is very time consuming, so I am wondering if there is a way to do ANY of the following:

1) Find out the record and field(s) that caused the validation error programmatically

2) Override data validation during import so that people can afterwards look through the table to find the issues highlighted

3) Disable and then re enable validation for the same effect (short of looping through all columns, disabling data validation while saving the settings, then after the insert loop through again and re enable it for each column).

4) Some other suggestion I haven't thought of because it is past 10:00 pm here.

Any help would be very much appreciated.

No, Access does not allow validation rules to be disabled or overridden.

A separate import table is the best alternative that would facilitate possibilities #1 and #2.

  • Copy the destination table structure to a new table. You can do this with Copy (Ctrl+C) and Paste (Ctrl+V) within the navigation panel.
  • Alter the copied table definition by removing all validation rules and constraints.
    • Pay special attention to the primary key for the destination table.
      • If the primary key is an AutoNumber column, then go ahead and keep this column in the import table as the primary key. Just be aware that this value will be an arbitrary, temporary number used only for import row identification. Eventually the destination table will have a new unique value assigned.
      • If the primary key is composed of meaningful data columns, I suggest removing the primary key and unique constraint. Instead, add an additional AutoNumber primary key column just for importing.
      • If there is no primary key, you probably want to consider adding one and reviewing proper table normalization. As before, at least add an AutoNumber primary key as already described.

Define an import process. Some or all of this could be automated via code / macros.

  • Clear import table of existing data.
  • Import external data into the import table.
  • Copy rows from the import table to the permanent table.
    • The easiest method is probably an SQL INSERT INTO statement. Within the Access Query designer, an Append Query is the same thing as the INSERT INTO statement.
    • Alternative methods are to use VBA to loop through rows via RecordSet objects.
  • Run one or more comparison queries to detect and report rows which failed the copy operation.
    • This assumes that the data has a unique field or fields that can be used to distinguish rows from each other, both for the imported data as well as existing data in permanent table.
      • If the data does NOT have identifying fields, the only viable option is probably to loop through a RecordSet object in VBA code since a RecordSets can be used to select and operate on individual rows. But SQL statements operate on entire sets of data and rely on unique values to distinguish between rows of data.
      • Aside from problems verifying imported data, one should probably question the general usefulness and/or validity of the data rows if they have no individual identity.
  • For any import row which fails the copy operation, do one or more of the following, recursively improving the process and building a set of validations (queries and/or code) that you can automate.
    • Manually inspect data and try to determine failure reasons.
    • Write a validation query or set of validation queries which identify import rows that do not satisfy validation rules or constraints of the permanent table.
    • Possibly write additional queries which "massage" data into the permanent table, or which report on bad data to be fixed manually or researched further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM