VBA Access 2013表格:如何刷新?

时间:2018-11-03 12:38:06

标签: vba forms access-vba access

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

我尝试对GotFocus表单进行ActivateClickRefresh ...事件,但是我猜想表单在以下情况下不会失去焦点或未停用 InputForm 已打开,因此这些事件当然不会发生。我还尝试从 InputForm 中进行Refresh 概述。我也尝试找到ActivateDeactivate函数,但它们无处出现。老实说,我不知道如何更新概述表格中的表格。

编辑:

概述

Private Sub btnOpenInputForm_Click()

DoCmd.OpenForm FormName:="InputForm", OpenArgs:=0 & "," & 0

End Sub

InputForm

Private Sub btnAddRecord_Click()

'Read data from the input fields 
'CurrentDb.Execute "INSERT..."

DoCmd.Close

End Sub

1 个答案:

答案 0 :(得分:1)

如果我理解正确,那么您名为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”)中

  • “确定”按钮的Click过程必须调用Me.Visible = False才能隐藏对话框。
  • “取消”按钮的Click过程必须调用DoCmd.Close acForm, Me.Name以关闭对话框。