Select to view content in your preferred language

Using multivalue parameters in a custom toolbox script

3994
3
Jump to solution
12-14-2015 09:09 AM
BenjaminSimpson
Deactivated User

Hi,

I have a custom toolbox which I added a python script too. The script currently runs when the input parameters are not multivalue, but the script falls over when I set the input parameters to multivalue. Where they seem to be falling over is when I try to use pyodbc to connect to an access database. I am currently using the arcpy.GetParameterAsText function and then using the split function to separate the semicolon delimited string into a list.

This is the error information that I am currently receiving.

Error Info:

<class 'pyodbc.Error'>: ('HY000', "[HY000] [Microsoft][ODBC Microsoft Access Driver] Not a valid file name. (-1044) (SQLDriverConnect); [01000] [Microsoft][ODBC Microsoft Access Driver]General Warning Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x1200 Thread 0x128c DBC 0x24d28a4 Jet'. (1); [01000] [Microsoft][ODBC Microsoft Access Driver]General Warning Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x1200 Thread 0x128c DBC 0x24d28a4 Jet'. (1); [HY000] [Microsoft][ODBC Microsoft Access Driver] Not a valid file name. (-1044)")

A snippet of my code:

import arcpy
import pyodbc
import sys
import os
import pythonaddins

SAGIS_Input = arcpy.GetParameterAsText(0)
SAGIS_model_new = SAGIS_Input.split(";")
SAGIS_Count = len(SAGIS_model_new)
arcpy.AddMessage(SAGIS_Count)
for model in SAGIS_model_new:
     SAGIS_model = str(model)
     arcpy.AddMessage(SAGIS_model)
     # connection to SAGIS model geodatabase
     access_conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};DBQ='+SAGIS_model)
     access_cursor = access_conn.cursor()


     tablelist = arcpy.ListTables()
     tablecount = len(tablelist)
     arcpy.AddMessage(tablecount)
     arcpy.AddMessage("Script run successfully.")
     #print "Script run successful."

     access_cursor.close()
     access_conn.close()

I would very much appreciate any advise on how to get the muiltvalue paramters to work with the pyodbc connection.

Thanks in advance.

Ben.

0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

ArcGIS wraps parameters with spaces in them in single quotes when passing multivalue parameters.

If I run the following script as a toolbox script tool and pass it "a b c" and  "abc" as the first parameter, I get the below output

import arcpy

SAGIS_Input = arcpy.GetParameterAsText(0)
SAGIS_model_new = SAGIS_Input.split(";")
SAGIS_Count = len(SAGIS_model_new)
for model in SAGIS_model_new:
  arcpy.AddMessage(model)

results.png

Notice the single quotes around the values 'a b c'?

You need to strip them off with something like:

for model in SAGIS_model_new:
  model = model.strip("'")

View solution in original post

3 Replies
FreddieGibson
Honored Contributor

Have you tried adding a message to the dialog to verify the name returned for your model? The values should come in as a string delimited by semi-colons. I would assume that most likely the quotes may be coming in if you have spaces in the strings and you need to strip them out.

Luke_Pinner
MVP Regular Contributor

ArcGIS wraps parameters with spaces in them in single quotes when passing multivalue parameters.

If I run the following script as a toolbox script tool and pass it "a b c" and  "abc" as the first parameter, I get the below output

import arcpy

SAGIS_Input = arcpy.GetParameterAsText(0)
SAGIS_model_new = SAGIS_Input.split(";")
SAGIS_Count = len(SAGIS_model_new)
for model in SAGIS_model_new:
  arcpy.AddMessage(model)

results.png

Notice the single quotes around the values 'a b c'?

You need to strip them off with something like:

for model in SAGIS_model_new:
  model = model.strip("'")
BenjaminSimpson
Deactivated User

Thank you both for your helpful advice. Stripping the single quotes worked a treat. Your help is much appreciated.