River34
Apr 24 2012, 12:27 PM
This is the same Select Case statement I had the type mismatch error on earlier. Now I have discovered that it is not checking for all Cases. There are 6 groups of cases that it needs to check for. Below is example of 2.
Select Case (cStat)
Case "Proposal - Development-In Process", _
"Proposal - Awaiting Formal Approval", _
"Proposal - Formally Approved", _
"Proposal - Not Funded Per Client", _
"Project - Not Started", _
"Project - In Process", _
"Project - Completed", _
"Project - On Hold", _
"Project - Deferred Prior Month"
If ([Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[OptPRD] <> 2 Or [Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PRD_RecdDate] <> Null) And IsNull([Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PropActualStartDate]) Then
MsgBox "You must enter the Proposal Actual Start Date before continuing.", vbOKOnly
jvVal = True
[Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PropActualStartDate].SetFocus
Exit Function
End If
Case "Proposal - Awaiting Formal Approval", _
"Proposal - Formally Approved", _
"Proposal - Not Funded Per Client", _
"Project - Not Started", _
"Project - In Process", _
"Project - Completed", _
"Project - On Hold", _
"Project - Deferred Prior Month"
If ([Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[OptPRD] <> 2 Or [Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PRD_RecdDate] <> Null) And IsNull([Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PropActualCompDate]) Then
MsgBox "You must enter the Proposal Actual Completion Date before continuing.", vbOKOnly
jvVal = True
[Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PropActualCompDate].SetFocus
Exit Function
End If
A lot of the 'Status'' are repeated in these cases because it will need to validate for multiple date fields to have been filled out. I thought of listing each Case separately but that would require I then write nested if statements for each Case (there are 15 Status conditions and 8 date fields).
The problem above is that it will check for the first case and find the PropActualStartDate has been filled out. But instead of moving on to the second case to check if the PropActualCompDate was filled out, it Ends Select. I'm guessing that once it found 'Proposal - Awaiting formal Approval' in the first Case it didn't feel the need to check further. Is that assumption correct? If so, is there a better way to write this validation code? ---OR --- Should I just set each Status as it's own case then nest a bunch of if statements under it? Some of these Cases will require up to 5 date fields to be set so that equates to checking 5 fields for null values. Sometimes I tend to go with the easiest solution without thoroughly thinking it through... Then I run to a forum and whine ...
doctor9
Apr 24 2012, 12:52 PM
River34,
Your assumption is correct. A Select Case statement will exit once it finds the first matching case. If you need to check for xyz AND abc, you probably need consecutive, non-nesting IF statements.
If xyz Then
...
End If
If abc Then
...
End If
Hope this helps,
Dennis
River34
Apr 24 2012, 01:33 PM
Glad the doctor is in the house. Wondering why non nested if statements? Below sample of 1 Status condition checking 4 conditions:
Case "Proposal - Formally Approved"
If ([Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[OptPRD] <> 2 Or [Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PRD_RecdDate] <> Null) And IsNull([Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PropActualStartDate]) Then
MsgBox "You must enter the Proposal Actual Start Date before continuing.", vbOKOnly
jvVal = True
[Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PropActualStartDate].SetFocus
Exit Function
ElseIf ([Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[OptPRD] <> 2 Or [Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PRD_RecdDate] <> Null) And IsNull([Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PropActualCompDate]) Then
MsgBox "You must enter the Proposal Actual Completion Date before continuing.", vbOKOnly
jvVal = True
[Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PropActualCompDate].SetFocus
Exit Function
ElseIf ([Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[OptPRD] <> 2 Or [Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PRD_RecdDate] <> Null) And IsNull([Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PropITDirectorsApprvDecisionDate]) Then
MsgBox "You must enter the Proposal IT Directors Approval DecisionDate before continuing.", vbOKOnly
jvVal = True
[Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PropITDirectorsApprvDecisionDate].SetFocus
Exit Function
ElseIf ([Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[OptPRD] <> 2 Or [Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PRD_RecdDate] <> Null) And IsNull([Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PropMinistryApprvDecsionDate]) Then
MsgBox "You must enter the Proposal Ministry Approval Decsion Date Date before continuing.", vbOKOnly
jvVal = True
[Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PropMinistryApprvDecsionDate].SetFocus
Exit Function
End If
River34
Apr 24 2012, 01:37 PM
You're right on the non-nested IF statements. A Status condition may have 2 of 6 needed dates already supplied. If the IF statements are nested, it will break out after the first condtion is found. My brain is just turning to mush. Glad to have a doctor in the house. Thanks!
fkegley
Apr 25 2012, 10:37 AM
You could nest Select Case...End Select structures:
Select Case whatever
Case value
Select Case whateverelse
Case value ' second select case statement
Case value
...
End Select
Case value ' first Select case statement
...
End Select
ace
Apr 25 2012, 10:54 AM
Do yourself a favor and convert statements like:
If ([Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[OptPRD] <> 2 Or [Forms]![frmProjectIntake]![sfrmProjectIntakeDetail].Form.[PRD_RecdDate] <> Null)
that are used multiple times into a function that returns a true or false. It will make staring at the resulting decision statements
much easier on the eyes and brain.
River34
May 1 2012, 09:01 AM
You're right, Ace. These things always start off as one or two validations, then become 30 or 40. Problem is that the second condition each of these lines points to a different data element. As it is, we wound up eliminating the first conditon (Opt_PRD <>2) anyway. One baby step at a time. Thanks for the second eyes though, always good to know people have your back.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please
click here.