简体   繁体   中英

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. 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. 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.

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

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.

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. In my case I just added Public OpeningID As Long at the top of my VBA module. 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() . 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):

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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