Create a table with validation rule : Table Create « Access « VBA / Excel / Access / Word






Create a table with validation rule

 

Sub exaCreateTable()
    Dim db As Database
    Dim tblNew As TableDef
    Dim fld As Field
    
    Set db = CurrentDb
    
    Set tblNew = db.CreateTableDef("NewTable")
    Set fld = tblNew.CreateField("NewField", dbText, 100)
    
    fld.AllowZeroLength = True
    fld.DefaultValue = "Unknown"
    fld.Required = True
    fld.ValidationRule = "Instr$(Like 'A*' or Like 'Unknown'"
    fld.ValidationText = "Known value must begin with A"
    tblNew.Fields.Append fld
    db.TableDefs.Append tblNew
End Sub

 








Related examples in the same category

1.Creating a Table (ADOX data types vs. Microsoft Access data types)
2.Creating a Table in the Current Database with SQL statement
3.Creating a Table in a New Database with AUTOINCREMENT column
4.Creating a Table with a Single-Field Index with SQL command
5.Adding a Single-Field Index to an Existing Table with SQL command
6.Creating an Index Based on Two Fields with SQL command
7.Default Column value
8.Creating Check Constraints:add business rules for a table