Set the Validation Rule of a column in a Microsoft Access database, in .NET

132 views Asked by At

After adding a new column to a table in a Microsoft Access database, how can you set its Validation Rule and Validation Text?

My current workaround is a check constraint on the table.

The code uses System.Data.OleDb classes from .NET Framework 1.1, and the database engine is Microsoft Jet 4.0 (it is a very old application).

1

There are 1 answers

0
Gord Thompson On

You can use Jet DAO if you add the "Microsoft DAO 3.6 Object Library" COM reference to your .NET project. Then you can do something like this:

var dbEngine = new DAO.DBEngine();
DAO.Database db = dbEngine.OpenDatabase(@"C:\Users\Public\mdbTest.mdb");
DAO.TableDef tbd = db.TableDefs["rule"];
DAO.Field fld = tbd.Fields["rule_number"];
fld.ValidationRule = "<> 6";
fld.ValidationText = "There is NO rule 6!";
db.Close();