Using if then statements in field calculator to condense / merge fields

355
4
10-18-2011 07:09 AM
ShaneMay
New Contributor
I'm new to using the field calculator, and also new to writing code. I'm trying to use VB Script in the calculator to essentially merge the information in four separate fields into one large field. I started out just by adding them all together in order separated by spaces. This worked however many of the rows are blank which is creating field values of numerous spaces, so I want to try to incorporate an if then statement for each field that will be merged so that if a field is blank for one record it won't be transferred into the new field. I've found and used coding examples in the archived forums and copy/pasted them with the values I though should work but nothing seems to be working. I keep getting syntax errors. Also is it even possible to have four to five if then statements in one field calculator statement? Please help relieve my frustration.
0 Kudos
4 Replies
DarrenWiens2
MVP Honored Contributor
First, it will help if you paste the code you're trying to use, especially since it's giving you a syntax error. Second, yes, it is possible to use as many if/then statements as you please.
0 Kudos
ShaneMay
New Contributor
Thanks for replying so quickly I'll post one of the scripts that I've tried, and a quick follow up question before hand, when using the Pre-Logic Script Code block, why do you still need to add a statement into the normal block? Shouldn't it just use the code that is written to determine the field value?

My attempted code:

Dim result as String
If [PROJECT_CO] = 0  Then
result = 0  Else
result = [PROJECT_CO]
End if
0 Kudos
DarrenWiens2
MVP Honored Contributor
VBScript doesn't recognize dimming as a type, so change it to: Dim result (or just don't dim it at all). Also, "Else" has to be on a new line (I assume that was a copy/paste thing).
0 Kudos
DaleHoneycutt
Occasional Contributor III
If you are adventurous and want to abandon VB for Python, here's a blog article on how to concatenate fields using Python in the Calculate Value tool. It shows how to use If statements and the like.
0 Kudos