MSAccess - 插入后重新查询子表单? [英] MSAccess - Requery a Subform After Insert?
问题描述
背景:
我有一个子表单(数据表),我在更新事件后使用数据表复选框进行更新:
I have a subform (datasheet) I update using a datasheet checkbox afterupdate event:
- 我克隆了点击的记录 &通过插入查询插入到引用的表中
- 我修改原始记录以通过更新查询与插入的记录区分开来
为了避免 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.
问题:
在子表单的 afterupdate 事件中,使用
Forms![ParentForm].[SubForm].Form.Requery
不会产生错误,但会打开代码窗口并突出显示该行黄色.
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
在AddNew
和Update
之间可以修改和插入代码来调整一些不应该直接复制的字段的值.
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屋!