我可以在Access VBA中将字符串变量用作命令的一部分吗?

时间:2017-11-01 13:34:29

标签: string variables access-vba subroutine

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

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

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

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,测试表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

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

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

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

2 个答案:

答案 0 :(得分:0)

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

curl_getinfo()

答案 1 :(得分:0)

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

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

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作为字符串的一部分来定义我想与之交互的表单控件。