Select to view content in your preferred language

Model Builder - Caluclate Value

1221
4
12-24-2013 03:57 AM
ChrisMatthews
Deactivated User
Hello All

I'm using the Calculate Value Tool within Model Builder to take a user variable (a date) to create a where clause to apply as the Expression on a Make Query Table. Please see below the values I've used for my Calculate Value Tool.

When I run the model I'm getting the following error:

ERROR 000539: Error running expression: x(str(Start_Date)) <type 'exceptions.NameError'>: name 'Start_Date' is not defined
Failed to execute (CalcQueryWhere).

It looks to me that the Python script is not receiving the user variable as intended. Can any body help me with this? I really hope so, it would be very much appreciated.


Regards

Chris Mathtews



Expression:
x("Start_Date")

Code Block:
def x(Start_Date):
   strDay = Start_Date[:2]
   strMonth = Start_Date[2:-4]
   strFYear = Start_Date[-4:]

   intTYear = int(strFYear) + 2
   strTYear = str(intTYear)

   strFromDate = "'" + strFYear + "-" + strDay + "-" + strMonth + "'"
   strToDate = "'" + strTYear + "-" + strDay + "-" + strMonth + "'"

   strWhere = "qryELM.elmPK = tblELM.elmPK AND dbo.tblELM.LoggedDate >= " + strFromDate + " AND dbo.tblELM.LoggedDate <= DATEADD(year, 1," + strToDate + ") AND dbo.tblELM.EnquiryFK IN (4,22,25,61)"
   return strWhere

Data type:
String
Tags (2)
0 Kudos
4 Replies
DouglasSands
Deactivated User

ERROR 000539: Error running expression: x(str(Start_Date)) <type 'exceptions.NameError'>: name 'Start_Date' is not defined
Failed to execute (CalcQueryWhere).


Try using:

x(str(!Start_Date!))


... instead. See the help information here. Make sure to specify that the calculation is using python, also.
0 Kudos
JoshuaChisholm
Frequent Contributor
Hello Chris,

Firstly, if you haven't already, make sure you are using proper indentation on your code book (although you probably did in your tool).

You also need to call the user variable in the expression. Use the % signs around the variable. In your case I think you'll use %Start_Date%. It's also a good idea to use str(%Start_Date%) to convert whatever the user provides to a string.

I've also attached a screenshot if it helps.

Let me know how it goes!
0 Kudos
ChrisMatthews
Deactivated User
Many thanks Doug

You didn't quite answer the right question, but you gave me enough to resolve the issue.

For anyone who reads this thread here is the link to the correct online help page.

//http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Calculate_Value/0017000000v8000000/

The solution to the problem was to enclose the parameter in %'s, whilst converting the variable to a string.

Expression:
x(str(%Start_Date%))
0 Kudos
ChrisMatthews
Deactivated User

[/HR]
Hello Chris,

Firstly, if you haven't already, make sure you are using proper indentation on your code book (although you probably did in your tool).

You also need to call the user variable in the expression. Use the % signs around the variable. In your case I think you'll use %Start_Date%. It's also a good idea to use str(%Start_Date%) to convert whatever the user provides to a string.

I've also attached a screenshot if it helps.

Let me know how it goes!


Thanks Hua, you must have posted whilst I was posting. Your answer is what I've ended up doing.
0 Kudos