Hi, I'm fairly new to Access and I'm trying to figure out how to create a validation rule via SQL. I searched through the forums but didn't find anything. Hope someone can point me in the right direction. For simplicity say I have the following extremely simple CREATE STATEMENT.
CREATE TABLE GAMES (
Name TEXT PRIMARY KEY NOT NULL,
Amount CURRENCY NOT NULL,
);
How could I rewrite the above query to specify that the only valid amounts allowed are say between 1 and 10?
I'm familiar with the Constraint statement and the Check clause but I can't get these to work in Access.
Validation rules are an "Access" thingie, and, as far as I know, they can't be written through DDL.
Now the "trickiness" begins. Access is really the front end, which can use several different databases. The native, or default, database is Jet.
The Jet database supports a lot of features that isn't supported through the Access interface, and check constraints is one of them. You can use check constraints in DDL to achieve what you wish, but the downside is that you cannot maintain, or even see these, in the Access interface. You'll also need to execute the DDL on an ADO/OLEDB connection, say
Code:
dim SqlString as string
SqlString= "CREATE TABLE GAMES ( " & _
"TheName TEXT(25) PRIMARY KEY NOT NULL, " & _
"Amount CURRENCY NOT NULL, " & _
"CONSTRAINT chk_amount_between_1_and_10 " & _
"CHECK (Amount Between 1 And 10))"
Currentproject.Connection.Execute SqlString, , adcmdtext+adexecutenorecords
You can also switch SQL compatibility mode to more ANSI-92 like syntax (Tools | Options - somewhere at the bottom right of one of the tabs, if I remember correct). Then you can use more SQL server like syntax in the query tool (Jet SQL, not the Access "dialect").
If I haven't goofed up too much on the typing, the above should create a table with a check constraint preventing entries that aren't between 1 and 10, but if you enter table design, you will find nothing in the validation rule property. Also, if you try to delete this table through the interface, you'll be informed you dannot do so, because apparently the table is referenced in a check constraint *). If this is interesing, you'll find more info here http://msdn2.microsoft.com/en-us/library/Aa140015(office.10).aspx