Select to view content in your preferred language

VBA: If Then with OR

440
2
09-07-2013 11:58 AM
CarmenTorres
New Contributor
Hello,

I'm tryng to populate a field with this VBA code but it doesn't work. I really appreciate any help you can provide.

Dim codigo as string

If [CAMPO]="Captura" OR [CAMPO]="Replanteo" OR [CAMPO]="GPS" Then
       codigo = "1"
    ElseIf [CAMPO]="Plano" OR [CAMPO]="Ortofoto" Then
        codigo = "2"
    ElseIf [CAMPO]="Catastro" OR [CAMPO]="Ajuste parcelario" Then
        codigo = "3"
    Else codigo = [CAMPO]

EndIf



__esri_field_calculator_splitter__
codigo
Tags (2)
0 Kudos
2 Replies
XanderBakker
Esri Esteemed Contributor
Hello,

I'm tryng to populate a field with this VBA code but it doesn't work. I really appreciate any help you can provide.

Dim codigo as string

If [CAMPO]="Captura" OR [CAMPO]="Replanteo" OR [CAMPO]="GPS" Then
       codigo = "1"
    ElseIf [CAMPO]="Plano" OR [CAMPO]="Ortofoto" Then
        codigo = "2"
    ElseIf [CAMPO]="Catastro" OR [CAMPO]="Ajuste parcelario" Then
        codigo = "3"
    Else codigo = [CAMPO]

EndIf

__esri_field_calculator_splitter__
codigo


Hi alaska@pozo,

The only thing I see in your code it the "Else codigo = [CAMPO]" being on 1 line, but changing it doesn't solve the problem. What you can do is change your code and use a Select Case. This simplifies the code and seems to work (at least for me in 10.2):

Select Case [CAMPO]
Case "Captura", "Replanteo", "GPS" 
  codigo = "1"
Case "Plano", "Ortofoto"
  codigo = "2"
Case "Catastro","Ajuste parcelario" 
  codigo = "3"
Case Else 
  codigo = [CAMPO]
End Select

__esri_field_calculator_splitter__
codigo


Hope this works for you too.

Kind regards,

Xander

BTW: since this is the Python forum, you could consider using the Python syntax:

def ClfyCampo(campo):
    if campo == "Captura" or campo == "Replanteo" or campo == "GPS":
        return "1"
    elif campo == "Plano" or campo == "Ortofoto":
        return "2"
    elif campo == "Catastro" or campo == "Ajuste parcelario":
        return "3"
    else:
        return campo


__esri_field_calculator_splitter__
ClfyCampo( !CAMPO! )
0 Kudos
RichardFairhurst
MVP Honored Contributor
You made three errors. 

Xander pointed out one, which is that you put the result on the same line as the else clause rather than on a new line.

The second it that at 10.0 you should not use Dim statements.  VBA is gone and in VB script all variables are Variant and can no longer be defined, so don't bother with them or just put Dim codigo and nothing else.

The third is that you closed the If block with EndIf (one word) and not End If (two words).

Assuming there are no null values in your data and your field is long enough to handle the largest value stored in the CAMPO field this code will work (with codigo being put in the field text box as you indicated):

If [CAMPO] = "Captura" OR [CAMPO] = "Replanteo" OR [CAMPO] = "GPS" Then
  codigo = "1"
ElseIf [CAMPO] = "Plano" OR [CAMPO] = "Ortofoto" Then
  codigo = "2"
ElseIf [CAMPO] = "Catastro" OR [CAMPO] = "Ajuste parcelario" Then
  codigo = "3"
Else
  codigo = [CAMPO] 
End If
0 Kudos