Help with code...EASY...


Posted by Steve on July 10, 2001 8:34 AM

I have this code that is loaded when form is opend, I need it to not open after an incorrect password is presented. The form is called "update".

Private Sub Form_Load()
Dim MyPassword
MyPassword = InputBox("Please enter password", "Password Prompt", "********")
'hardcode password
If MyPassword = "password" Then
MsgBox "Access Granted", vbInformation, "Access"
'call macro
Exit Sub
Else
MsgBox "Access denied", vbCritical, "Error"
Exit Sub
End If
End Sub

Thanks!

Posted by Dax on July 10, 2001 9:15 AM

Just put the Unload Me in where I've put it above.

Dax.

Posted by Steve on July 10, 2001 9:56 AM

Runtime Error

I got an error message "Runtime Error 429--Active X component cant create object.



Posted by Dax on July 10, 2001 12:53 PM

Re: Runtime Error

Oops, bit hasty there. It's not working because it's trying to Unload the form before it's actually loaded into memory. You could put some code in the form i.e.

Dim MyPassword
Private Sub UserForm_Activate()
If MyPassword <> "password" Then Unload Me
End Sub
Private Sub UserForm_Initialize()
MyPassword = InputBox("Please enter password", "Password Prompt", "********")
End Sub

although this will momentarily show the form. Alternatively, you could include the password routine in the code that actually displays the form.

Sub ShowForm()
Dim MyPassword
MyPassword = InputBox("Please enter password", "Password Prompt", "********")
If MyPassword="password" Then Userform1.Show
End Sub

I hope this helps,
Dax.