PDA

View Full Version : Solved: forms - passing valued returned to code



philfer
03-16-2008, 11:58 AM
Hi,

I am using a userform to get a password from the user.

I have declared the return as a public variable but it wont pass it from the form to my code module.

I have no idea why this is.

Do I have to put all the code into the form module?

Can't I have my code is a standard module and then activate the userform, get the password from the user and then continue with the code i.e. test if its right and ask them if they want to retry and reload the form etc.

The reason I am using a form instead of an inputbox is so I can use the PasswordChar

Bob Phillips
03-16-2008, 12:15 PM
Yes, but show the code.

Norie
03-16-2008, 12:27 PM
What's the problem with having the code in the userform module?

philfer
03-16-2008, 12:32 PM
its not really a problem.

I just like to have all my code in a standard module for neatness and making it easy for someone else to follow.

Is that the only solution?

Norie
03-16-2008, 12:36 PM
How is code in a standard module easier to follow?:confused:

Also, what about security?

You say you are dealing with passwords, it would be easier for someone to access the code in a standard module, though obviously they can still easily access userform code - but that might need a little more savvy.

PS Excel is not a secure environment.

mikerickson
03-16-2008, 03:07 PM
If the variable is to be used outside of the Userform the declaration statement needs to be in a normal module
Public myVariable as Variant

philfer
04-07-2008, 09:44 AM
Still wont work

In the User form code module I have

Sub btnOK_Click

If Me.txtPword.Value = "" Then
Msgbox "You havent entered a password"
Exit Sub
Else
pword = Me.txtPword.Value
End If

End Sub



Then in my main code I have

at the top

Public pword as String


and in the code


frmPsword.Show

If pword <> "hello" Then
Unload frmPsword
Msgbox "The Password is incorrect"
Exit Sub
Else
Unload frmPsword
Msgbox "The password is correct"
End If


The problem is when I click the OK button on the userform it doesnt do anything

rory
04-07-2008, 09:55 AM
Your main code stops until your userform is either hidden or unloaded. You could put either:
Me.Hide or
Unload Me behind the OK button.

philfer
04-07-2008, 10:04 AM
Thanks

I tried that but now it keeps telling me the password is incorrect even when it is correct.

I have attached a rough and ready example of what I have been doing below.

Note :- it wants me to declare the variable pword in the user form module even though it was already declared in the sheet1 module as public

rory
04-07-2008, 10:17 AM
Your code is not in a normal module, it's in the worksheet code module. In the attached I have moved the declaration of the variable to a normal module and removed it from the userform.

artds
04-07-2008, 10:50 PM
Did you have any other "Pword" declaration anywhere else in the WorkBook?
So, if i'm not mistaken, theres a total of 2 "pword" declaration, am i not?

However this by itself may not be the main problem. Pword in the user form will continue to store the password, until you close the form or until you 'unload' it. Thats when the 'pword' variable be release, or remove from memory.

If I may suggest, Do Not declare pword twice. Only once in the Main code. Not in the user form, and declare it in Option Explicit



Option Explicit
Public pword as String



In the userform codes, you should



sheet1.pword = Me.txtPword.Value

artds
04-07-2008, 10:52 PM
Hope this helps....
Btw, after I post this, only then did I see your book1.xls

so i tried it there, and it works


regards
art.ds