Pausing VBA Code

DragonTech

Registered User.
Local time
Today, 05:38
Joined
Jun 25, 2014
Messages
41
How do I pause VBA code while a form is open? I have a records form, and I have a button on it that opens another form. The other form is a password form, in which I enter a password to continue. However, the button continues its code regardless of whether the password form is open, closed, valid, or invalid. How do I get the code to pause while the password form is open, and not continue until and unless the correct password is entered? Thanks.
 
If you open the Form in Dialog Mode the code will pause until the Form is closed.
 
I suspect you could also do this with an INPUT box, which has options including that you would turn off the echo for entering passwords. If you do an input box in Modal Dialog mode, you won't go anywhere until the box returns a value. Your code will freeze while waiting for input.

Read up on input boxes and see if maybe that is all you need. Look into the options that can be used with input boxes and you might find exactly what you need.
 
Another thing you can do is loop in your code until the form is closed. I've used this a few times:

Code:
DoCmd.OpenForm ("frm_Login")

Do While CurrentProject.AllForms("frm_Login").IsLoaded
    DoEvents
Loop

Hope that's useful!
 
the trouble with an input box is that you can't mask the input as "password", so you end up rolling your own, if you need a secret password. I use cavman's method.
 
Thank you all for your inputs! I agree with Gemma about masking the password. If an input box cannot mask the password, then it is of no use in this instance. A form has more customizability, and I've already designed the password form to my liking, so I think I'll try to stick with what I already have.

Thank you, Gina. That works. But I don't want the code to continue unless the correct password is entered, even if the password form is closed manually. How do I do that?

Pbaldy, I tried your code but VBA didn't seem to recognize GetPassword(). I'm using Access 2007, if that makes a difference.

Cavman, I would use looping, but I've read that loops eat up the processor. Also, how would I implement it such that the code would not continue unless the correct password is entered?
 
Pbaldy, I tried your code but VBA didn't seem to recognize GetPassword(). I'm using Access 2007, if that makes a difference.

GetPassword() is a custom function you can find in the sample db. It all works together.
 
Ah. I see. Well, I still can't get it to work, though it now recognizes the function. I'm afraid I don't understand, even with the notes provided in the database.
 
I know what the problem is now. Forms!frmPassword.txtPassword isn't returning anything. In my database, I have:
Code:
sPassword = Forms!RestrictedOp.Password & vbNullString

I've tried excluding "& vbNullString" but it makes no difference.
 
Another thing you can do is loop in your code until the form is closed. I've used this a few times:

Code:
DoCmd.OpenForm ("frm_Login")

Do While CurrentProject.AllForms("frm_Login").IsLoaded
    DoEvents
Loop

Hope that's useful!

Rather than compute this loop like mad. Have the opened form call a function that continues the code during its onClose event...
 
I know what the problem is now. Forms!frmPassword.txtPassword isn't returning anything. In my database, I have:
Code:
sPassword = Forms!RestrictedOp.Password & vbNullString

I've tried excluding "& vbNullString" but it makes no difference.

Did you catch the fact that the button on the password form doesn't close the form, it just hides it?

Me.Visible = False
 
Dan, I am not using the loop method due to its limited and and intensive nature.

Paul, yes I caught that. I have it set so that On Enter calls Me.Visible = False. I have tried Debug.Print, and sPassword appears to always be empty, no matter what I put into the Password text box. I believe that the password form is not closing prematurely because I think it would be throwing an error.
 
On Enter of the textbox? That wouldn't work; that's when it gets focus. Try After Update.
 
On Enter is when you press the enter key. On Got Focus is when it gets focus. I used to have it as After Update, but I found that On Enter works better.
 
Then I don't know why the password form hides only when I press enter. It does not hide when I focus the text box. Anyway, that part is working for me. What I really need to know now is why GetPassword and sPassword only return a zero-length string.
 
Gosh, dang it! I found a segment in my code that reset the text box to zero. Whelp, that's solved.
 
Paul, I see that you're right now. I've always assumed that On Enter meant when the enter key was pressed, and it's always worked for me. I don't really understand why it has though.
 
Glad you got it sorted. Code in the enter event of the next control in the tab order could work (it would seem to fire when you hit enter in this control), but for this type of thing I typically use the after update event.
 

Users who are viewing this thread

Back
Top Bottom