Skip to content Skip to sidebar Skip to footer

Using Calculatefield To Update Field With Date

I'm working with Arcpy in ArcGIS and trying to write code that will update a field with the last day of the previous month (ie. if I run the code today, 2017-09-01, it will update

Solution 1:

You are getting the error "Cannot use parenthesis while calling the sub". This is because of the following rules of a function call in VBScript:

3 Methods of calling a function in VBScript:

  • fn a,b - when using this way, you CANNOT enclose the parameter-list in the parenthesis
  • Call fn(a,b) - when you write the call keyword explicitly, you must enclose the parameter-list in parenthesis
  • c=fn(a,b) - when you assign the value returned by the function to a variable, you must enclose the parameter-list in the Parenthesis.

To understand better about these rules, check this answer.

What happened in your case:

In the VBScript code that you posted, you used the 3rd method and you followed the rule by enclosing the parameter-list inside parenthesis. Hence, it worked fine.

In the Python code, you are simply using DateSerial(Year(Date ()), Month(Date ()), 0) which is the 1st method. According to the rule of 1st method, you must not enclose the parameter-list in the parenthesis. Since you did enclose the param-list inside parenthesis, you violated that rule and got that error.

Probable Answer :

Either use the call method 1 correctly by removing the parenthesis as below:

codeblock = '''def getdate():
    DateSerial Year(Date ()), Month(Date ()), 0'''

OR explicitly write the call keyword before actually calling the function:

codeblock = '''def getdate():
    Call DateSerial(Year(Date ()), Month(Date ()), 0)'''

OR try storing the resulting date in a variable as below:

codeblock = '''def getdate():
    varDate = DateSerial(Year(Date ()), Month(Date ()), 0)'''

Please note that I do not know python. So, I am assuming whatever python code you have written is correct. This error was specific to vbScript, hence wrote this answer.

Post a Comment for "Using Calculatefield To Update Field With Date"