Application.OnTime

pit84

New Member
Joined
Sep 17, 2017
Messages
16
Hi

Wile reading a book, I came across this function and I try to grasp it, yet there is something I am clearly missing. I tried to create pseudo password login. I crated a userform, added text field and login button. I added code:

Code:
Private Sub confirmPassword_Click()
If passwordBox.Text = "qwerty" Then
    Unload Me
Else
    greetingLabel.Caption = "Password Not Recogized"
    'Application.OnTime Now + TimeValue("00:00:05"), "ExitApp"
    Call ExitApp
    
End If
End Sub

Sub ExitApp()
    Unload PasswordForm
    ActiveWorkbook.Close (False)
End Sub

if I simply call ExitApp, it closes workbook, leaving app open. If I try to use OnTIme (commented out), nothing happens.
If you could head me into direction, what I'm missing here, what should I look for to learn more

Thanks
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I think ExitApp needs to be in a general code module, not the Userform code module.
 
Upvote 0
True, thanks. Oddly, it should give me any message saying it can't be called or something.
 
Upvote 0
I think ExitApp needs to be in a general code module, not the Userform code module.

Alternatively, does it work to declare Public Sub ExitApp() ?

[ERRATA] I guess not. At least, it does not work for worksheet modules.

Oddly, it should give me any message saying it can't be called or something.

That happens at run-time, in my experience.
 
Last edited:
Upvote 0
Hi,
Try this:

In Your Forms Code Page

Code:
Private Sub confirmPassword_Click()
    If passwordBox.Text = "qwerty" Then
        Unload Me
    Else
        greetingLabel.Caption = "Password Not Recogized"
        Application.OnTime Now + TimeValue("00:00:05"), "'ExitApp PasswordForm'"
        
    End If
End Sub

In a STANDARD Module

Code:
Sub ExitApp(ByVal Form As Object)
    Unload Form
    ActiveWorkbook.Close (False)
End Sub


Dave
 
Upvote 0
True, thanks. Oddly, it should give me any message saying it can't be called or something.

You'd think so, but they obviously designed it not to. Perhaps because the code runs as a background task, they decided not to allow it to intrude on the foreground task. I don't know.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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