Results 1 to 4 of 4
  1. #1
    shani20 is offline Novice
    Windows 8 Access 2013
    Join Date
    Jan 2015
    Posts
    6

    Using Constraint statement in SQL

    I am using MS Access to do a project and I was trying to add CONSTRAINT to check the values being inserted but I kept getting syntax errors and I have looked everything to solve this but was not able to.



    ALTER TABLE Parts
    ADD CONSTRAINT chk_Category CHECK (Category IN (‘CPUs/Processors’, ‘Motherboards’, ‘Memory’, ‘Hard Drives’, ‘Video Cards/Video Devices’, ‘Power Supplies’, ‘Computer Cases’, ‘Fans/PC Cooling’))

    What did I wrong? I am trying to use SQL view on MS Access 2013.

    THanks

    Shani

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,633
    What you appear to be trying to do is enforce data validation for specific literal strings at table level. I don't think can set up this way, never seen. This is what lookup fields are for (I never build in table) and restricted comboboxes on form.

    Do these help
    http://www.w3schools.com/sql/sql_constraints.asp
    https://msdn.microsoft.com/en-us/lib...ffice.15).aspx
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    shani20 is offline Novice
    Windows 8 Access 2013
    Join Date
    Jan 2015
    Posts
    6
    Thanks - I just discovered that MS Access don't accept CONSTRAINT anyway, but the format that I had was correct one. So I will just use the format for homework and add the drop list on my own on MS Access.

    Shani

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849
    Shani,

    I have Acc 2010 and tried something I found online. Apparently this was available in 2007 according to the post I saw.

    You can add a CHECK constraint in Access, but you must do it directly in code. You can not use the query designer.

    I took a sample table in my database and added the Check constraint with this code entered directly in the immediate window.

    Code:
    CurrentProject.Connection.Execute "ALTER TABLE CityTbl ADD CONSTRAINT CheckSt3 CHECK(State IN ('Texas', 'Michigan'))"
    I tried adding a record for Oswego , New York and got the attached message.

    So the constraint was added and works as expected.

    Good luck with your project.
    Attached Thumbnails Attached Thumbnails CheckConstraint.jpg  

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

Similar Threads

  1. Replies: 5
    Last Post: 09-29-2014, 07:43 PM
  2. Replies: 2
    Last Post: 01-11-2014, 03:56 PM
  3. Referential integrity constraint
    By dsaxena15 in forum Access
    Replies: 3
    Last Post: 09-27-2012, 02:07 PM
  4. How do I display SQL constraint errors?
    By isabelle2378 in forum Forms
    Replies: 3
    Last Post: 10-19-2010, 03:23 PM
  5. On delete cascade fails in constraint clause
    By Victor EGBE in forum Queries
    Replies: 0
    Last Post: 03-02-2009, 09:52 PM

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