【问题标题】:Access VBA refer to a textbox on Active FormAccess VBA 引用 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 来用作表单的名称。

有什么想法吗?谢谢。

【问题讨论】:

    标签: vba ms-access


    【解决方案1】:

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

    Screen.ActiveForm![TimerActivatedLabel].Visible = True
    

    对我来说效果很好。

    还是谢谢!

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    相关资源
    最近更新 更多