2

I want to do some advanced level scripting with VBA in the field calculator. I have done a simple coding in the code block. Here is the codes.

dim X
  if [Name]="University" then
    X=101101
  elseif [Name]= "College" then
    X=101102
  elseif [Name]= "Medical College" then
    X=101103
  elseif [Name]= "School" then
    X=101104
  else
    X=""
  end if

This is working correctly. But I want to do something more. Those are-

  1. If the Name field contain the word University or College or Medical College or School then X return the expecting value.
  2. Is it possible to create new fields through VBScript? If its possible then the X will split into two from its middle (3-3) and put the value in the newly generated column/fields.

If this possible it will help me a lot.

Thanks in advance

2 Answers 2

3

Yes and no. You can create fields from a standalone VBScript, but not within Field Calculator. See link below for how to write VBScript:

Creating the geoprocessor object

I would recommend learning and using python instead for the task that you are trying to do. You could use a SearchCursor to search for the field values, then add your fields based on the SearchCursor result and then calculate.

2

For your first question, you can use InStr() to find out whether a string exists within another string

dim X   
if InStr([Name], "University") then
   X=101101 
elseif  InStr([Name], "Medical College") then 
  X=101103 
elseif  InStr([Name], "College") then 
  X=101102 
elseif  InStr([Name], "School") then 
  X=101104 
else 
  X=""
end if

Note that you'd have to change your logic slightly, running the If statement on "Medical College" before "College". In your original logic, the Instr function would assign X=101102 to both "College" and "Medical College" since the word "College" is found in each string.

2
  • I am facing some problem. Can you help me a little. I want a case insensitivity. If I wrote "college" instead of college its not returning anything. What should I do? Jan 10, 2013 at 7:18
  • Use the syntax if InStr( LCase( [Name] ),"college" ) then
    – kenbuja
    Jan 10, 2013 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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