How to Add a Constraint in Access

How to Add a Constraint in Access thumbnail
Create a new table using VBA in Access.

Adding a constraint in Access is quite simple if you know a little about Visual Basic for Applications. VBA is a programming language used in Access and other Microsoft Office applications to automate routine tasks. A constraint is similar to an index. It helps you identify each row in a table with a unique value. A constraint can also be used to establish a relationship with other tables in your database.

Instructions

    • 1

      Launch Microsoft Access, click the "Database Tools," and click "Visual Basic" to launch the VB Editor window. Click the "Insert" menu and click "Module" to insert a new code module.

    • 2

      Type the following to start a new sub procedure:

      Private Sub createTableWithConstraint()

    • 3

      Copy and paste the following code to create a new table with three columns, one being a primary key constraint:

      DoCmd.RunSQL ("CREATE TABLE NewTable " _

      & "(FirstName CHAR, LastName CHAR, " _

      & "SSN INTEGER CONSTRAINT MyFieldConstraint " _

      & "PRIMARY KEY);")

    • 4

      End your sub procedure by typing "End Sub." Press "F5" to run the procedure.

Related Searches:

References

  • Photo Credit Thinkstock Images/Comstock/Getty Images

Comments

Related Ads

Featured