0

Using Access 2010 and python 2.7.8

Have a command button on Access 2010 form. I am trying to pull the value from the Field1 text box and pass it to a python script. I am struggling with passing the variable. Commented out stuff is other things I tried.

Value in Field1 Text box is:

C:\\tests\\Project

VBA behind command button:

Private Sub Command0_Click()
    arg1 = """" & Field1 & """"
    'arg1 = Field1
  Debug.Print arg1
    'Call Shell("C:\\Python27\\ArcGIS10.3\\python.exe " & "C:\\tests\\Test.py " & "C:\\tests\\Project", vbNormalFocus)
    Call Shell("C:\Python27\ArcGIS10.3\python.exe " & "C:\tests\Test.py " & arg1, vbNormalFocus)
End Sub

Python code is:

import os.path

#fp = "C:\\tests\\Projects"
fp = argv[1]
os.makedirs(fp)

2 Answers 2

1

Try the following in Python

# should be sys.argv[1]
sys.argv[1]

You may want to utilise os.path.join to be certain where your makedirs are going

import os
import sys

sys.argv[1]
fp = r'c:\test'
os.makedirs(os.path.join(fp,sys.argv[1]))

THE VBA in Excel - works for me...

Private Sub test()
    arg1 = Range("A1").Value
    Debug.Print arg1
    Call Shell("""C:\Python27\python.exe"" " & """c:\test\test.py"" " & """arg1""", vbNormalFocus)
End Sub 
Sign up to request clarification or add additional context in comments.

3 Comments

No this did not work. The command button is executing the python script but the argument is still not getting passed and or recognized.
The command fp = r'c:\test' did not seem to do anything since the argv[1] passes the entire pathname. I changed it to fp = r'c:\monkey' and the program still ran and no directory named monkey was created. What is intent of this command? Otherwise your suggestions did work. I am not sure why it didn't this past weekend. Maybe I missed something in my code.
r'c:\test' the r indicates that it is a raw literal string, '\' and such is treated as backslash. Otherwise \t \n \r maybe read as tab, new line or return carriage... But if you are passing the string it doesn't matter really...
0

Thanks go to Superfly. Need to import sys into python script and make fp=sys.argv[1].

Access VBA

Private Sub Command0_Click()
    argv1 = Field1.Value
    Debug.Print argv1
    Call Shell("C:\Python27\ArcGIS10.3\python.exe " & "C:\tests\Test.py " & argv1, vbNormalFocus)
End Sub

Python

import os.path
import sys

fp = sys.argv[1]
os.makedirs(fp)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.