Results 1 to 3 of 3
  1. #1
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    310

    Jet CHECK Constraint

    Hi


    Check constraints are only at the table level. The example at the site:
    example: https://mskb.pkisolutions.com/kb/201888
    Shows how to create a new table with a check constraint and how to add a new record to the table that validate a constraint.

    1- Is it possible to UPDATE a (Yes/No) field in a table using a CHECK Constraint? Default value of the field is No as follows:
    Check for a Yes/No field, If Yes then Error Message if No then run some VBA code and after that update the value of the field from No to Yes ?

    2- What is the difference between having the code as a check constraint in a module or having the code as an Event Procedure?

    Thank you
    Khalil

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    See this info from W3Schools re Check Constraint.

    Here is an example to limit 7 records in table tblTestMaxRecs.

    Code:
    Currentproject.connection.execute "ALTER TABLE tblTestMaxRecs ADD CONSTRAINT MaxRecs CHECK ((SELECT Count(*) FROM tblTestMaxRecs) <=7);"
    Sample to remove the constraint

    Code:
    Currentproject.connection.execute "ALTER TABLE tblTestMaxRecs DROP CONSTRAINT MaxRecs;"

    I am not sure about your #1. You can use the w3schools info and try to construct the CONSTRAINT on a yes/no column???
    But, my guess is that a standard sub could accomplish your intended logic. Caution: Once you change the value to YES, that record would receive ERROR MESSAGE in subsequent processing (suggest you clarify the logic before proceeding).

    A CHECK constraint on a Table/column is applied/tested/executed by the database system "automatically" when there is an action against the table/column.
    An Event Procedure is run when the specific event is encountered/actioned based on the logic of your program/code.


    Sample code to demo your #2 using tblTestMaxRecs

    id pname checkcol
    19 record2 No
    23 record6 No
    24 record1 Yes
    25 hhhhh No
    26 ddd No

    Code:
    ' ----------------------------------------------------------------
    ' Procedure Name: changeCheck
    ' Purpose: demo routine to check value of CheckCol
    '             if YES then error message
    '             if NO  then run some vba, then update CheckCol to True/Yes
    ' Procedure Kind: Sub
    ' Procedure Access: Public
    ' Author: Jack
    ' Date: 30-May-24
    ' related to https://www.accessforums.net/showthread.php?t=89784&p=524794#post524794
    ' ----------------------------------------------------------------
    Sub changeCheck()
        Dim rs As DAO.Recordset, i As Integer
        Set rs = CurrentDb.OpenRecordset("tblTestMaxRecs")
        Do While Not rs.EOF
            If rs!CHECKCOL Then
                MsgBox "Error --was Yes " & rs!id
            Else                     'is NO/FALSE
                For i = 1 To 2       ' run some vba
                    Debug.Print i & "  " & rs!id
                Next i
                rs.Edit
                rs!CHECKCOL = True  'update the NO value to Yes
                rs.Update
            End If
            rs.MoveNext
        Loop
    End Sub
    Last edited by orange; 05-30-2024 at 07:15 AM. Reason: added demo

  3. #3
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    310
    Hi,
    Thank you for the reply.
    Related to your reply about :Sub changeCheck()
    I will be selecting one single record for the table based on a value from a combo box, check the value and apply the update based on the checkcol field value.

    Khalil

Please reply to this thread with any new information or opinions.

Similar Threads

  1. CHECK CONSTRAINT causing Syntax Error?
    By AishlinnAnne in forum SQL Server
    Replies: 18
    Last Post: 09-13-2016, 12:42 PM
  2. Using Constraint statement in SQL
    By shani20 in forum Programming
    Replies: 3
    Last Post: 01-27-2015, 09:48 PM
  3. Replies: 2
    Last Post: 01-11-2014, 03:56 PM
  4. Referential integrity constraint
    By dsaxena15 in forum Access
    Replies: 3
    Last Post: 09-27-2012, 02:07 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums