I am trying to create a check constraint on an access (jet?) table.
So, I open the .mdb file with access, go into queries->create query in design view, type esc, then menu->view->query and finally type
create table X ( a number, check (a > 20) )
but access thinks that I have a "syntax error in field definition". However, I don't think so. Therefore my question: is it possible to create a check constraint with access. If so: how.
Additionally, I'd like to create the constraint with dao/vba, not on the GUI. Is that possible?
And lastly, on a slightly related note: how do you enter sql statements into access. I can't imagine that I have to use the queries->design view->query->view route in order to do that. I am used to Oracle's SQL*Plus, which I like very much, and I'd hope there's something similar for access as well.
Thanks for any input Rene
You can’t use standard ANSI in the query builder UNLESS you set the database as sql ansi compatible. If you do change this setting, then you CAN can use the sql in the query builder as you have. I would not recommend changing this setting for existing databases however.
If you do, you could type in:
In you don’t need to save the sql in the query builder, and just want to type in the sql, then simply whack ctrl-g to get the access command line prompt, and you can then type in:
The above would be typed on one line. So, you can use the command line prompt if you want..
Here are some notes.
You can create a Pass-Through query for Oracle (Select menu "Query" > "SQL Specific" > "Pass-Through")
Since Access 2003, you can select SQL Server Compatible Syntax (ANSI 92) (http://office.microsoft.com/en-us/access/HA010345621033.aspx)
A validation rule with VBA / DAO
Constraints with ADO / VBA. See [Intermediate Microsoft Jet SQL for Access 2000](http://msdn.microsoft.com/en-us/library/aa140015(office.10).aspx)
There is Validation rule on a column. You can use VB for Access. no SQL*Plus here... You can always use SQL Express as a data source - with all the benefits of real sql server and use access only as a front.
To do this in Access, you need to first open the interface into ANSI-92 Query Mode. I've tested your SQL DDL code: it works fine and creates a column of type
FLOAT
(Double).Is is not possible to do this using DAO but you can use ADO. Long story short:
CHECK
constraints were introduced into the engine in the Jet 4.0 era when the Access Team were favouring ADO. With effect from Access2007, the Access Team are back to favouring DAO but have yet to plug the Jet 4.0 'holes' in DAO. So for the majority of Jet 4.0 -only functionality (compressible data types, fixed-length text data types, fast foreign keys, etc) you need to use ADO.