VBA code to close an inputBox when system is inactive for some time.

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
In a post solved for me here by @Jaafar Tribak I used the computer’s system inactivity to display an inputBox where a user must enter certain credentials to get access to the userform again.

It’s working very great. But there has been new developments and I need help to take care of that.

As pointed out by @Jaafar Tribak the code works for modeless forms - which I have no problem with.

But I do have problems with inputBoxes. That is when an inputBox is active, that prevents @Jaafar Tribak code from running no matter how long the waiting period is.

Then it occurred to me that if I could find a way to close objects or processes such as those for message boxes, inputBoxes and the others, then I would be able to get the Sub running smoothly for me.

And the tricky part is that even if I find an easy way to close message boxes and inputBoxes, that would still present a bigger problem because I would also end up closing the input box I am using to restrict access to the userform.

So I am thinking of a way to id that inputBox so that when it comes to closing them, I will exclude that one.

But I have not been able to come up with anything yet.

For the message boxes, I have scripts from multiple sources that do that.

Could someone please help me out here?

Thanks in advance.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Answering because I'd sure like to know it if someone posts how to run any code at all when a modal dialog is open I'd like to read about it.
AFAIK, what you're asking is not possible if the dialog (message box or input box) is modal. I didn't read the linked code so hope you don't mind the suggestion, but why not hide the form before raising the dialog? Then when the dialog is dealt with you can unhide (or not) the form based on user response.
 
Upvote 1
As Micron pointed out, you can't run code asynchronously when a modal dialog is on display. Actually, to be more precise, you can but, you will need a callback procdure like using a windows timer.

What do you want to happen when the InputBox becomes active ?
 
Upvote 1
@Jaafar Tribak
I use the InputBox to make decisions. Say how to generate a report and so on.

My concern is that when InputBox is active, your code as mentioned before cannot run since the InputBox is a modal one.

So when the input box is active and there is no system activity for some time, say one minute, then I want to close or quit the InputBox.

By so doing, I will have the opportunity to run the code from the link above.

This is part of the code you provided in the above link:

Code:
Private Sub Wb_OnIdleTimeReached(ByVal IdleMinutesElapsed As Long)
    MsgBox IdleMinutesElapsed & " Minute(s) have elapsed w/o user activity.", vbSystemModal, "Idle Time reached !!"
    'Do some other thing(s) here.
End Sub

But instead of the message box in the code sample above, I have replaced that with an InputBox. So I was thinking, if there is a way to close or cancel the other InputBoxes, then that particular InputBox I replaced with the Message box should be exempted from being closed.
 
Upvote 0
if there is a way to close or cancel the other InputBoxes, then that particular InputBox I replaced with the Message box should be exempted from being closed
This is not very clear.

You say you have other inputboxes. What are they for ?

Also, why are you replacing the MsgBox with another InputBox ?
 
Upvote 0
In order to differenciate between the inputboxes that you want to be closed and the inputbox that you want to keep active , you will need to somehow flag it.

One idea is perhaps to marque the inputbox you want to keep active with some unusual character in its caption. That way, the code would read each Inputbox title and exclude the one whose title contains the unusual character.

EDIT:
Something such as :
InputBox Prompt:="Hello", Title:="Active InputBox *"

Notice the star * character at the end of the titlebar.
 
Upvote 0
Yes
I am replacing the msgBox with an InputBox.

I am using it to restrict access to the form.
So until the user enters the right input, access to the form is not allowed.

And it shows up anytime the system stays inactive for a given period of time - say 1 minute.

So this inputbox is not to be closed with code because it must and can stay active as long as possible until the user correctly enters the required input.

Code:
Private Sub Wb_OnIdleTimeReached(ByVal IdleMinutesElapsed As Long)
    MsgBox IdleMinutesElapsed & " Minute(s) have elapsed w/o user activity.", vbSystemModal, "Idle Time reached !!"
    'Do some other thing(s) here.
End Sub

When I say other inpuboxes, I mean any other inputbox that is not called within this code:

Code:
Private Sub Wb_OnIdleTimeReached(ByVal IdleMinutesElapsed As Long)
    MsgBox IdleMinutesElapsed & " Minute(s) have elapsed w/o user activity.", vbSystemModal, "Idle Time reached !!"
    'Do some other thing(s) here.
End Sub

If any other inputbox is active for the given time period, then I want to close it then bring up the one I replaced with the msgbox.

I hope this helps
 
Upvote 0
Again, I doubt you can. Not even with sendkeys. Modal boxes are waiting for user clicks. Now if you used a userform instead and made it modal - maybe.
 
Upvote 0
Now if you used a userform instead and made it modal - maybe.
I forgot that I tried that yesterday. I could not interact with the immediate window to close it, nor any code window to try to run any code to close it. I'm fairly certain that I've seen timer events not run on time when a modal object was open. Why can't you just hide the form instead of raising an input box? Or is the user opening input box and leaving it open?
 
Upvote 0
@Micron
Yes the user may open the input box and leave it open.

That’s what I want to avoid.

Concerning using userform to replace the input box, I think that would make the coding a complex one since each form might have different layout and or design
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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