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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Require Password to Email File

Status
Not open for further replies.

dunkyn

Technical User
Apr 30, 2001
194
US
I have a userform with a textbox that acts like a password screen.
Two problems:

1) I want to have the user hit enter after entering the password. Now, after one character is entered (changed) the macro is executed. I tried changing the event to enter (Private Sub TextBoxPassword_Enter), but then my userform won't load. ? I want the password to be more than one character.

2) How do I disable the "File Send" feature so that a file cannot be sent manually by the user? I want them to have to use a drop down menu to email the file to another department, which invokes the password requirement.

Private Sub TextBoxPassword_Change()
On Error Resume Next
Me.Hide

Dim NoGoMsg As String
NoGoMsg = "Invalid Password. File not sent."

TextBoxPassword.SetFocus

If TextBoxPassword.Value = "x" Then
ActiveWorkbook.SendMail Recipients:="Smith, Sally", Subject:="Authorized File Transfer", ReturnReceipt:=True
MsgBox "File Sent. You will get a 'return receipt'."
Unload UserFormEMail

ElseIf TextBoxPassword.Value <> &quot;x&quot; Then
MsgBox NoGoMsg, vbCritical
Unload UserFormEMail
End If
End Sub

Many thanks.

 
Hi Wigmore,

I think the problem here is that the
Code:
_Enter
event does not refer to the enter key, but to the focus entering the text box. Therefore when the form loads, the
Code:
_Enter
event is automatically triggered as the focus is on the textbox.

You would be better off using the
Code:
_Change
event. Here's a quick example:
Code:
Private Sub TextBoxPassWord_Change()

Dim pWord As String 'put password in String variable
pWord = &quot;password&quot; 'this will be case sensitive
If Len(TextBoxPassWord.Value) = Len(pWord) Then
  If TextBoxPassWord.Value = pWord Then
  'execute code for good password
  MsgBox &quot;Good Password&quot;
  Else
  'execute code for bad password
  MsgBox &quot;Bad Password&quot;
  End If
ElseIf Len(TextBoxPassWord.Value) > Len(pWord) Then
  'execute code for bad password
  MsgBox &quot;Bad Password&quot;
Else
  Exit Sub
End If

End Sub
I hope this helps, SteveB.
 
I'll give it a try. Thank you very much for taking the time to help me!
 
That last suggestion was a late Friday afternoon job, and now I've thought about it I'm not sure it's a good solution. Counting the characters entered and responding when the number matches the password will give away the length of the password. Also, nothing happens until the number of characters matches or exceeds that in the password which is confusing for the user.

It would be more conventional to have an enter button that responds to a mouse click or the enter key, so that the user decides when to submit their attempt at the password.

Try putting this code in the form (with a button called CommandButtonEnter and your text box called TextBoxPassword):
Code:
Private Sub CommandButtonEnter_Click()
'This event runs if command button clicked
Call ChkPassword

End Sub

Private Sub TextBoxPassword_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'This event runs if enter button pressed
Call ChkPassword

End Sub

Function ChkPassword()

Dim pWord As String 'put password in String variable
pWord = &quot;password&quot; 'this will be case sensitive
If TextBoxPassword.Value = pWord Then
  'execute code for good password
  MsgBox &quot;Good Password&quot;
  Unload Me
Else
  'execute code for bad password
  MsgBox &quot;Bad Password&quot;
End If
Regards, SteveB.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top