Run VBA After Form Loaded

Caleeco

Well-known Member
Joined
Jan 9, 2016
Messages
966
Hello,

I have a basic form which I need to open & navigate to a record (based on todays date).

I have set the form "HotDesk Booker" form to open using the File > Options > Current Database > Display Form menu.

Within the Form Code, I have:
VBA Code:
Private Sub Form_Load()
DoEvents
Call LoadRecord(Me)
End Sub

which links to
VBA Code:
Sub LoadRecord(F As Access.Form)
    'DoCmd.FindRecord Date, acEntire, True, acSearchAll, False, acAll, True
End Sub

I'm not so sure the LoadRecord sub will work. However, the LoadRecord vba is running before the Hotdesk Form has even loaded. Which results in an error message saying .FindRecord is unavailable.

How can I force Access to wait for the form to load before selecting the right record upon opening the file?

Many Thanks
Caleeco
 

Some videos you may like

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Micron

Well-known Member
Joined
Jun 3, 2015
Messages
2,019
Office Version
  1. 365
Platform
  1. Windows
Are you sure the error isn't because there are no record for the Date?
That method isn't the preferred one these days, it would seem. It's more reliable to open the form with a filter. You could also switch to the Current event but if you're navigating records on that form, it will fire every time you (or Access) selects a record. You'd need to code around that. If you stick with your present approach, it might help to work with a recordset clone.
 

Caleeco

Well-known Member
Joined
Jan 9, 2016
Messages
966
Are you sure the error isn't because there are no record for the Date?
That method isn't the preferred one these days, it would seem. It's more reliable to open the form with a filter. You could also switch to the Current event but if you're navigating records on that form, it will fire every time you (or Access) selects a record. You'd need to code around that. If you stick with your present approach, it might help to work with a recordset clone.
Hi Micron,

I believe so. If I put a STOP command as the first line of LoadRecord sub-routine, I can see the LoadRecord routine runs without the form visually loaded in the background.

I definitely do have a date record for today, perhaps it's a formatting issue.

I am certainly interesting in adopting best practice. I will google filtering the form! Yes, I will also be navigating record on the form so would only wish the filter to be applied on launch.

Thanks for the pointer, I'll let you know if I get stuck!

Cheers
Caleeco
 

Micron

Well-known Member
Joined
Jun 3, 2015
Messages
2,019
Office Version
  1. 365
Platform
  1. Windows
without the form visually loaded in the background.
A bit of a misnomer there. You can't see the form load, you can only see it when it becomes current and while it may become the active window, you're really not going to see that when you have the code window on top. Might help you to review form events for now and future tasks.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,392
Messages
5,601,400
Members
414,448
Latest member
Jessica 22664

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top