Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Validation Rule in MS Access

Tags:

ms-access

I have a table in MS-Access - let's call it tComputers. Two of the fields in that table are titled Status - with the options of Active, Storage, Deactivated - and DeactivationDate.

If I wanted to make DeactivationDate mandatory if and only if the value of Status is Deactivated, how can I do that?

like image 555
Zack Avatar asked May 20 '11 14:05

Zack


People also ask

What is validation rule used for in MS Access?

Validation rules prevent bad data being saved in your table. Basically, they look like criteria in a query. You can create a rule for a field (lower pane of table design), or for the table (in the Properties box in table design.) Use the table's rule to compare fields.

How do I change the validation rule in Access?

Select the Fields tab, then locate the Field Validation group. Click the Validation drop-down command, then select Field Validation Rule. The Expression Builder dialog box will appear. Click the text box and type your validation rule.

How do you create a validation rule for gender in Access?

Type the Gender validation rule into the “Validation Rule” box as shown in the record type. This will prevent any value other than “M” or “F” from being entered. You should also add a brief error message in the “Validation Text” box, something like “Value must be 'M' or 'F'”. Likewise with the Class validation rule.


1 Answers

If I wanted to make DeactivationDate mandatory if and only if the value of status is Deactivated how can I do that?

Conversely, if a Deactivated record later changes Status ... say becomes Active ... should the DeactivationDate be discarded?

I think you can accomplish this with a table level Validation Rule. With tComputers in Design View, open the property sheet and use this as the Validation Rule property:

IsNull([DeactivationDate])=IIf([Status]="Deactivated",False,True)

Figure out what message you want the users to see when that validation rule is violated and put it in as the Validation Text property. It's unlikely the default message would mean anything to them.

Although it's possible, I doubt know how useful this is. The users should be editing data via a form, and that gives you the opportunity to enforce your validation requirements before the database engine even attempts to save the data ... use the Form_BeforeUpdate event as @mwolfe02 described.

Edit: Here is an outline for Form_BeforeUpdate. It assumes you have a combo named cboStatus bound to the Status field, and a text box named txtRetireDate bound to the RetireDate field.

Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim strMsg As String
    Select Case Me.cboStatus
    Case "Deactivated"
        If IsNull(Me.txtRetireDate) Then
            strMsg = "RetireDate is required for Status = Deactivated"
            MsgBox strMsg
            Me.txtRetireDate.SetFocus
            Cancel = True
        End If
    Case "Active", "Storage"
        '* what should happen here? *'
    Case Else
        '* what should happen here? *'
    End Select
End Sub
like image 147
HansUp Avatar answered Sep 23 '22 09:09

HansUp