I have a userform that calls a function with username/password prompt.
The user usually has to run it more times and each time is prompted for the login credentials so I want the userform to remember the user/password once it has been given until the workbook has been closed.
It is working fine until the user presses the 'process' button or closes the userform from the 'close' button, but when he/she closes it with the 'red x', the user/pass disappears on the next run.
I know that the 'red x' can be disabled, but I am curious if there is another way to make the userform remember even when the 'red x' is pressed?
Here is my code so far:
Private Sub cbCancel_Click()
maplogin.Hide
End Sub
Sub cbProcess_Click()
Dim drivetomap As String
Static user As String
Static pass As String
pcname = ActiveCell.Value
user = Me.txtUser.Value
pass = Me.txtPass.Value
...
You can redirect the X to run the cancel button code instead:
There area few points here. Let's deal with your specific question first: you can use the
QueryClose
event to handle aUserform
being closed, and in your case you'd set theCancel
parameter toTrue
(or basically any integer not 0) so that you couldHide
the form rather thanUnload
it. Like so:But there are other issues.
If you use this
Userform
instance to store the password then you'd need to scope the variables at module level so that you could access the variables outside of yourcbProcess_Click
routine. As below:The
Static
keyword in VBA is only useable at procedure level. Yes, it will retain the values of the variables when the procedure ends, but you still wouldn't be able to access those variables outside of the procedure. Hence the use ofPrivate
for module-level declarations.Life's tricky if you store variable relating to the application in a
Userform
. It basically means you can neverUnload
theUserform
for the duration of the programme. Far better would be to pass those values into some other storage method. Examples might be to pass them into aModule
, to pass them into aClass
, to write them into a hiddenWorksheet
, etc. An example of the module would be like so:Module
Userform