Search code examples
vbams-accessvalidationrule

Validation Rule in VBA to determine the length of text


I have a form for recording the phone numbers which have two kinds:

  • Internal has 4 characters
  • External has 4 or 7 or 8 characters

I want to make the ValidationRule change according to the Combobox that determines the type of phone numbers.

I tried to use ValidationRule in the form properties but it doesn't work with IIf formula or dependening on the value of another textbox or combobox.

So I made this piece of code, but it doesn't work:

If me.combo.value = "internal" then 
    Me.field.validationrule = "Len([field]) = 4"
ElseIf Me.combo.value = "external" Then 
    Me.field.validationrule = "Len([field]) = 4 or Len([field]) = 7 or Len([field]) = 8"
End If

Thanks in advance.


Solution

  • You don't have to change the validation rule. Try something like this instead:

    ([combo]="internal" And Len([field])=4) Or ([combo]="external" And (Len([field])=4 Or Len([field])=7 Len([field])=8))