Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

we need just like the following, only with multiple records selected... have tried the "xyz" in (1,2,3,4,5) SQL code but of course VB Script wants something else.

dim f2
if [field1] = "foo" then
  f2 = "bar"
end if

Screen shot of this being used in the field calculator.

enter image description here

share|improve this question
3  
I like the idea of being able to use custom code logic in the field calculator, might be something to add to QGIS. –  Nathan W Mar 9 '11 at 5:42
    
Hi Sirgeo, welcome to GIS.se :) For code formatting while editing select the relevant text and slap [ctrl]-[k]. –  matt wilkie Mar 9 '11 at 18:49
1  
Nathan, I downloaded QGIS yesterday and was very impressed with it. Looks like ESRI has some serious competition there, QGIS is a breath of fresh air in terms of user-friendliness and easy functionality. A custom code logic addition to QGIS's Field Calculator would make it perfect. –  sirgeo Mar 10 '11 at 17:19
    
the vb works well but I was trying to nest the if and it doesn't work. I tried adding if statements after the initial end if. Also I get a warning that field 1 is empty and have to click yes to continue...can this be stopped? –  GeorgeC Oct 26 '11 at 10:14
add comment

2 Answers 2

up vote 6 down vote accepted

If you like SQL, you can apply a Definition Query:

IN('x','y','z')

then process your field calculator results. Only the filtered records will be modified.


If you want Python, it will look similar to the SQL:

if !field1! in ("x","y","z"):
    f2 = "field1 is yep"

[Added as solution] Or as a 1-liner without any preprocessing required:

'yes' if !field1! in ('x','y','z') else ''
share|improve this answer
    
Thanks for the reply Mike, i need to be able to do it all in one step (will have to do the same operation hundreds of times). Can you post the entire Python code for Pre-Logic? Or the VB Script equivalent using SQL's IN operator? Have tried it 20 different ways and it doesn't like the SQL is my guess. screen shot: i.imgur.com/GsBMs.jpg –  sirgeo Mar 9 '11 at 13:50
    
also, that screen shot doesn't have "then" or "end if" in it, but i fixed that i still get the same errors –  sirgeo Mar 9 '11 at 14:45
3  
For Python, you could probably jump the whole pre-logic and use a one-liner: 'yes' if !field1! in ('x','y','z') else 'no' –  Mike T Mar 9 '11 at 23:53
    
Thanks Mike, i will give that a shot first thing tomorrow.... i do see there is no Then in your suggestion, not sure if it will work without a Then but will definitely try it. –  sirgeo Mar 10 '11 at 1:52
1  
SOLUTION --- 'yes' if !sc2! in (14175,14161,14180,13459,13460,14652,14648,14647,14644) else '' simply take everything out of the '' after the else..... I have a new found love for Python, much thanks to all contributions here! –  sirgeo Mar 10 '11 at 14:05
show 5 more comments

It appears that you're trying to conditionally calculate the value of field 2 based on the value of field 1. you could do the entire operation in a python script using a combination of Mike's suggestion and an UpdateCursor something like this:

cursor = gp.UpdateCursor(yourfeatureclass)
row = cursor.Next()

while row:
     for row.GetValue("yoursourcefield") in ["foo", "bar", "snafu"]:
          row.YOURTARGETFIELD = "your target value"
     cursor.UpdateRow(row)
     row = cursor.Next()

otherwise, using just the pre-logic and the calculator, if it's just a boolean check, personally I'd do the inverse of what Mike suggested:

if [field1] not in ["x","y","z"]:
     [field2] = "NO WAY JOSE"
else:
     [field2] = "shoot'em up cowboy"
share|improve this answer
    
thanks Luke, i think i need to stay with VB Script on this one if possible.... i understand very little Python.... where does the "dim f2" and "f2" go into your VBScript example? sorry if this seems so simple, code is not my forte. –  sirgeo Mar 9 '11 at 17:33
    
You can look at it as the nice part about python, you don't have to make that verbose declarative statement. But you can replace [field2] with some variable name and input the new variable name in the bottom portion of the field calculator and get the same affect. –  Luke Mar 9 '11 at 18:01
    
can you do a screen of your field calculator with all this code in it? One of the confusing things is how Field Calculator labels any field double clicked with two "!!" around it.... why do the examples given here use brackets [] around the fields and not "!!" ? –  sirgeo Mar 9 '11 at 19:01
    
@sirgeo, the syntax changes depending on whether the data is shapefile, personal geodatabase, filegdb, etc. (see Arcgis 9.3 SQL Reference. Scroll down to "Comparison operators" to see example of the IN operator. –  matt wilkie Mar 9 '11 at 19:29
    
Matt, doesn't that link only give syntax parameters for SQL? I need the VBScript equivalent for SQL's "STATE_NAME" IN ('14175','14161','14180') operation. This is a shape file i am trying to populate with a new identifier field. ArcGIS 10's field calculator only uses VBScript and Python yes? –  sirgeo Mar 9 '11 at 19:33
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.