【问题标题】:Can I use a string variable as part of a command in Access VBA?我可以在 Access VBA 中使用字符串变量作为命令的一部分吗?
【发布时间】:2017-11-01 13:34:28
【问题描述】:

我在 Access 中有一个表单,其中包含一些 VBA 自动化,以防止将垃圾数据输入数据库。该自动化的一部分是清除用户取消选中的步骤下游的步骤。它跟踪一个线性过程,所以如果步骤 1、2、3 和 4 完成,并且用户取消选中 2,则代码取消选中 3 和 4。

这些步骤分布在多种可能的测试表格中。我想编写一个程序可以调用自动取消选中的子程序,并根据用户单击的测试表单将变量传递给子程序。

这是我的代码,它不起作用。 (“干”和“湿”是过程中的步骤,按顺序排列。):

Private Sub t2Dry_AfterUpdate()
    Dim TFNum As String
    TFNum = "t2"
    'Autodate and expose wet check fields once t2dry box is checked
    If t2Dry Then
    t2DDate.Enabled = True
    t2DDate.SetFocus
    t2DDate = Date
    t2Wet_Label.Visible = True
    t2Wet.Visible = True
    t2Wdate_Label.Visible = True
    t2Wdate.Visible = True
    End If

    'If dry box unchecked after date entered, undo all subsequent steps
    If Not t2Dry And t2date = "" Then
    Dim t2DUnchk As Byte
    t2DUnchk = msgbox("Unchecking this box will clear all subsequent data. Are you sure?", vbYesNo + vbQuestion, "Undo this step?")
    If t2DUnchk = vbYes Then
    t2DDate.SetFocus
    t2DDate = ""
    t2DDate.Enabled = False
    clearWetData (TFNum)
    Else: t2Dry = True
    t2DDate.SetFocus
    End If
    End If

End Sub

请注意,这仅适用于测试表 2,它在我的数据库中定义,代码为 t2。这就是声明 TFNum 变量的原因。从底部开始的 7 行,您会看到我调用了我编写的子例程,以在用户取消选中干框且湿数据存在后清除湿数据。这是那个子程序:

Private Sub clearWetData(TFNum)

        'Call this subroutine to clear wet data from test forms on uncheck of dry comm step.
        'This subroutine relies on the TFNum argument to decide which test form's wet data to clear.
        (TFNum)Wet = False
        t2Wdate.Visible = True
        t2Wdate.Enabled = True
        t2Wdate.SetFocus
        t2Wdate = ""
        t2Wdate.Enabled = False
        t2Wet_Label.Visible = False
        t2Wet.Visible = False
        t2Wdate_Label.Visible = False
        t2Wdate.Visible = False

End Sub

cmets 之后的第一行是我正在尝试做的一个示例:使用传递的变量 TFNum(它是一个包含文本“t2”的字符串)作为对象名称的一部分。 “t2Wet”是我要清除的复选框对象之一的名称,因此我希望它以这种方式解析。

所有剩余的以 t2 开头的代码已经在运行;子例程在调用时会清除除复选框之外的所有内容。

对于短篇小说,我很抱歉,我想尽可能详细。感谢您的帮助!

【问题讨论】:

    标签: string ms-access variables vba subroutine


    【解决方案1】:

    据我了解,您的 (TFNum)Wet 是表单上的控件。如果是这种情况,您可以只使用控件集合:

    Me.Controls(TFNum & "Wet") = False
    

    【讨论】:

      【解决方案2】:

      好的,我知道这个问题不是长久之计,但办公室的一位朋友为我解决了这个问题。

      子程序上的代码需要如下所示:

      Private Sub clearWetData(TFNum As String)
      
          'Call this subroutine to clear wet data from test forms on uncheck of dry comm step.
          'This subroutine relies on the TFNum argument to decide which test form's wet data to clear.
          Me.Controls(TFNum & "Wet") = False
          Me.Controls(TFNum & "Wdate").Visible = True
          Me.Controls(TFNum & "Wdate").Enabled = True
          Me.Controls(TFNum & "Wdate").SetFocus
          Me.Controls(TFNum & "Wdate") = ""
          Me.Controls(TFNum & "Wdate").Enabled = False
          Me.Controls(TFNum & "Wet_Label").Visible = False
          Me.Controls(TFNum & "Wet").Visible = False
          Me.Controls(TFNum & "Wdate_Label").Visible = False
          Me.Controls(TFNum & "Wdate").Visible = False
      
      End Sub
      

      Me.Controls 是所有表单控件的数组。通过首先调用它而不是直接调用所需的表单控件,我可以使用我的变量 TFNum 作为字符串的一部分来定义我想要与之交互的表单控件。

      【讨论】:

        猜你喜欢
        • 2013-09-24
        • 1970-01-01
        • 1970-01-01
        • 2022-12-02
        • 1970-01-01
        • 1970-01-01
        • 2012-12-01
        • 2017-02-13
        • 1970-01-01
        相关资源
        最近更新 更多