是否可以单击报告记录以使用 VBA 在 Access 中打开相关表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11304831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to click on a report record to open relevant form in Access using VBA
提问by andrewb
I have a report with details of jobs/tasks, and also a form which contributes the majority of the data towards that report. Given that a report is a nice way of looking at the larger picture of the data, and a form is the best way of editing data, I would like to be able to click on a row, and have it open up the relevant record in the form view.
我有一份包含工作/任务详细信息的报告,还有一个表格,该表格为该报告提供了大部分数据。鉴于报表是查看数据大图的一种很好的方式,而表单是编辑数据的最佳方式,我希望能够单击一行,并让它在表单视图。
Does anyone know how to do this through VBA? In my mind it should be possible, though my knowledge of objects in Access is limited.
有谁知道如何通过 VBA 做到这一点?在我看来,这应该是可能的,尽管我对 Access 中的对象的了解有限。
Update
更新
I've implemented the following code for my report:
我已经为我的报告实现了以下代码:
Private Sub Edit_Click()
Dim EntityName As String
Dim DocName As String
DocName = "Entity: Overview"
strWhere = "[Entity Name]='" & Entity & "'"
DoCmd.OpenForm DocName, acNormal, , EntityName
End Sub
It successfully opens the correct form, and it also grabs the correct entity name, however it isn't filtering properly. I can't see what the issue is with the code.
它成功打开了正确的表单,并且还获取了正确的实体名称,但是它没有正确过滤。我看不出代码有什么问题。
采纳答案by HansUp
In your Edit_Click()
procedure, you have EntityName
as the WhereConditionparameter to OpenForm
.
在您的Edit_Click()
过程中,您EntityName
将WhereCondition参数设为OpenForm
.
DoCmd.OpenForm DocName, acNormal, , EntityName
However, you haven't assigned anything to EntityName
, so it's an empty string. I think you should use strWhere
as the WhereCondition.
但是,您尚未为 分配任何内容EntityName
,因此它是一个空字符串。我认为您应该将其strWhere
用作WhereCondition。
Private Sub Edit_Click()
Dim strWhere As String
Dim DocName As String
DocName = "Entity: Overview"
strWhere = "[Entity Name]='" & Me.Entity & "'"
DoCmd.OpenForm DocName, acNormal, , strWhere
End Sub
I assumed Entity
is the name of a control, and that's where you get a value to build strWhere
. If there is no control in the report by the name of Entity
, then the code won't work even if there is an Entity
in the original query.
我假设Entity
是控件的名称,这就是您获得 build 值的地方strWhere
。如果报表中没有名称为 的控件Entity
,那么即使Entity
原始查询中有 ,代码也不会工作。
回答by McGarnagle
You should be able to add an On Clickevent in the Detailsection of the report, then add something like this in the VBA handler:
您应该能够在报告的“详细信息”部分添加一个On Click事件,然后在 VBA 处理程序中添加如下内容:
DoCmd.OpenForm "MyForm", acNormal, "", "ID=" & ID.Text
(where MyFormis the target form, and IDis a hidden or visible control in your report that identifies the current record).
(其中MyForm是目标表单,ID是报表中标识当前记录的隐藏或可见控件)。
回答by Fionnuala
You say report, but you should not be using a report but a continuous form or datasheet. You can then add events to any of the controls on any line, and add a double-clickevent, if you do not want to drive your users insane. You could also add a command button if that would be clearer to your users, or format the "link" textbox to underline. The record opened by the action shown by @dbaseman will open which even record has the focus (current record). And that will lead you to discover what you can and can't do with a continuous form :D
你说报告,但你不应该使用报告,而是使用连续的表格或数据表。然后,如果您不想让用户发疯,您可以将事件添加到任何行上的任何控件,并添加双击事件。如果您的用户更清楚,您还可以添加一个命令按钮,或者将“链接”文本框格式化为下划线。@dbaseman 显示的操作打开的记录将打开哪个记录具有焦点(当前记录)。这将引导您发现使用连续形式可以做什么和不能做什么:D