如何保持对子窗体上选定记录的关注?

问题描述 投票:0回答:2

我有一个与subForm没有联系的parentForm。我通过点击然后通过subFormOn_Click()事件运行代码从subForm中选择一条记录:

 DoCmd.GoToRecord, acDataForm, "parentForm", acGoTo, me.Form.CurrentRecord

它将parentForm更新为所需的记录,但也刷新了subForm,因为焦点偏离了我最初点击的记录。

如何保持对subForm上所选记录的关注?

ms-access access-vba
2个回答
0
投票

我已经在这里写了一个答案(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

0
投票

最后,我找到了问题的答案。

通过单击parentForm上与continuous subForm无关的特定记录,运行下面的代码移动到parentForm上的某个记录。 Me.CurrentRecord不是一个可靠的方法来移动parentForm记录在subForm被过滤除了ID以外的其他一些参数,而且linked也不是parentForm。因此,我修改了我设置SQL查询的代码,然后将其作为RecordSourceparentForm。我把这个新代码放在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被移动到所需的记录。

© www.soinside.com 2019 - 2024. All rights reserved.