【发布时间】: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