+ Reply to Thread
Results 1 to 8 of 8

Thread: Calculate Field - VB - Conditional If,elseif statement... not working?!

  1. #1
    Hailey Monod
    Join Date
    May 2012
    Posts
    24
    Points
    0
    Answers Provided
    3


    0

    Question Calculate Field - VB - Conditional If,elseif statement... not working?!

    Hi,

    Wihtin model builder I have used the field calculator, to try and populate a new empty field (called Water_Classification) based on the contents of another field. However, my VB statement is not working and I am unable to figure out why? If anybody can help, it would be much appreciated

    I would like to put numeric values in a field called [Water_Classification] depending on what the string values are from another field called [CLASSIFICATION_1]

    This is what I currently have:

    If [CLASSIFICATION_1] = "High" then
    x =5
    ElseIf [CLASSIFICATION_1] = "Medium" then
    x =3
    ElseIf [CLASSIFICATION_1] = "Low" then
    x =1
    ElseIf [CLASSIFICATION_1] = "<null>" then
    x =0
    Else
    x = 9999
    EndIf

    At the end (in the box at the bottom), I have: Water_Classification = x

    If anybody has any advice or tips, pelase let me know,
    Thanks!

  2. #2
    Duncan Hornby
    Join Date
    Apr 2010
    Posts
    931
    Points
    310
    Answers Provided
    52


    0

    Lightbulb Re: Calculate Field - VB - Conditional If,elseif statement... not working?!

    I think you need to add at the top of your code a simple dim statement like:

    Dim x

    Also not sure this line is correct:

    ElseIf [CLASSIFICATION_1] = "<null>"

    you may want to try something like (I did not test these):

    ElseIf [CLASSIFICATION_1] = ""

    or

    ElseIf IsNull([CLASSIFICATION_1]) = True

    Duncan
    If you feel my response answered your question please vote it up, I want to be famous!

  3. #3
    Hailey Monod
    Join Date
    May 2012
    Posts
    24
    Points
    0
    Answers Provided
    3


    0

    Default Re: Calculate Field - VB - Conditional If,elseif statement... not working?!

    Hhmm I have added Dim x at the top and changed that one line to be ElseIf IsNull([CLASSIFICATION_1]) = True then.... However its still not working. But I am getting a new error, which says "Error Executing Funtion. Expected 'Then' Failed to execute".
    Thanks for the help! any other ideas?

  4. #4
    Duncan Hornby
    Join Date
    Apr 2010
    Posts
    931
    Points
    310
    Answers Provided
    52


    0

    Question Re: Calculate Field - VB - Conditional If,elseif statement... not working?!

    Can you post up the script, exactly how you entered it?
    If you feel my response answered your question please vote it up, I want to be famous!

  5. #5
    Hailey Monod
    Join Date
    May 2012
    Posts
    24
    Points
    0
    Answers Provided
    3


    0

    Default Re: Calculate Field - VB - Conditional If,elseif statement... not working?!

    Dim x
    If [CLASSIFICATION_1] = "High" then
    x =5
    ElseIf [CLASSIFICATION_1] = "Medium" then
    x =3
    ElseIf [CLASSIFICATION_1] = "Low" then
    x =1
    ElseIf IsNull [CLASSIFICATION_1] = True then
    x=0
    Else
    x = 9999
    EndIf





    Water_Classification= x




    Thanks for your help!



    Click image for larger version

Name:	Capture.JPG
Views:	144
Size:	61.4 KB
ID:	14100

    Click image for larger version

Name:	Capture2.JPG
Views:	124
Size:	58.0 KB
ID:	14101
    Last edited by monodh; 05-04-2012 at 08:58 AM.

  6. #6
    Darren Wiens

    Join Date
    Apr 2011
    Posts
    823
    Points
    271
    Answers Provided
    37


    0
    This post is marked as the answer

    Default Re: Calculate Field - VB - Conditional If,elseif statement... not working?!

    "EndIf" needs a space ---------> "end if"

    Also, IsNull is a function, and requires ()'s around the field name.

  7. #7
    Duncan Hornby
    Join Date
    Apr 2010
    Posts
    931
    Points
    310
    Answers Provided
    52


    0

    Smile Re: Calculate Field - VB - Conditional If,elseif statement... not working?!

    This is going to be painful...

    EndIf

    should be (note space)

    End If

    Duncan
    If you feel my response answered your question please vote it up, I want to be famous!

  8. #8
    Hailey Monod
    Join Date
    May 2012
    Posts
    24
    Points
    0
    Answers Provided
    3


    0

    Default Re: Calculate Field - VB - Conditional If,elseif statement... not working?!

    You guys are BRILLIANT !!
    Both the space was required and the ()'s after IsNull.

    Thank you so much!

    Hailey

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts