在Access Vba

时间:2017-01-10 17:08:10

标签: vba ms-access

我编写了两个函数,它们一直工作到我尝试添加其他参数时。第一个功能有效,没有错误。我写了这个,所以我可以根据实际的控件名称轻松获取控件的值。

    Public Function GetControlValue(name As String, FormName As Form) As Object

        Dim obj As Object

        For Each obj In FormName.Controls
            If obj.name = name Then
                Set GetControlValue = obj
             Exit Function
            End If

        Next

    End Function

name变量是实际表单控件的名称,FormName是控件所在表单的名称。

下一个函数是发生错误的地方,我只是为了节省空间而关闭了函数的剩余部分,让我知道你是否需要其余部分。

Public Function SqFtGalCovAfterUpdate(RowNumber As Integer, frmName as String)

     Dim SqFtGalCov As Object
     Dim TranEff    As Object
     Dim NetSqFtGal As Object

     Set SqFtGalCov = GetControlValue("tb" & RowNumber,frmName)
     Set TranEff = GetControlValue("tb" & RowNumber + 1,frmName)
     Set NetSqFtGal = GetControlValue("tb" & RowNumber + 2,frmName)

如果我尝试通过变量frmName将表单的名称作为GetControlValue函数中的FormName参数传递,那么我会收到语法错误。现在,如果我从SqFtGalCovAfterUpdate函数中删除了frmName参数,并直接输入表单名称而不是变量,它将起作用。

Public Function SqFtGalCovAfterUpdate(RowNumber As Integer)


 Dim SqFtGalCov As Object
 Dim TranEff    As Object
 Dim NetSqFtGal As Object

 Set SqFtGalCov = GetControlValue("tb" & RowNumber,NO VARIABLE JUST TYPE IT IN)
 Set TranEff = GetControlValue("tb" & RowNumber + 1,frmName)
 Set NetSqFtGal = GetControlValue("tb" & RowNumber + 2,frmName)

当我直接输入表单名称时它起作用,所以我假设我没有在SqFtGalCovAfterUpdate函数中正确声明我的frmName参数。

Public Function SqFtGalCovAfterUpdate(RowNumber As Integer, frmName As Form)

 Dim SqFtGalCov As Object
 Dim TranEff    As Object
 Dim NetSqFtGal As Object

 Set SqFtGalCov = GetControlValue("tb" & RowNumber, frmName)
 Set TranEff = GetControlValue("tb" & RowNumber + 1, frmName)
 Set NetSqFtGal = GetControlValue("tb" & RowNumber + 2, frmName)

我从

更改了frmName As

我将对象调暗为控件,将frmName调暗为字符串,但得到相同的结果

Public Function SqFtGalCovAfterUpdate(RowNumber As Integer, frmName As String)

 Dim SqFtGalCov As Control
 Dim TranEff    As Control
 Dim NetSqFtGal As Control

 Set SqFtGalCov = GetControlValue("tb" & RowNumber, Forms(frmName))
 Set TranEff = GetControlValue("tb" & RowNumber + 1, Forms(frmName))
 Set NetSqFtGal = GetControlValue("tb" & RowNumber + 2, Forms(frmName))

Public Function GetControlValue(name As String, FormName As Form) As Control

    Dim obj As Control

2 个答案:

答案 0 :(得分:1)

在GetControlValue中,您将FormName声明为Form,在SqFtGalCovAfterUpdate中,frmName是一个String。

答案 1 :(得分:1)

您仍然传递一个字符串,因此您需要按名称分配表单:

Public Function SqFtGalCovAfterUpdate(RowNumber As Integer, frmName as String)

     Dim SqFtGalCov As Object
     Dim TranEff    As Object
     Dim NetSqFtGal As Object

     Set SqFtGalCov = GetControlValue("tb" & RowNumber, Forms(frmName))
     Set TranEff = GetControlValue("tb" & RowNumber + 1, Forms(frmName))
     Set NetSqFtGal = GetControlValue("tb" & RowNumber + 2, Forms(frmName))

此外,由于对象是控件,因此将它们调暗并返回控件:

Public Function GetControlValue(name As String, FormName As Form) As Control

    Dim obj As Control

    Dim SqFtGalCov As Control
    Dim TranEff    As Control
    Dim NetSqFtGal As Control

修改

然后尝试一直传递表单名称:

Public Function GetControlValue(name As String, FormName As String) As Control

    Dim obj As Control

    For Each obj In Forms(FormName).Controls
        If obj.name = name Then
            Set GetControlValue = obj
            Exit Function
        End If
    Next

End Function