简体   繁体   English

连续形式的 MS Access VBA 代码在子表单中不起作用

[英]MS Access VBA code in continuous form not working while a subform

Good afternoon,下午好,

I'm running into an issue with some code.我遇到了一些代码的问题。 Basically I have a subform attached to a question and answer table.基本上我有一个附加到问答表的子表单。

The subform displays the question and the button displays the answer.子表单显示问题,按钮显示答案。

This works perfectly when I open the form directly, but it won't work as a sub.当我直接打开表单时,这很有效,但它不能作为子工作。

Here is the original code:这是原始代码:

Set r = Forms![FAQs_Questions].RecordsetClone 'Clone the recordset
r.Bookmark = Forms![FAQs_Questions].Bookmark 'Navigate to the active record
MyAnswer = r!Answer.Value

But when it's a subform, I get an error that says "...cannot find the referenced form 'FAQs_Questions'."但是当它是一个子表单时,我收到一条错误消息,上面写着“......找不到引用的表单'FAQs_Questions'。”

So I tried a bunch of things were I'd reference the main page first, below are all of my attempts, each has failed.所以我尝试了很多东西,我先参考主页,下面是我所有的尝试,每一个都失败了。

Dim r As DAO.Recordset
Set r = Forms![FAQs]![FAQs_Questions].RecordsetClone 'Clone the recordset
r.Bookmark = Forms![FAQs_Questions].Bookmark 'Navigate to the active record
MyAnswer = r!Answer.Value


Dim r As DAO.Recordset
Set r = FAQs.FAQs_Questions.Form.RecordsetClone 'Clone the recordset
r.Bookmark = FAQs.FAQs_Questions.Form.Bookmark 'Navigate to the active record
MyAnswer = r!Answer.Value


Dim r As DAO.Recordset
Set r = FAQs_Questions.Form.RecordsetClone 'Clone the recordset
r.Bookmark = FAQs_Questions.Form.Bookmark 'Navigate to the active record
MyAnswer = r!Answer.Value

Dim r As DAO.Recordset
Set r = Forms!FAQs_Questions.Form.RecordsetClone 'Clone the recordset
r.Bookmark = Forms!FAQs_Questions.Form.Bookmark 'Navigate to the active record
MyAnswer = r!Answer.Value


Dim r As DAO.Recordset
Set r = Forms!FAQs_Questions.Form.FAQs_Questions.RecordsetClone 'Clone the recordset
r.Bookmark = Forms!FAQs_Questions.Form.FAQs_Questions.Bookmark 'Navigate to the active record
MyAnswer = r!Answer.Value

I'm at a loss.我不知所措。 Can anyone point me in the right direction?谁能指出我正确的方向?

Thank you!谢谢!

Since your VBA is code behind the subform button, you can simplify this thing by referencing RecordsetClone and Bookmark via Me (the current form; the one which contains the code).由于您的 VBA 是子表单按钮后面的代码,因此您可以通过Me (当前表单;包含代码的表单)引用RecordsetCloneBookmark来简化此操作。

Dim r As DAO.Recordset
Set r = Me.RecordsetClone 'Clone the recordset
r.Bookmark = Me.Bookmark 'Navigate to the active record
MyAnswer = r!Answer.Value

That approach should work regardless of whether the Me form is operating as a subform or if it was opened directly as a top-level form.无论Me表单是作为子表单运行还是直接作为顶级表单打开,这种方法都应该有效。

However, if you don't absolutely need to go the RecordsetClone and Bookmark route, just retrieve Answer.Value directly from the current row of the form's recordset:但是,如果您不是绝对需要 go RecordsetCloneBookmark路由,只需直接从表单记录集的当前行检索Answer.Value

MyAnswer = Me.Recordset!Answer.Value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM