Form Validation Rules

GraemeG

Registered User.
Local time
Today, 21:41
Joined
Jan 22, 2011
Messages
212
Hello,

I was wondering if it is possible to setup validation rules on a form to say if you answer one questions you must answer another?

For example.

I have a combo box for kitchen type so the surveyor selects type, or NONE if there isn't one. If there is one they answer some related questions i.e Install Year and Renew Year.

So is there something that can be done to say if they enter none the renew year and install year dont need to be answered but if the answer anything else then they must answer the other questions?

Thanks
 
Yes, you can do that and it would go in the Form's Before Update event where you can issue a

Cancel = True

if any of the validations fail.
 
Yes, you can do that and it would go in the Form's Before Update event where you can issue a

Cancel = True

if any of the validations fail.

I havent got any validations set up at the minute, I am not sure what is required.
 
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

 If Me.cboKitchenType <> "None" And Nz(Me.InstallYear, "") = "" Then
  MsgBox "Install Year Must be Entered!"
  Cancel = True
  InstallYear.SetFocus
  Exit Sub
 End If

If Me.cboKitchenType <> "None" And Nz(Me.RenewYear, "") = "" Then
  MsgBox "Renew Year Must be Entered!"
  Cancel = True
  RenewYear.SetFocus
  Exit Sub
 End If

End Sub

Linq ;0)>
 
For this:
I have a combo box for kitchen type so the surveyor selects type, or NONE if there isn't one. If there is one they answer some related questions i.e Install Year and Renew Year.

Something like this:

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Me.KitchenType <> "None" And Len(Me.InstallYear & vbNullString) = 0 And _
Len(Me.RenewYear & vbNullString) = 0 Then
       Cancel = True
       MsgBox "You need to fill in ....." 
   End If
 
'And then another one here etc.
End Sub

But if you have a lot of them then I would set a flag if any fail validation and build your message as you go along for each missing one and then show it at the end.
 
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
 
 If Me.cboKitchenType <> "None" And Nz(Me.InstallYear, "") = "" Then
  MsgBox "Install Year Must be Entered!"
  Cancel = True
  InstallYear.SetFocus
  Exit Sub
 End If
 
If Me.cboKitchenType <> "None" And Nz(Me.RenewYear, "") = "" Then
  MsgBox "Renew Year Must be Entered!"
  Cancel = True
  RenewYear.SetFocus
  Exit Sub
 End If
 
End Sub

Linq ;0)>
Linq - You forgot to account for potential empty strings :D
 
Thanks Boblarson and missinglinq

I am getting some errors with my code.
My field names are:
Kitchen (15)
Kitchen (Renew Year)
Kitchen (Install Year)

I tried to change the names where you have put them but no luck
 

Users who are viewing this thread

Back
Top Bottom