0

我正在尝试从访问中的多个表单控件值构建 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() 方法。

知道为什么这会失败吗?

4

1 回答 1

1

我在引用标签,所以正确的语法是

valuesSQL = valuesSQL & ",('" & Me.Controls(prov & region & "Lbl").Caption & "','" & Me.Controls(prov & region & "Lbl").Caption & "')"
于 2013-07-01T11:02:51.733 回答