Link to home
Start Free TrialLog in
Avatar of epicazo
epicazoFlag for United States of America

asked on

I get an Error on Set frmCurrentForm = Screen.ActiveForm when I am previewing report

I have a form that runs some queries every two minutes and only when it has focus. However, when I am previewing a report I get an Run-time error #2475: "You entered an expression that requires a form to be the active window;" It doesn't like the "Set frmCurrentForm = Screen.ActiveForm" statement.

Private Sub Form_Timer()
Dim frmCurrentForm As Form
Set frmCurrentForm = Screen.ActiveForm
'run queries only when form is on focus
If frmCurrentForm.Name = "Frm_MyList" Then
   Me.LstReminder.Requery
   Me.LstFollowUp.Requery
   If fFollowUpCases() Then
      DoCmd.OpenForm "Frm_POPFollowUpReminder", acNormal
   End If
End If
End Sub

Could an expert please provide me a workaround or a fix?

Avatar of Leigh Purvis
Leigh Purvis
Flag of United Kingdom of Great Britain and Northern Ireland image

The form has to be the current object - with focus - for that to work.

Either set focus to the form before calling ActiveForm or set the form reference explicitly by name.
ASKER CERTIFIED SOLUTION
Avatar of flavo
flavo
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ajkamp
ajkamp

Try changing it to Me.Setfocus
try:

Private Sub Form_Timer()
Dim frmCurrentForm As Form
if not screen.activeform is nothing then
Set frmCurrentForm = Screen.ActiveForm
'run queries only when form is on focus
If frmCurrentForm.Name = "Frm_MyList" Then
   Me.LstReminder.Requery
   Me.LstFollowUp.Requery
   If fFollowUpCases() Then
      DoCmd.OpenForm "Frm_POPFollowUpReminder", acNormal
   End If
End If
end if
End Sub
My suggestion would look like:

Private Sub Form_Timer()

Me.Setfocus
'run queries only when form is on focus
If frmCurrentForm.Name = "Frm_MyList" Then
   Me.LstReminder.Requery
   Me.LstFollowUp.Requery
   If fFollowUpCases() Then
      DoCmd.OpenForm "Frm_POPFollowUpReminder", acNormal
   End If
End If
End Sub
Avatar of epicazo

ASKER

I tried for RCORRIE suggested but I still get the same error message.

Thanks flavo, an error handler will do the trick, but how can I make it to pop-up an error message when not #2475?


Private Sub Form_Timer()
On Error GoTo Form_Timer_Err
Dim frmCurrentForm As Form
Set frmCurrentForm = Screen.ActiveForm
'run queries only when form is on focus
If frmCurrentForm.Name = "Frm_MyList" Then
   Me.LstReminder.Requery
   Me.LstFollowUp.Requery
   If fFollowUpCases() Then
      DoCmd.OpenForm "Frm_POPFollowUpReminder", acNormal
   End If
End If
Form_Timer_Exit:
    Exit Sub

Form_Timer_Err:
    'MsgBox Err.Description
    'Here I want to popup message other than 2475
    Resume Form_Timer_Exit

End Sub
Form_Timer_Err:
If Err.Number = 2475 Then
  '// Do nothing :-)
Else
    MsgBox Err.Description
End if
    Resume Form_Timer_Exit

Dave :-)

Avatar of epicazo

ASKER

Thank God for people like you.