View Full Version : Set validation rules in SQL?


kcarpy
02-08-2008, 08:51 AM
Hello to all. First time poster.

I have a question about creating tables in Access with an SQL query (or VBA, I guess, if that's not possible, though my example below is SQL). Specifically, I'd like to know how one adds validation rules programmatically. For some reason, I can't seem to find this information.

For instance, for simplicity's sake, let's say I'd like to create a table (table1) with a single variable (varx), a long integer, and I'd like the validation rule to be "<5 or >10".

I know how to do everything except add the validation rule in. So what alterations would I make to this to add it?

CREATE TABLE table1 (varx LONG);

Thanks in advance for any help.

KeithG
02-08-2008, 10:19 AM
Look up the tableDef object in VBA help.

DJkarl
02-08-2008, 12:14 PM
You could also use a seperate alter table statement to add a CHECK constraint.

ALTER TABLE table1 ADD CONSTRAINT validRuleName CHECK (varx <5 OR varx >10)

But this is not quite the same as an Access validation rule but if you wanted to stick with an all SQL solution this would work.

kcarpy
02-08-2008, 01:30 PM
Thanks.

I'll check out the tableDef object.

I don't mind using VBA. I'm basically building tables with runSQL statements (I'm a bit of a novice, so this is the best way I know), so I thought it might be simpler to just add it if there were an SQL way of doing it. If it's easier don in VBA, I don't mind learning that. I just don't even know where to look (except, not, for tableDef).