简体   繁体   English

如何让表单实例在创建时打开特定记录?

[英]How can I make a form instance open a specific record on creation?

I'm trying to optimize the performance of my access frontend.我正在尝试优化我的访问前端的性能。 One of the problems I'm stumbling on is how to set a multi-window form to open immediately to a specific record, because at the moment it queries everything twice.我遇到的问题之一是如何设置多窗口表单以立即打开特定记录,因为目前它会查询所有内容两次。

Essentially, I allow my users to open multiple instances of a form.本质上,我允许我的用户打开一个表单的多个实例。 That way, a user can compare multiple records by placing the windows of those forms side by side.这样,用户可以通过并排放置这些表单的窗口来比较多条记录。

At the moment, my code looks like this:目前,我的代码如下所示:

Set frm = New Form_Name
frm.RecordSource = "select * from Table where id = " & ID 'ID is a variable passed to the method

I'm pretty sure back then this question was one of the building blocks I relied on.我很确定当时这个问题是我所依赖的基石之一。

The problem is that on the first line, access already entirely opens the form and does everything the form does when opening, such as Form_Open , Form_Current , and loading subforms.问题在于,在第一行,access 已经完全打开了表单,并完成了表单在打开时所做的一切,例如Form_OpenForm_Current和加载子表单。 Then, when I set the recordsource, it does all (or most) of that again, which significantly slows down the process of opening the form.然后,当我设置记录源时,它会再次执行所有(或大部分)操作,这会显着减慢打开表单的过程。

Here are some of the things I've tried:以下是我尝试过的一些事情:

  1. Changing the Form_Current to recognize that it's being "used" twice and only actually run the code once.更改Form_Current以识别它被“使用”两次,并且只实际运行一次代码。 The problem is that the code is already very optimized and doesn't seem to be the bottleneck, so this doesn't seem to do much.问题是代码已经非常优化了,似乎还不是瓶颈,所以这似乎没有多大作用。 Actually opening the form seems to be the bottleneck.实际上打开表格似乎是瓶颈。
  2. Trying to change the properties of the original form so that it opens the specific record I need, but I can't seem to change the properties without it opening the form.试图更改原始表单的属性,以便它打开我需要的特定记录,但如果不打开表单,我似乎无法更改属性。
  3. Looking for a way to supply arguments to the form.寻找一种为表单提供参数的方法。 That doesn't seem to be supported in VBA.这似乎在 VBA 中不受支持。

One other idea that popped into my mind was creating a variable in vba with the ID of the record, setting recordsource in the form properties to fetch that variable, but then, when I would open another form and change that ID, the form我想到的另一个想法是在 vba 中使用记录的 ID 创建一个变量,在表单属性中设置 recordsource 以获取该变量,但是,当我打开另一个表单并更改该 ID 时,表单

I realize that I can do this with DoCmd.OpenForm , but that doesn't give me the option to open multiple instances of the same form.我意识到我可以用DoCmd.OpenForm做到这一点,但这并没有让我选择打开同一表单的多个实例。

How can I achieve this?我怎样才能做到这一点? This would mean a significant performance improvement.这将意味着显着的性能改进。

OK, so I actually wanted to ask this question.好的,所以我实际上想问这个问题。 But just before I hit the submit button, I had an idea...但就在我点击提交按钮之前,我有一个想法......

Here's the idea: you set a global variable in VBA, which you then access in the form filter command.想法是这样的:在 VBA 中设置一个全局变量,然后在表单过滤器命令中访问它。 In my case I just added Public OpeningID As Long at the top of my VBA module.就我而言,我只是在我的 VBA 模块顶部添加了Public OpeningID As Long Then I create a function to get that value:然后我创建一个函数来获取该值:

Public Function getFormID() As Long
    getFormID = OpeningID
End Function

Then, in the form, you can set the filter criteria as ID = getFormID() .然后,在表单中,您可以将过滤条件设置为ID = getFormID() And then when you open the form instance, you do it like this:然后当你打开表单实例时,你会这样做:

OpeningID = ID
Set frm = New Form_Name
OpenindID = 0 'reset this to make sure that if it's being accessed when it shouldn't, it generates a clear error

The only problem is what happens when you refresh the form, as that will call the method again.唯一的问题是刷新表单时会发生什么,因为这将再次调用该方法。 When you refresh the form via VBA, you can set the OpeningID beforehand, and to avoid users refreshing the form, you can add this to your form (don't forget to turn on Key Previews in the form settings):当你通过VBA刷新表单时,你可以预先设置OpeningID ,为了避免用户刷新表单,你可以将这个添加到你的表单中(不要忘记在表单设置中打开Key Previews):

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF5 Then KeyCode = 0
End Sub

Bang.砰。 Forms open almost twice as fast.表单打开的速度几乎是原来的两倍。 Awesome.惊人的。

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

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