Access VBA 引用 Active Form 上的文本框

Posted

技术标签:

【中文标题】Access VBA 引用 Active Form 上的文本框【英文标题】:Access VBA refer to a textbox on Active Form 【发布时间】:2015-04-14 16:37:44 【问题描述】:

我知道如何通过选择表单来引用表单上的项目,即Forms!frmMyForm![Control].Value。但是我有代码可以找到打开的表单,然后从该变量执行代码,但它不起作用。我想我只需要使用 ActiveForm 来引用控件,而不是明确的表单名称,但是我尝试完成的事情没有奏效。

这是我的代码。

Private Sub OrderButton_Click()
'
DoCmd.SetWarnings False
'
CurRecord = Forms!frm_EC_All![L#].Value
GetID = Forms!frm_MainMenu!AssocIDBox
'
Me.LBox.Value = CurRecord
'
If CurrentProject.AllForms("frm_EC_All").IsLoaded = True Then
    RightForm = "frm_EC_All"
ElseIf CurrentProject.AllForms("frm_EC_EC#").IsLoaded = True Then
    RightForm = "frm_EC_EC#"
ElseIf CurrentProject.AllForms("frm_EC_Holds").IsLoaded = True Then
    RightForm = "frm_EC_Holds"
ElseIf CurrentProject.AllForms("frm_EC_L1").IsLoaded = True Then
    RightForm = "frm_EC_L1"
ElseIf CurrentProject.AllForms("frm_EC_L1_L2").IsLoaded = True Then
    RightForm = "frm_EC_L1_L2"
ElseIf CurrentProject.AllForms("frm_EC_L2").IsLoaded = True Then
    RightForm = "frm_EC_L2"
ElseIf CurrentProject.AllForms("frm_EC_L34").IsLoaded = True Then
    RightForm = "frm_EC_L34"
End If
'
Me.Refresh
'
DoCmd.RunSQL "UPDATE tbl_CQueue SET Order1 = -1, Order1Date = Now WHERE [L#] = " & CurRecord
DoCmd.Close acForm, "frm_Requests", acSaveYes
DoCmd.Close acForm, RightForm, acSaveYes
'
DoCmd.OpenForm RightForm, acNormal, , , , acWindowNormal
'
Dim CurrentForm As Form: Set CurrentForm = Screen.ActiveForm
MsgBox "Current form is " & CurrentForm.Name
'
CurrentForm.TimerActivatedLabel.Visible = True
'
GetID = Forms!frm_MainMenu!AssocIDBox
CurRecord = Forms!CurrentForm![L#].Value
'
DoCmd.RunSQL "UPDATE tbl_Data SET tbl_Data.[AssocID] = " & GetID & " , tbl_Data.[tsStartAll] = Now WHERE tbl_Data.[AssocID] Is Null AND tbl_Data.[L#] = " & CurRecord
'
Forms!CurrentForm!IDFieldBox.Value = GetID
'
DoCmd.SetWarnings True
'
End Sub

我在CurrentForm.TimerActivatedLabel.Visible = True 行收到错误消息。错误说"Database can't find the form 'CurrentForm' referred to in a macro expression or Visual Basic code."

所以它会看到在后台打开的表单并将其存储为“RightForm”,然后关闭该表单,再次打开它,然后尝试使其工作,我将活动表单设置为CurrentForm , 然后它应该设置一些文本框并运行 SQL 语句等。但是它需要知道表单的名称并且它没有正确地抓取 CurrentForm 来用作表单的名称。

有什么想法吗?谢谢。

【问题讨论】:

【参考方案1】:

哎呀,我发帖后马上就找到答案了!

Screen.ActiveForm![TimerActivatedLabel].Visible = True

对我来说效果很好。

还是谢谢!

【讨论】:

你需要学习when to use a bang and when to use a dot。这种知识会使这个问题过时。 :o)

以上是关于Access VBA 引用 Active Form 上的文本框的主要内容,如果未能解决你的问题,请参考以下文章

Access VBA添加引用正在重复

Access VBA 添加引用重复

如何在 Access VBA 中从另一个窗体调用一个窗体的 Form_Load

Access 2010 VBA:为啥这个表单打开和关闭序列不起作用?

如何在 Access VBA 中声明对 ActiveX“ListView”控件的引用?

Form.Recordset 属性(访问)