Share via

VBA Code correction for Active Form

Anonymous
2016-12-27T18:25:06+00:00

Below is the code I want to run only if a particular form "SalesForm" is active.

It works fine, If I leave this form open and work on another forms (It does not switch back to this Form, which means code does not work if it deactive)

But once if I open a report preview screen, it switch back to this form

As per code, the below should only run if the form is active

Why it is getting switch back to this form from report preview

Below is the code I am using on "SalesForm"

Private Sub Form_Timer()

If Screen.ActiveForm.Name = Me.Name Then

DoCmd.Maximize

Me!lblClock.Caption = Format(Now, "dddd, mmm d, yyyy") & Chr(13) & Chr(10) & Format(Now, "hh:mm:ss AMPM")

End If

End Sub

Timer interval is set to 1000

What changes I should do to the code so that it should not switch from report preview to "SalesForm"

Please advice

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
2016-12-29T02:53:36+00:00

It seems like you found a working solution. I see nothing wrong with this code (other than my previous usability concerns), so I would use this and see how your users fare. The file type (mdb/accdb) has no bearing on it.

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
2016-12-27T21:32:55+00:00

When I try this, I get:


Microsoft Visual Basic for Applications


Run-time error '2475':

You entered an expression that requires a form to be the active window.

Maybe your form is a popup or otherwise has properties that prevent this error. Or you did not share the error handling part of your procedure with us.

There may be some code in the report that switches focus (maybe when the report criteria form is closed). Hard to know without inspecting the application itself.

But let's step back and see what this code is really doing:

Maximize form: this is one of the most horrible things a developer can do to a user. I am perfectly capable of deciding if I want a form to be maximized or not.

Set the current time: this is already available in the Windows task bar. I could do without it.

So: comment out this Timer event altogether. Problem goes away and happy users  :-)

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2016-12-28T16:55:27+00:00

    Sir,

    Further to my earlier reply to your advice, just want to mentioned that I changed the below code and it worked

    If Application.CurrentObjectName = "SalesForm" And Application.CurrentObjectType = acForm Then

    DoCmd.Maximize

    Beep

    Me!lblClock.Caption = Format(Now, "dddd, mmm d, yyyy") & Chr(13) & Chr(10) & Format(Now, "hh:mm:ss AMPM")

    End If

    It does switch back from report automatically and it works only if the form "SalesForm" is active.

    But worried, if it is ok, as it has to be used by several users.

    I was just testing and it worked.

    Please advice, any disadvantage of its use (to be used for mdb and also for accde for access 2010)

    Regards

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2016-12-28T16:10:28+00:00

    Sir,

    Thanks for your reply and advise.

    "SalesForm" is a switchboard Form (has company logo and data time display for just a better looks) which opens first and close at last as I put code not to quit the database from any other form, only from the switchboard form exit button.

    To avoid minimize the "salesform", I made the MinMax Button to None and run this code after every second. So that this form should remain always in maximize.

    Error is not been produced as this Form is not closing nor it is minimised till the database is running. The error will happen if we minimize this form ( I also tested and found the same).

    I could not understand the code mistake which I have written should only run, if "SalesForm" is the current active form, it works as per the requirement if only forms are active, but once I open any report in preview it switch back to "Salesform".

    Is there any other way out method, where code should only run if the current active form is "Salesform"

    Please advice.

    Was this answer helpful?

    0 comments No comments