Validation Rule in VBA to determine the length of text

438 Views Asked by At

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.

2

There are 2 best solutions below

0
Wolfgang Kais On BEST ANSWER

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))
0
Sarooj On

The code for which I ask

If me.combo.value = "enteral"  then Me.field.validationrule ="Is Null OR Like """"" Elseif me.combo.value = "extetnal" then.   Me.field.validationRule="Is Null OR Like """" OR Like """"  OR Like """""
End if.   

The source https://www.officena.net/ib/topic/87200-التحكم-بخاصية-قاعدة-التحقق-من-الصحة-validationrule-لمربع-نص-برمجياً-vba/