I need a code for Userform1. Please help!

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
i have user form name "Userform1"
lebel1 with caption Username
lebel2 with caption password
textbox1 is to enter username
textbox2 is to enter password (i pressed f4, and in properties i have placed * next to PasswordChr) which shows only ***
when i enter the password:

I have a commandButton2 named cdmenter (to click after entering username and password).
I also have commandButton2 name cmdclose (to cancel if i dont want to log on to excel).
I need a code that if i dont enter unsername = "v456i" & password = "jiz21l"
msgbox "wrong info entered" "do you want to try again?" Options yes/NO
if yes selected then go back and give another chance to enter or if no selected then triger cancel and close the worksheet.
If anyone could help then, I will really appriciate it. Thanks.

Thanks again for helping!
;)
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
:rolleyes:

I have found a similiar which is very helpfull but the form does not pop by when i close and reopen the workbook to check.

thank you.
 
Upvote 0
Here's something you can give a try for the cdmenter button.
Code:
Private Sub cdmenter_Click()
If Not Me.TextBox1.Value = "v456i" Or Not Me.TextBox2.Value = "jiz21l" Then
    Reply = MsgBox("Wong info entered." & vbCr & "Do you want to try again?", vbYesNo)
    If Reply = vbYes Then _
        Me.TextBox1 = "": Me.TextBox2 = "": Me.TextBox1.SetFocus: Exit Sub
    Unload Me
    ThisWorkbook.Close False
End If
'''Your code here if the passwords are good.
End Sub

Is that close to what you're looking to do?
 
Upvote 0
Oh yeah I forgot to mention I assumed you meant to have it close out the workbook instead of the worksheet.
And be sure to save the workbook before trying out the 'No' option in the messagebox, as it will close the workbook
without saving or asking if you want it to.

And to have it pop up when you open the workbook, try this in the ThisWorkbook module.

Private Sub Workbook_Open()
With UserForm1
.TextBox1.SetFocus
.Show
End With
End Sub

(Sorry...)
 
Last edited:
Upvote 0
I thought people have given up for thi thread but there you are Dan!
Great!

Hmmmm...let me check this out now. thanks again Dan!;)
 
Upvote 0
You're very welcome Pedie.
You know, if you want to we can do this without the need for the cdmenter button. We can use the AfterUpdate event in the textboxes themselves, which would mean when the user enters the wrong username or password, your message would pop up when they hit enter (or try to tab out of the textbox) after making their entry. No need for the button to click when their finished entering.
The only advantage to having it as it is now, is that if someone were trying to get in unauthorized, with the button they don't know if it was the username or the password that they got wrong. (But we could tie the code to the password textbox only and still retain that advantage.)

Sound like something you want to try? or do you want to just keep the button?

[EDIT:]
Oh... and check the spelling in the messagebox message. Right now it says 'Wong info'. . . instead of 'Wrong info'... (oops)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top