Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"How can I close My Application if Nobody works on it?" 1

Status
Not open for further replies.

Crownknox

Technical User
Apr 27, 2000
25
0
0
US
"How can I close My Application if Nobody works on it?"
I found this link in a previous post.
What I have done is
1.Made Form "IDLE MINUTES" to startup in hidden mode
2.Added the code(Listed below) to OnTimer()event to this form
THE ERROR MESSAGE I GET IS:"Sub or Function not defined"

AM I MISSING SOMETHING? DO I NEED TO ADD ANYTHING?


THE CODE IN THE LINK IS:
'---------------------------------------------------------
Sub Form_Timer()
' IDLEMINUTES determines how much idle time to wait before
' running the IdleTimeDetected subroutine.
Const IDLEMINUTES = 5
Static PrevControlName As String
Static PrevFormName As String
Static ExpiredTime
Dim ActiveFormName As String
Dim ActiveControlName As String
Dim ExpiredMinutes
On Error Resume Next


' Get the active form and control name.
ActiveFormName = Screen.ActiveForm.Name
If Err Then
ActiveFormName = "No Active Form"
Err = 0
End If


ActiveControlName = Screen.ActiveControl.Name
If Err Then
ActiveControlName = "No Active Control"
Err = 0
End If
' Record the current active names and reset ExpiredTime if:
' 1. They have not been recorded yet (code is running
' for the first time).
' 2. The previous names are different than the current ones
' (the user has done something different during the timer
' interval).
If (PrevControlName = "") Or (PrevFormName = "") _
Or (ActiveFormName <> PrevFormName) _
Or (ActiveControlName <> PrevControlName) Then
PrevControlName = ActiveControlName
PrevFormName = ActiveFormName
ExpiredTime = 0


Else
' ...otherwise the user was idle during the time interval, so
' increment the total expired time.
ExpiredTime = ExpiredTime + Me.TimerInterval
End If


' Does the total expired time exceed the IDLEMINUTES?
ExpiredMinutes = (ExpiredTime / 1000) / 60


If ExpiredMinutes >= IDLEMINUTES Then
' ...if so, then reset the expired time to zero...
ExpiredTime = 0
' ...and call the IdleTimeDetected subroutine.
Call fExitApplication
End If
End Sub
'---------------------------------------------------------
 
Where's fExitApplication located? You may need to use the following format rather than Call:
<location>.fExitApplication

<location> = Form_<frmname>
or
<location> = <modulename>

If it's in the current form, you may just be able to say
fExitApplication

I had a problem with Call before, so I never use it.
 
Thanks for the help.
I am a new user. fExitApplication has not been established by me. Do I need to add it to a module? If so, do you have a sample code?

Also, the only thing I have done to accomplish logging off inactive applications was adding the code I first mentioned in a hidden form at startup. Do I need to do anything else?

I appreciate your help?
 
The command to exit is DoCmd.Quit. If you don't need to do anything when you exit, that's all you need.

There are probably several ways of checking for inactivity. Your method looks like it's designed to check whether they've gone to another form or control, which will work. One problem, though, is that your variables are declared in the Timer function. At every time interval, they will be initialized, then dropped. Thus, it will never kick idle users out. Your variables need to be global to the form.
 
Thanks for the reply:
After posting I searched many web sites for help. I found this solution in a Microsoft support community post. I then have added the formname &quot;Closemsg&quot; that has the command Application.Quit. after a msg &quot;....will close in 5 minutes&quot;. The command Application.Quit is on form_Timer for 5 minutes.

This seems to work!
I'm sure there is a better way and probably one that could just have the one form.

Below is the code I used:

Sub Form_Timer()
' IDLEMINUTES determines how much idle time to wait for before
' running the IdleTimeDetected subroutine.
Const Idleminutes = 30

Static PrevControlName As String
Static PrevFormName As String
Static ExpiredTime

Dim ActiveFormName As String
Dim ActiveControlName As String
Dim ExpiredMinutes

On Error Resume Next

' Get the active form and control name.

ActiveFormName = Screen.ActiveForm.Name
If Err Then
ActiveFormName = &quot;No Active Form&quot;
Err = 0
End If

ActiveControlName = Screen.ActiveControl.Name
If Err Then
ActiveControlName = &quot;No Active Control&quot;
Err = 0
End If

' Record the current active names and reset ExpiredTime if:
' 1. They have not been recorded yet (code is running
' for the first time).
' 2. The previous names are different than the current ones
' (the user has done something different during the timer
' interval).
If (PrevControlName = &quot;&quot;) Or (PrevFormName = &quot;&quot;) _
Or (ActiveFormName <> PrevFormName) _
Or (ActiveControlName <> PrevControlName) Then
PrevControlName = ActiveControlName
PrevFormName = ActiveFormName
ExpiredTime = 0
Else
' ...otherwise the user was idle during the time interval, so
' increment the total expired time.
ExpiredTime = ExpiredTime + Me.TimerInterval
End If

' Does the total expired time exceed the IDLEMINUTES?
ExpiredMinutes = (ExpiredTime / 1000) / 60
If ExpiredMinutes >= Idleminutes Then
' ...if so, then reset the expired time to zero...
ExpiredTime = 0
' ...and call the IdleTimeDetected subroutine.
IdleTimeDetected ExpiredMinutes
End If
End Sub
Sub IdleTimeDetected(ExpiredMinutes)
Dim Msg As String


DoCmd.OpenForm &quot;CloseMsg&quot;, , , , , acDialog

'MsgBox &quot;No user activity detected. This program will shut down automatically in 5 minutes.&quot;

'Msg = &quot;No user activity detected in the last &quot; & QuitTime

'Msg = Msg & ExpiredMinutes & &quot; minute(s)!&quot;
'MsgBox Msg, 48




End Sub
 
How might I make the variables global to the form?

I have had some problems noticed since I've been using the inactivity procedure that I previously listed using the timer function.
 
Make them global by declaring them outside all functions/subs. Declare them under Option ... at the beginning of all the code. As long as the form's open, they'll keep their values. By using global variables, you shouldn't need to use another form.
 
Sounds like it would work better that way....but, it's unfamiliar territory for me. It is something I want to do though.

Is there any faq's with a sample of this global code. Maybe a download? Or does anyone want to share theirs?
 
I think you're original code was fine. Your Static variables just need to be moved outside the sub. Just cut, Ctrl+Home, then paste right under the Option Explicit declaration. That way, they're global to the form and will retain their values as long as the form is open.

There probably is a FAQ somewhere, but I don't know right off. If not in this forum, perhaps in Microsoft: Access Other topics.
 
OK Thanks: HERE is what I did. I CUT and pasted the section below under the Option Explicit.
----------
Static PrevControlName As String
Static PrevFormName As String
Static ExpiredTime
----------
I get the ERROR MESSAGE: &quot;Invalid Outside Procedure&quot;
 
Oh. Interesting. You probably need to declare the type of ExpiredTime. I'd say, declare as a Variant or a Long.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top