在运行其他代码时打开和更新文本框*上的显示访问表单*

问题描述 投票:0回答:1

有时试图完成最轻微的转变成一个主要的头痛...我试图解决这个问题时,我以某种方式损坏了数据库。现在我真的很想明白这一点。 (我以后会担心损坏的数据库!)

最终目标:

打开.accdb文件时,自动:

  1. 开放形式未绑定)
  2. 运行代码: 从表中获取Last Update Date 在表单上的文本框中显示Last Update Date(必须对用户可见)
  3. 如果表数据已过时,请运行代码: 更新表中的数据 在表单上的文本框中显示Update Progress Message(s)(必须对用户可见) 等结束了: 在表单上的文本框中显示Last Update Date(必须对用户可见)

问题是(must be visible to user)的目标。

我无法在所有代码完成运行之前显示任何内容,无论我如何运行代码(包括:使用File→Options→Display Form将表单设置为自动打开;添加AutoExec宏以调用代码打开表单并运行更新;或者,手动运行它。)


我一直在试验表格的开场活动,看看我是否错过了什么。

根据Order of Events文件:

处理表单上的数据

当您在表单中的记录之间移动并更改数据时,会发生表单和控件事件。例如,首次打开表单时,会发生以下事件序列:

Open(形式)→Load(形式)→Resize(形式)→Activate(形式)→Current(形式)→Enter(对照)→GotFocus(对照)

表格在任何时候都不会明显更新。我尝试添加Me.Repaint,甚至尝试过Echo=TrueDoEvents但显然他们没有改变任何东西。


我的测试代码:

Private Sub Form_Open(Cancel As Integer)
    txtTest = "Form_Open()": MsgBox "1. Form_Open"
End Sub

Private Sub Form_Load()
    txtTest = "Form_Load()": MsgBox "2. Form_Load"
End Sub

Private Sub Form_Current()
    txtTest = "Form_Current()": MsgBox "3. Form_Current"
End Sub

Private Sub txtTest_Enter()
    txtTest = "txtTest_Enter()": MsgBox "4. txtTest_Enter"
End Sub

Private Sub txtTest_GotFocus()
    Echo True : Repaint : DoEvents
    txtTest = "txtTest_GotFocus()": MsgBox "5. txtTest_GotFocus"
End Sub

Private Sub txtTest_Change()                        : MsgBox "This Does't Run!" : End Sub
Private Sub Form_AfterUpdate()                      : MsgBox "This Does't Run!" : End Sub
Private Sub txtTest_Change()                        : MsgBox "This Does't Run!" : End Sub
Private Sub txtTest_BeforeUpdate(Cancel As Integer) : MsgBox "This Does't Run!" : End Sub
Private Sub txtTest_AfterUpdate()                   : MsgBox "This Does't Run!" : End Sub

打开表格的结果:

(animated screenshot)

(请注意,表单的ActivateAfterUpdate事件以及控件的ChangeBeforeEventAfterEvent事件根本不会发生。)

从理论上讲,表单应该在MsgBox后面可见,但既不运行实际的更新代码,也不运行“暂停”(使用DoEvents循环)也不会显示表单。

我可以发誓我过去没有遇到过这个问题,但无论我做什么,表格只会在所有代码运行完毕后显示。

vba forms ms-access events access-vba
1个回答
3
投票

您可以使用Form_Timer事件来延迟执行,直到表单完全加载完毕。

Private Sub Form_Load()
    Me.TimerInterval = 1 'Trigger 1 millisecond, but asynchronously from other tasks
                         'Triggers after AutoExec macro has finished
End Sub

Private Sub Form_Timer()
   Me.TimerInterval = 0 'Trigger only once
   'Your other tasks here
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.