Question

I have multiple forms that write to the same table. I want all data written to that table to be validated with the same rules. I can not find a way to apply the validation at the table level. It looks like it will have be done on a per-form basis using BeforeUpdate to call my vba.

I am running Access 2013. The validation of some fields will be based on the contents of other fields in the same record. I will need to do string manipulations along the lines of the following pseudo-code:

for field.Serial; do
  if ( field.Model == "FORKLIFT" ); then
    validation_good_if( (left(field.Serial, 2) == "FL") && (length(field.Serial) == 5) && (right(field.Serial, 3) isNumeric) )
endFor

for field.AssetTag; do
  field.AssetTag = right("0000" & field.AssetTag, 8)
endFor

for field.Model, field.Location; do
  toUpperCase
endFor

What is the best way to make sure all inserts/updates to my table undergo the same validation?

Was it helpful?

Solution

This question sounds like a good candidate for a Data Macro. Here's what I came up with:

enter image description here

I also tested this by trying to perform an INSERT from an external program (C#, using ODBC) and the Data Macro works in that context, too:

cmd.CommandText = "INSERT INTO Table1 ([Model], [Serial]) VALUES (?,?)";
cmd.Parameters.AddWithValue("?", "FORKLIFT");
cmd.Parameters.AddWithValue("?", "FL21");
try
{
    cmd.ExecuteNonQuery();
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}

ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver] Validation failed for FORKLIFT: Serial Number must be five (5) characters long.

When the validation succeeds the record is correctly inserted into the table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top