我有一个与subForm
没有联系的parentForm
。我通过点击然后通过subForm
的On_Click()
事件运行代码从subForm
中选择一条记录:
DoCmd.GoToRecord, acDataForm, "parentForm", acGoTo, me.Form.CurrentRecord
它将parentForm
更新为所需的记录,但也刷新了subForm
,因为焦点偏离了我最初点击的记录。
如何保持对subForm
上所选记录的关注?
我已经在这里写了一个答案(Recalc Parent Form Without Losing Focus From The Current Record Of The SubForm),但我还没有回复。这就是为什么我不将你的问题标记为重复。
假设您在主窗体上的子窗体控件名为ctlSubForm
,这将是您在子窗体OnClick()
事件中调用的代码::
'Store the current sub forms record/bookmark
Dim currentRecord As Variant
currentRecord = Me.Bookmark
'Your current code:
DoCmd.GoToRecord, acDataForm, "parentForm", acGoTo, me.Form.CurrentRecord
'Maybe, if it is not working, try to also use this:
'DoEvents
'Set the sub forms record/bookmark to the stored record/bookmark
Me.Bookmark = currentRecord
'Set the focus to the main forms sub form control
'(this is necessary to really get the focus back to the subform)
Me.Parent.ctlSubForm.SetFocus
最后,我找到了问题的答案。
通过单击parentForm
上与continuous subForm
无关的特定记录,运行下面的代码移动到parentForm
上的某个记录。 Me.CurrentRecord
不是一个可靠的方法来移动parentForm
记录在subForm
被过滤除了ID
以外的其他一些参数,而且linked
也不是parentForm
。因此,我修改了我设置SQL查询的代码,然后将其作为RecordSource
的parentForm
。我把这个新代码放在subForm的On_Click()
事件中。
Dim lngPosition as Long
Dim SQL1 as String
Dim a as Long
a = Me.txt_ID 'It is shared unique ID between mainForm and subForm
lngPosition = Me.CurrentRecord
SQL1 = "SELECT myTbl.a, myTbl.b, mTbl.c FROM myTbl " _
& "WHERE ((myTbl.a) = " & a & ");"
Me.Parent.RecordSource = SQL1
Me.Parent.Requery
Me.Form.Recordset.Move lngPosition - 1
运行时,焦点仍然是我最初点击的subForm
的活动记录,而mainForm
被移动到所需的记录。