在用户窗体中检测控件中的更改

时间:2018-10-26 16:47:08

标签: excel vba excel-vba

下午好,

我为VBA创建了一个函数,当我尝试关闭窗体而不将更改保存到数据库时,如果给定UserForm中的所有控件都有更改,则返回true或false。

如果我在For Each C In FormName.Controls中指定一个现有的表单名称,例如SMSConfig.Controls,则代码可以正常工作。

但是我试图使其更具动态性,因此我可以以任何形式使用此函数,因此我只需要在Close事件中调用函数Call FuncChanges(True, Me.Name)即可使其正常工作,但是我在哪里FormName我不知道如何引用我将其名称存储在Me.Name中的表单。

我收到FormName的无效合格错误

对不起,我的英语不太好...有什么想法吗?

Public ChangesOld() As String
Public ChangesNew() As String
Public CtrlCount As Byte

Function FuncChanges(NewChanges As Boolean, FormName As String) As Boolean

    Dim C As Control
    Dim FLG As Boolean

    CtrlCount = 0

    For Each C In FormName.Controls
        If TypeName(C) = "TextBox" Then CtrlCount = CtrlCount + 1
        If TypeName(C) = "ComboBox" Then CtrlCount = CtrlCount + 1
        If TypeName(C) = "OptionButton" Then CtrlCount = CtrlCount + 1
    Next C

    If NewChanges = True Then

        ReDim ChangesNew(1 To CtrlCount)

        CtrlCount = 0
        FLG = False

        For Each C In FormName.Controls
            If TypeName(C) = "TextBox" Then CtrlCount = CtrlCount + 1: ChangesNew(CtrlCount) = C.Text
            If TypeName(C) = "ComboBox" Then CtrlCount = CtrlCount + 1: ChangesNew(CtrlCount) = C.Text
            If TypeName(C) = "OptionButton" Then CtrlCount = CtrlCount + 1: ChangesNew(CtrlCount) = C.Value
        Next C

        For X = LBound(ChangesOld) To UBound(ChangesOld)
            If ChangesNew(X) <> ChangesOld(X) Then
                FLG = True
                Exit For
            End If
        Next X

        If FLG = True Then FuncChanges = True Else FuncChanges = False

    Else

        ReDim ChangesOld(1 To CtrlCount)

        CtrlCount = 0

        For Each C In FormName.Controls
            If TypeName(C) = "TextBox" Then CtrlCount = CtrlCount + 1: ChangesOld(CtrlCount) = C.Text
            If TypeName(C) = "ComboBox" Then CtrlCount = CtrlCount + 1: ChangesOld(CtrlCount) = C.Text
            If TypeName(C) = "OptionButton" Then CtrlCount = CtrlCount + 1: ChangesOld(CtrlCount) = C.Value
        Next C

    End If

End Function

0 个答案:

没有答案