Run VBA After Form Loaded

Caleeco

Well-known Member
Joined
Jan 9, 2016
Messages
980
Office Version
  1. 2010
Platform
  1. Windows
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
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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.
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,213,483
Messages
6,113,919
Members
448,533
Latest member
thietbibeboiwasaco

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
Back
Top