尝试在访问表单上使用Controls()函数时出错

时间:2013-07-01 10:31:36

标签: vba ms-access

我正在尝试从访问中的几个表单控件值构建一个SQL插入语句。

我读过我可以使用Controls()函数动态引用我的控件。

我试过这样做:

   Private Sub InsertCoeff(ByVal prov As String, ByVal region As String)
    Dim strSQL As String
    Dim insertSQL As String
    Dim valuesSQL As String
    insertSQL = "INSERT INTO 'Coefficient de Fermage'"
    valuesSQL = "("
    valuesSQL = valuesSQL & "('1F','2F')"
    valuesSQL = valuesSQL & ",('" & Me.Controls(prov & region & "Lbl") & "','" & Me.Controls(prov & region & "Lbl") & "')"
    valuesSQL = valuesSQL & ",('" & Me.Controls(prov & "Lbl") & "','" & Me.Controls(prov & "Lbl") & "')"
    valuesSQL = valuesSQL & ",('" & Me.Controls(prov & region & "Terres") & "','" & Me.Controls(prov & "Bat") & "')"
    valuesSQL = valuesSQL & ",('" & Me.DateEff & "','" & Me.DateEff & "')"
    valuesSQL = valuesSQL & ")"
    strSQL = insertSQL & " VALUES " & valuesSQL
End Sub

但是我得到运行时错误438(对象不支持此属性或方法),即使我开始键入“我”时vba自动建议Controls()方法。

知道为什么会失败吗?

1 个答案:

答案 0 :(得分:1)

我正在引用标签,因此正确的语法是

valuesSQL = valuesSQL & ",('" & Me.Controls(prov & region & "Lbl").Caption & "','" & Me.Controls(prov & region & "Lbl").Caption & "')"