MSAccess-插入后重新查询子窗体? [英] MSAccess - Requery a Subform After Insert?

查看:80
本文介绍了MSAccess-插入后重新查询子窗体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我有一个子表单(数据表),我在更新事件后使用数据表复选框进行更新:

I have a subform (datasheet) I update using a datasheet checkbox afterupdate event:

  1. 我克隆单击的记录&通过插入查询插入到引用的表中
  2. 我修改了原始记录,以通过更新查询将其与插入的记录区分开来

为避免RecordLock投诉,我在上述每个选项之后插入了SendKeys "+{Enter}", True以保存更新-有没有更好的方法?

To avoid RecordLock complaints, I have inserted: SendKeys "+{Enter}", True after each of the above to save the updates - is there a better way to do this??

接下来,我需要重新查询子窗体以显示新插入的记录,并使用书签(?)保留原始记录的光标位置(插入的记录将是相邻的).子表单数据表记录集基于查询.

Next I need to requery the subform to display the newly inserted record AND use a bookmark (?) to retain the cursor position of the original record (inserted record will be adjacent). Subform datasheet recordset is based on a query.

问题:

  1. 在子窗体的afterupdate事件中,使用Forms![ParentForm].[SubForm].Form.Requery不会产生错误,但是会打开代码窗口,并以黄色突出显示行.

  1. From within the subform's afterupdate event, using Forms![ParentForm].[SubForm].Form.Requery does not produce an error, but does open the code window with the line highlighted in Yellow.

下一次尝试,是在子窗体的afterupdate事件中,尝试使用Forms![ParentForm].[SubForm].Form.Requery之前将焦点设置在ParentForm上的控件上,但是如上所述,我收到类似的〜silent错误〜.

Next attempt, from within the subform's afterupdate event, I have attempted to set focus to a control on the ParentForm BEFORE using Forms![ParentForm].[SubForm].Form.Requery, but I get a similar ~silent error~ as noted above.

从同一子表单中查询子表单的正确方法是什么?

What is the proper way to REQUERY a subform from within the same subform?

谢谢!

推荐答案

您对自己而言太难了.这是通过单击按钮复制记录的方法.没有锁,没有查询,并且不会自动更新表单:

You are making it way too hard for yourself. Here is how to copy a record from a button click. No locks, no queries, and auto update of the form:

Private Sub btnCopy_Click()

  Dim rstSource   As DAO.Recordset
  Dim rstInsert   As DAO.Recordset
  Dim fld         As DAO.Field

  If Me.NewRecord = True Then Exit Sub

  Set rstInsert = Me.RecordsetClone
  Set rstSource = rstInsert.Clone
  With rstSource
    If .RecordCount > 0 Then
      ' Go to the current record.
      .Bookmark = Me.Bookmark
      With rstInsert
        .AddNew
          For Each fld In rstSource.Fields
            With fld
              If .Attributes And dbAutoIncrField Then
                ' Skip Autonumber or GUID field.
              ElseIf .Name = "SomeFieldToHandle" Then
                rstInsert.Fields(.Name).Value = SomeSpecialValue
              ElseIf .Name = "SomeOtherFieldToHandle" Then
                rstInsert.Fields(.Name).Value = SomeOtherSpecialValue
              ElseIf .Name = "SomeFieldToSkip" Then
                ' Leave empty or with default value.
              ElseIf .Name = "SomeFieldToBlank" Then
                rstInsert.Fields(.Name).Value = Null
              Else
                ' Copy field content.
                rstInsert.Fields(.Name).Value = .Value
              End If
            End With
          Next
        .Update
        ' Go to the new record and sync form.
        .MoveLast
        Me.Bookmark = .Bookmark
        .Close
      End With
    End If
    .Close
  End With

  Set rstInsert = Nothing
  Set rstSource = Nothing

End Sub

如果该按钮位于父窗体上,则将Me替换为:

If the button is placed on the parent form, replace Me with:

Me!NameOfYourSubformControl.Form

AddNewUpdate之间,您可以修改和插入代码以调整某些字段的值,这些字段不应该仅被复制.

Between AddNew and Update you can modify and insert code to adjust the value of some fields that should not just be copied.

这篇关于MSAccess-插入后重新查询子窗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆