将参数从Access窗体上的命令按钮传递给python脚本

时间:2015-06-12 23:42:32

标签: python-2.7 access-vba

使用Access 2010和python 2.7.8

在Access 2010表单上有一个命令按钮。我试图从Field1文本框中提取值并将其传递给python脚本。我正在努力传递变量。注释掉的东西是我尝试的其他东西。

Field1文本框中的值为:

C:\\tests\\Project

VBA后面的命令按钮:

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代码是:

import os.path

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

2 个答案:

答案 0 :(得分:1)

在Python中尝试以下内容

[
  {
    "SampleID": "150000001",
    "SampleName": "Json1",
    "Status": 0,
    "EditType": 0,
    "Tests": [
      {
        "Results": [
          {
            "Value": "444",
            "RunNumber": 1,
            "InSpec": true,
            "ParameterName": "NA",
            "Unit": "ppm",
            "TypeOfParameter": 2,
            "LowerLimit": null,
            "UpperLimit": null,
            "Selected": true,
            "Formula": null
          },
          {
            "Value": "555",
            "RunNumber": 1,
            "InSpec": true,
            "ParameterName": "K",
            "Unit": "ppm",
            "TypeOfParameter": 2,
            "LowerLimit": null,
            "UpperLimit": null,
            "Selected": true,
            "Formula": null
          }
        ],
        "Status": 1,
        "Lab": "AA",
        "TestName": "W:NA,K",
        "Parameters": [
          {
            "ParameterName": "NA",
            "Unit": "ppm",
            "TypeOfParameter": 2,
            "LowerLimit": null,
            "UpperLimit": null,
            "Selected": true,
            "Formula": null
          },
          {
            "ParameterName": "K",
            "Unit": "ppm",
            "TypeOfParameter": 2,
            "LowerLimit": null,
            "UpperLimit": null,
            "Selected": true,
            "Formula": null
          }
        ],
        "TypeOfTest": 0
      }
    ]
  }
]

您可能希望利用os.path.join来确定您的makedirs的去向

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

Excel中的VBA - 适合我...

import os
import sys

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

答案 1 :(得分:0)

谢谢Superfly。需要将sys导入python脚本并生成fp = sys.argv [1]。

访问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)