VBA Access 2013表单:如何刷新



我有一个表单Overview,其中有一个从数据库获取数据的表。我想在该数据库中添加一行,并更新Overview中的表。我在Overview中创建了一个按钮,打开另一个窗体InputForm(弹出在Overview前面(。在VBA中,我编写了一些代码,使用查询将数据添加到数据库中,然后关闭InputForm。当InputForm关闭时,Overview将再次可见。现在,我希望看到Overview中的表用新数据更新。问题是,只有当我点击Overview中的另一个按钮时,或者当我关闭并重新打开表单时,它才会更新。

我试过GotFocusActivateClick。。。事件Refresh表单,但我猜当InputForm打开时,表单不会失去焦点或未被停用,因此这些事件当然不会发生。我还尝试从InputFormRefresh概述。我还试图找到ActivateDeactivate函数,但它们没有出现。老实说,我不知道如何更新概览表格中的表格。

编辑:

概述

Private Sub btnOpenInputForm_Click()
DoCmd.OpenForm FormName:="InputForm", OpenArgs:=0 & "," & 0
End Sub

输入表单

Private Sub btnAddRecord_Click()
'Read data from the input fields 
'CurrentDb.Execute "INSERT..."
DoCmd.Close
End Sub

如果我理解正确,那么名为Overview的表单将绑定到同一个表,该表将是由该表单上的按钮添加的记录。

所以添加新记录的代码在该按钮的Click事件中运行,对吧?

因此,添加记录后必须调用Me.Requery(在这种情况下,Me关键字是对表单的引用(。

编辑:

关于您的新信息,我添加了这一点,展示了如何将InputForm作为模式对话框调用,并在用户没有取消的情况下读取值

这将是您的呼叫过程:

Private Sub btnOpenInputForm_Click()
Const INPUT_FORM_NAME As String = "InputForm"
'Call the form as a modal dialog
DoCmd.OpenForm FormName:=INPUT_FORM_NAME, OpenArgs:=0 & "," & 0, WindowMode:=acDialog
'If the user cancelled the dialog, it is not loaded any more.
'So exit the sub (or maybe do some other stuff)
If Not CurrentProject.AllForms(INPUT_FORM_NAME).IsLoaded Then
Debug.Print "The user cancelled the dialog"
Exit Sub
End If
'You can now check a value in the dialog
If IsNull(Forms(INPUT_FORM_NAME).MyTextBox.Value) Then
Debug.Print "Null in the control"
Exit Sub
End If
'Or read a value into a variable
Dim myVariable As String
myVariable = Forms(INPUT_FORM_NAME).MyTextBox.Value
'Close the dialog form now
DoCmd.Close A_FORM, INPUT_FORM_NAME
'Build and execute your sql here, like you already did in the dialog form before
'CurrentDb.Execute "INSERT..."
Debug.Print "User entered: ", myVariable
'And finally requery the form
Me.Requery
End Sub

在对话框形式("InputForm"(

  • OK按钮的Click过程必须调用Me.Visible = False才能隐藏对话框
  • 取消按钮的CCD_ 14过程必须调用CCD_