ACC2000: How to Create a Jet CHECK Constraint

This article was previously published under Q201888
This article has been archived. It is offered "as is" and will no longer be updated.
Advanced: Requires expert coding, interoperability, and multiuser skills.

Summary
The new SQL grammar exposed by Microsoft Jet database engine version 4.0 allowsusers to specify business rules that can span more than one table. These are calledcheck constraints and allow users to exceed a single validation rule per table. In thisarticle, a procedure is provided that demonstrates creating a table with aCHECK constraint. It also tests this constraint by attempting to add tworecords, one which conforms to the constraint, and one that intentionallyviolates the constraint.
More information
To demonstrate the example procedure, follow these steps:
  1. In a new Microsoft Access 2000 database, create a new module.
  2. On the Tools menu, click References, and make sure the following references are checked:
    • Microsoft ActiveX Data Objects 2.1 Library
    • Microsoft ADO Ext. 2.1 for DDL and Security
  3. In the new module, type the following:
    Sub CreateJetConstraint()Dim ADOConnection As New ADODB.ConnectionDim SQL As StringDim ADOXCat As New ADOX.Catalog On Error GoTo ErrorHandler'Delete the sample database JetContraint is it already exists.    Kill "c:\JetContstraint.mdb"' Using ADOX create a new Jet database.ADOXCat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data " & _ "Source=c:\JetContstraint.mdb" ' Set the ADO Connection Properties and open the database.  With ADOConnection     .Provider = "Microsoft.Jet.OLEDB.4.0"     .Open "Data Source=c:\JetContstraint.mdb"' Create a new table called CreditLimit.     .Execute "CREATE TABLE CreditLimit (CreditLimit DOUBLE);"     .Execute "INSERT INTO CreditLimit VALUES (100);"  End With  ' Create a new table called Customer with a check constraint which  ' validates that a new customers Credit Limit does not exceed the  ' credit limit in the CreditLimit table. SQL = "CREATE TABLE Customers (CustId IDENTITY (100, 10), "   SQL = SQL & _  "CFrstNm VARCHAR(10), CLstNm VARCHAR(15), CustomerLimit DOUBLE, " SQL = SQL & "CHECK (CustomerLimit <= " & _  "(SELECT SUM (CreditLimit) FROM CreditLimit)));" ADOConnection.Execute SQL  ' Add a new record that does not violate the Customers  ' check constraint. SQL = "INSERT INTO Customers (CLstNm, CFrstNm, CustomerLimit) VALUES " SQL = SQL & "('Smith', 'John', 100);" ADOConnection.Execute SQL ' Try to add a second record that violates the Customers check  ' constraint and results in an error. SQL = "INSERT INTO Customers (CLstNm, CFrstNm, CustomerLimit) VALUES " SQL = SQL & "('Jones', 'Bob', 200);" ADOConnection.Execute SQL  Exit Sub  ErrorHandler:'Trap for File not found error. If Err = 53 Then     Resume Next End If MsgBox Error & " Error# " & Err Resume Next End Sub					
  4. Type the following line in the Immediate window:
    CreateJetConstraint						
    Note the following error, demonstrating the CHECK constraint isworking:
    One or more values are prohibited by the validataion rule'Check_EC_57C7D4_11D2' set for 'Customers'. Enter a value that theexpression for the field can accept. Error# -2147467259
  5. Open the database C:\JetConstraint and open the table Customers.Note that the record for John Smith, which conforms to the constraint,was added, while the record for Bob Jones, which violated the constraint, was not added.
Properties

Article ID: 201888 - Last Review: 10/25/2013 23:29:00 - Revision: 4.0

  • Microsoft Access 2000 Standard Edition
  • kbnosurvey kbarchive kbhowto kbprogramming KB201888
Feedback