multiple validations on a table

D

drolette

Hi,

How do I set multiple validations on a table? I know how to set one. Do I
need to use VBA code to do this or is there a simple way?

Thanks in advance for your help.
L. Drolette
 
J

John W. Vinson

Hi,

How do I set multiple validations on a table? I know how to set one. Do I
need to use VBA code to do this or is there a simple way?

Thanks in advance for your help.
L. Drolette

A Table Validation rule needs to be a logical expression - which can involve
multiple AND and OR clauses - which returns TRUE if the record is valid, FALSE
otherwise. These statements can get pretty cryptic and hard to read, and you
really only get one validation text choice, so the opportunity to give the
user an informative message is pretty difficult.

You may want to - instead, or in a addition - enforce that all data entry be
done using a Form, and put VBA code in the form's BeforeUpdate event to
validate the data entry. This can be done field by field, give multiple or
variable error messages, set focus to the control for which data is missing or
invalid, etc.; a lot more flexible and user friendly.

John W. Vinson [MVP]
 
D

drolette

Thank you John. Yes, I agree that VBA code is more powerful. Unfortunately
I don't know it! I have [several] questions where the 1st question asks for
a number and the following question asks for another number that must be <=
to the previous number entered. Since you can't use the value of another
control in a validation rule for a control, I'm have trouble visualizing the
easiest way to accomplish this. Is VBA code in the form's BeforeUpdate event
the only way to do this? Can this be done in a macro? And your correct, I
do indeed need different error messages for each error.
 
J

John W. Vinson

Thank you John. Yes, I agree that VBA code is more powerful. Unfortunately
I don't know it! I have [several] questions where the 1st question asks for
a number and the following question asks for another number that must be <=
to the previous number entered. Since you can't use the value of another
control in a validation rule for a control, I'm have trouble visualizing the
easiest way to accomplish this. Is VBA code in the form's BeforeUpdate event
the only way to do this? Can this be done in a macro? And your correct, I
do indeed need different error messages for each error.

If that were the only rule (I gather that it isn't) then a table validation
rule might be

[FirstField] IS NOT NULL AND [SecondField] IS NOT NULL
And [SecondField] <= [FirstField]

with a validation text like

"Both Firstfield and secondfield must be entered, and secondfield must be less
than or equal to firstfield".

As you can see, this can get hairy with multiple rules!

I'm not experienced with macros; VBA for this kind of thing is actually easier
since you can use If... Then blocks of code.

Since I don't know the structure of your table or your rules, I can't be
specific about the code; but it would be something like

Private Sub Form_BeforeUpdate(Cancel as Integer)
If IsNull(Me!txtFirst) Then
MsgBox "First must be filled in!", vbOKOnly
Me!txtFirst.SetFocus
Exit Sub
End If
If IsNull(Me!txtSecond) Then
MsgBox "First must be filled in!", vbOKOnly
Me!txtSecond.SetFocus
Exit Sub
End If
If Me!txtSecond > Me!txtFirst Then
MsgBox "Second must be less than or equal to First", vbOKOnly
Me!txtSecond.SetFocus
Exit Sub
End If
.... <etc etc>
End sub
 
S

Steve Schapel

Drolette,

If you are talking about the value of different *fields*, then the
Validation Rule can easily be set up in the table design, for example:
[YourFirstField]>=[YourSecondField]

However, from your description, it soulds like you are trying to enforce
the value entered compared with the value in the previous *record*... am
I correct? If so, this is another kettle of fish altogether.
 
D

drolette

Hi John. Thanks for the examples and your response. Let me explain a bit
more. I'm developing a database that will be used for data entry of
questionnaires filled out by subjects in a research study. These
questionnaires often have crazy data and I want to avoid having data-entry
staff enter data that makes no sense. Here's an example of a questionnaire:

1. Did you have any outbreaks since your last visit? [hadob]
1a. If yes, how many outbreaks did you have? [numob]
1b. For how many of these outbreaks did you take therapy? [numtreated]
1c. For how many of these outbreaks did you come into the clinic?
[numclinic]
etc.

1a, 1b, and 1c are skipped if the subject had no outbreaks, so it's
perfectly fine for all of them to be null when [hadob]=no. In addition, if
the subject didn't answer any of the questions, data-entry is told to enter a
missing code (999 in this case). So in some cases, 1b will not be less than
1a (when 1b is 999). Then the same check needs to be done for 1a and 1c.
There are a ton of other checks I could do as well, like restrict entry of 1b
and 1c when 1a is 'no'. Am I beyond my capabilities given what little I know
about Access and VBA?

John W. Vinson said:
Thank you John. Yes, I agree that VBA code is more powerful. Unfortunately
I don't know it! I have [several] questions where the 1st question asks for
a number and the following question asks for another number that must be <=
to the previous number entered. Since you can't use the value of another
control in a validation rule for a control, I'm have trouble visualizing the
easiest way to accomplish this. Is VBA code in the form's BeforeUpdate event
the only way to do this? Can this be done in a macro? And your correct, I
do indeed need different error messages for each error.

If that were the only rule (I gather that it isn't) then a table validation
rule might be

[FirstField] IS NOT NULL AND [SecondField] IS NOT NULL
And [SecondField] <= [FirstField]

with a validation text like

"Both Firstfield and secondfield must be entered, and secondfield must be less
than or equal to firstfield".

As you can see, this can get hairy with multiple rules!

I'm not experienced with macros; VBA for this kind of thing is actually easier
since you can use If... Then blocks of code.

Since I don't know the structure of your table or your rules, I can't be
specific about the code; but it would be something like

Private Sub Form_BeforeUpdate(Cancel as Integer)
If IsNull(Me!txtFirst) Then
MsgBox "First must be filled in!", vbOKOnly
Me!txtFirst.SetFocus
Exit Sub
End If
If IsNull(Me!txtSecond) Then
MsgBox "First must be filled in!", vbOKOnly
Me!txtSecond.SetFocus
Exit Sub
End If
If Me!txtSecond > Me!txtFirst Then
MsgBox "Second must be less than or equal to First", vbOKOnly
Me!txtSecond.SetFocus
Exit Sub
End If
.... <etc etc>
End sub
 
D

drolette

Hi Steve. Thanks for responding. Your 1st assumption is correct. My
problem was how to have *multiple checks* with different error messages on a
table validation level because otherwise I'd have to write VBA code to do it
at the field level on the form.

Steve Schapel said:
Drolette,

If you are talking about the value of different *fields*, then the
Validation Rule can easily be set up in the table design, for example:
[YourFirstField]>=[YourSecondField]

However, from your description, it soulds like you are trying to enforce
the value entered compared with the value in the previous *record*... am
I correct? If so, this is another kettle of fish altogether.

--
Steve Schapel, Microsoft Access MVP
Thank you John. Yes, I agree that VBA code is more powerful. Unfortunately
I don't know it! I have [several] questions where the 1st question asks for
a number and the following question asks for another number that must be <=
to the previous number entered. Since you can't use the value of another
control in a validation rule for a control, I'm have trouble visualizing the
easiest way to accomplish this. Is VBA code in the form's BeforeUpdate event
the only way to do this? Can this be done in a macro? And your correct, I
do indeed need different error messages for each error.
 
J

John W. Vinson

Hi John. Thanks for the examples and your response. Let me explain a bit
more. I'm developing a database that will be used for data entry of
questionnaires filled out by subjects in a research study. These
questionnaires often have crazy data and I want to avoid having data-entry
staff enter data that makes no sense. Here's an example of a questionnaire:

1. Did you have any outbreaks since your last visit? [hadob]
1a. If yes, how many outbreaks did you have? [numob]
1b. For how many of these outbreaks did you take therapy? [numtreated]
1c. For how many of these outbreaks did you come into the clinic?
[numclinic]
etc.

Well... it sounds like you're making The Common Questionnaire Mistake, storing
one question per FIELD instead of one question per RECORD (in a related
table).

http://www.rogersaccesslibrary.com/Otherdownload.asp?SampleName='At Your Survey 000'

for a valuable alterntive structure.
1a, 1b, and 1c are skipped if the subject had no outbreaks, so it's
perfectly fine for all of them to be null when [hadob]=no. In addition, if
the subject didn't answer any of the questions, data-entry is told to enter a
missing code (999 in this case). So in some cases, 1b will not be less than
1a (when 1b is 999). Then the same check needs to be done for 1a and 1c.
There are a ton of other checks I could do as well, like restrict entry of 1b
and 1c when 1a is 'no'. Am I beyond my capabilities given what little I know
about Access and VBA?

Well, you'll need to learn some VBA even with the normalized structure. What
you're requiring will be all but impossible with just table design and macros
since there are evidently a vast number of logical decisions to be made.
 
D

drolette

yep! You're right. This is a great opportunity for me to learn more about
better design. Thank you so much for the link. I will check it out.



John W. Vinson said:
Hi John. Thanks for the examples and your response. Let me explain a bit
more. I'm developing a database that will be used for data entry of
questionnaires filled out by subjects in a research study. These
questionnaires often have crazy data and I want to avoid having data-entry
staff enter data that makes no sense. Here's an example of a questionnaire:

1. Did you have any outbreaks since your last visit? [hadob]
1a. If yes, how many outbreaks did you have? [numob]
1b. For how many of these outbreaks did you take therapy? [numtreated]
1c. For how many of these outbreaks did you come into the clinic?
[numclinic]
etc.

Well... it sounds like you're making The Common Questionnaire Mistake, storing
one question per FIELD instead of one question per RECORD (in a related
table).

http://www.rogersaccesslibrary.com/Otherdownload.asp?SampleName='At Your Survey 000'

for a valuable alterntive structure.
1a, 1b, and 1c are skipped if the subject had no outbreaks, so it's
perfectly fine for all of them to be null when [hadob]=no. In addition, if
the subject didn't answer any of the questions, data-entry is told to enter a
missing code (999 in this case). So in some cases, 1b will not be less than
1a (when 1b is 999). Then the same check needs to be done for 1a and 1c.
There are a ton of other checks I could do as well, like restrict entry of 1b
and 1c when 1a is 'no'. Am I beyond my capabilities given what little I know
about Access and VBA?

Well, you'll need to learn some VBA even with the normalized structure. What
you're requiring will be all but impossible with just table design and macros
since there are evidently a vast number of logical decisions to be made.
 
D

drolette

Hey John. I tried the link but get an error message saying that arguments
are of the wrong type, are out of acceptable range, or are in conflick with
one another. I also tried to cut and paste the URL but got the same message.
Any ideas?

John W. Vinson said:
Hi John. Thanks for the examples and your response. Let me explain a bit
more. I'm developing a database that will be used for data entry of
questionnaires filled out by subjects in a research study. These
questionnaires often have crazy data and I want to avoid having data-entry
staff enter data that makes no sense. Here's an example of a questionnaire:

1. Did you have any outbreaks since your last visit? [hadob]
1a. If yes, how many outbreaks did you have? [numob]
1b. For how many of these outbreaks did you take therapy? [numtreated]
1c. For how many of these outbreaks did you come into the clinic?
[numclinic]
etc.

Well... it sounds like you're making The Common Questionnaire Mistake, storing
one question per FIELD instead of one question per RECORD (in a related
table).

http://www.rogersaccesslibrary.com/Otherdownload.asp?SampleName='At Your Survey 000'

for a valuable alterntive structure.
1a, 1b, and 1c are skipped if the subject had no outbreaks, so it's
perfectly fine for all of them to be null when [hadob]=no. In addition, if
the subject didn't answer any of the questions, data-entry is told to enter a
missing code (999 in this case). So in some cases, 1b will not be less than
1a (when 1b is 999). Then the same check needs to be done for 1a and 1c.
There are a ton of other checks I could do as well, like restrict entry of 1b
and 1c when 1a is 'no'. Am I beyond my capabilities given what little I know
about Access and VBA?

Well, you'll need to learn some VBA even with the normalized structure. What
you're requiring will be all but impossible with just table design and macros
since there are evidently a vast number of logical decisions to be made.
 
L

L. Drolette

The link worked but I can't get the database to run on my Access 2007
platform. The error message says that I need to 'enable content' to get
certain macros to run and that the database is not trusted. However when I
close the error message box and follow the instuctions, there is no 'message
bar' for which to 'enable content' and 'trust in office'.
 
J

John W. Vinson

The link worked but I can't get the database to run on my Access 2007
platform. The error message says that I need to 'enable content' to get
certain macros to run and that the database is not trusted. However when I
close the error message box and follow the instuctions, there is no 'message
bar' for which to 'enable content' and 'trust in office'.

sorry... don't yet have 2007 installed. I'd suggest reposting to Duane's
attention and ask what needs to be done to get around 2007's enhanced
"security" (securing people everywhere from getting work done).
 
Top