Ants
Ants

Reputation: 390

How to run vba macro after an access form has loaded?

When I call a sub in Form_Load, it gives me an error cause by Screen.ActiveForm. This is due to the form not being loaded yet.

What sub/function can I use to run a macro once the form has loaded.

Here is my code for Form_Timer:

Private Sub Form_Timer()
    call Module6.loadRecords
    Me.TimerInterval = 500
End Sub

I was hoping that after 0.5 seconds that my form will be loaded and records will be display in the form controls.

Upvotes: 0

Views: 2487

Answers (1)

Andre
Andre

Reputation: 27634

Instead of depending on Screen.ActiveForm, you should simply pass the form reference to the function.

Private Sub Form_Load()
    Call Module6.loadRecords(Me)
End Sub

and

Public Sub loadRecords(F As Access.Form)

If you really want to use Screen.ActiveForm, it works like this:

Private Sub Form_Load()
    ' 1 ms is enough to de-couple the events
    Me.TimerInterval = 1
End Sub

Private Sub Form_Timer()
    ' Reset timer, always the first thing to do for single Timer events
    Me.TimerInterval = 0

    Call Module6.loadRecords
End Sub

Upvotes: 2

Related Questions