Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing arguments from one form to another 1

Status
Not open for further replies.

DPlank

IS-IT--Management
May 30, 2003
1,903
GB
Hi all.

I am trying to code a user/password routine. So far I have the variables uName and uPassword defined as public strings.

in my workbook_open event, I have :
Code:
loginwindow.show
which does what I'd expect it to. The login window appears and the user name is prepopulated from application.username

once the user has entered a password, they click the Log In button.

I have :
Code:
Public Sub Loginbutton_Click()
uPassword = Password.Value
If (uPassword = "") Then
    passreq = MsgBox("Please enter your password", vbOKOnly)
Else
    uName = UserName.Value
End If
'Call findaccess(uName, uPassword)
LoginWindow.Hide
End Sub
I've commented out the call to the other (public) procedure as it doesn't work anyway. On the HIDE event, the control returns to the workbook_open event and it continues running the code to determine the user's access level. But it doesn't pass back the values for uName and uPassword from the loginwindow userform..?

Anyone know what I'm missing? I tried to create a property to be used publicly, but it was a bit beyond me, and vba's help is less use than my own guesswork.

Thanks
David

[joinedupwriting]
"whaddaya mean, 'invalid tag'?!?"
[/joinedupwriting]

[lightsaber]
 
You didn't include the code where you're setting the uName variable, but I assume it's in the form load and looks similar to the above.

Where did you define these variables? If you put them as Public variables in a public module you should be fine. If they are in the form code it may be a problem.

Public uName as string
Public uPassword as string

Sub FindAccess
...
End Sub

calculus
 
What I do is setup a function that Shows my password form. In the password form is simple one or two text boxes, an okay button and a cancle button.

The okay and cancel buttons only HIDE the form, not Unload it.

Then, back in the function, I check the password text box using something like what is below. You'll have to play with it a little in order to setup "levels" of protection as you seem to want. This only Allows or Does NOT allow.

Code:
Public Function PasswordProtection(Password As String) As Boolean
Dim Response
    PasswordProtection = False

ShowBox:
    frmPassword.Show

    'If user clicks cancel, the forms tag is false
    'and there is no need to continue with the code.
    If frmPassword.Tag = False Then GoTo EndOfFunction

    If Not frmPassword.txtPassword.Value = Password Then
        Response = MsgBox("You have entered an incorrect password.", _
            vbRetryCancel + vbCritical + vbDefaultButton1, "Incorrect Password")
        If Response = vbCancel Then
            GoTo EndOfFunction
        Else
            frmPassword.txtPassword.Value = ""
            GoTo ShowBox
        End If
    End If

    PasswordProtection = True
EndOfFunction:
    Unload frmPassword
End Function

BUT, to make a long story short, you could use...
Code:
frmPassword.txtPassword.Value[\code] to access the data in an object on a form.

P.S. NEVER pass the value of the password to the Form!!  ALWAYS take the Users Password Attempt from the form to compare against the Password Value in code outside of the form.  It's a good practice.

********************
What's the best way to get the answers you need??  See FAQ222-2244 for details!
 
Thanks for your help guys!

I've managed to make it work using ssVBAdev's method.

Have a star for the assist!

David

[joinedupwriting]
"whaddaya mean, 'invalid tag'?!?"
[/joinedupwriting]

[lightsaber]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top