Password Protect Specific Textboxes in Excel Userform

I have a userform with some textboxes.
I created a button that disables some of the textboxes so no information can be entered.
Then I created another button on the userform to re-enable those textboxes.
What I am looking for is some code that if the button to re-enable those textboxes is clicked that another box pops-up that asks for a password.
If the password is entered correctly those textboxes are re-enabled and if the password is incorrect they receive an error message and those textboxes remain disabled.

Here is the code I used to disable the TextBoxes

Private Sub LockDataButton_Click()

    RrtNm.Enabled = False
    Loc.Enabled = False
    Tier.Enabled = False
    GeoCde.Enabled = False
    DivCmb.Enabled = False
    RegCmb.Enabled = False
    OpgDt.Enabled = False
   
End Sub



Thank you,

Edwin
gixxer1020Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

[ fanpages ]IT Services ConsultantCommented:
Hi Edwin,

Here is a very basic approach:

Private Sub UnlockDataButton_Click()

  Dim strInput                                          As String
  
  strInput = InputBox("Unlock Data Button", _
                      "Password required", _
                      "")

  If strInput = "Password" Then
     RrtNm.Enabled = True
     Loc.Enabled = True
     Tier.Enabled = True
     GeoCde.Enabled = True
     DivCmb.Enabled = True
     RegCmb.Enabled = True
     OpgDt.Enabled = True
  End If
    
End Sub

Open in new window



However, anybody reading the Visual Basic for Applications code will be able to find the explicit value of the Password ("Password", above).

I would recommend setting a VBProject password to protect your code from prying eyes; although, unfortunately, this is still not infallible (as there are many methods to "crack"/"hack" VBProject passwords available through a few minutes use of Google.com).

BFN,

fp.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gixxer1020Author Commented:
Thanks Ben,  That works great.
Just curious, what would that code look like if we had it pull up another form with the password box?
The main reason being that I would like some control over how that password box looks.

Thanks,

Edwin
0
[ fanpages ]IT Services ConsultantCommented:
Hi,

Ben isn't here at the moment.

Is it OK if I reply with a sample workbook (as attached)?

The code from the "frmPassword" code module is as follows:

Option Explicit
Private Sub chkShow_Password_Click()

  On Error Resume Next
  
  Me.txtPassword.PasswordChar = IIf(Me.chkShow_Password.Value, "", "*")
  
End Sub
Private Sub cmdCancel_Click()

  On Error Resume Next
  
  Me.txtPassword = vbNull
  
  Me.Hide
  
End Sub
Private Sub cmdOK_Click()

  On Error Resume Next
  
  Me.Hide
  
End Sub
Private Sub UserForm_Activate()

  On Error Resume Next
  
  Me.txtPassword = ""
  Me.chkShow_Password.Value = False
  
End Sub

Open in new window


The code from the "frmQ_28246601" code module is as follows:

Option Explicit
Private Sub LockDataButton_Click()

  On Error Resume Next

  RrtNm.Enabled = False
  Loc.Enabled = False
  Tier.Enabled = False
  GeoCde.Enabled = False
  DivCmb.Enabled = False
  RegCmb.Enabled = False
  OpgDt.Enabled = False
    
  Me.LockDataButton.Enabled = False
  Me.UnlockDataButton.Enabled = True
  
End Sub
Private Sub UnlockDataButton_Click()

  On Error Resume Next
  
  frmPassword.Show
  
  Select Case (frmPassword.txtPassword)
  
      Case (vbNull)
          Beep
          MsgBox "Password entry cancelled.", _
                 vbInformation Or vbOKOnly, _
                 ThisWorkbook.Name
      
      Case ("Password")
          RrtNm.Enabled = True
          Loc.Enabled = True
          Tier.Enabled = True
          GeoCde.Enabled = True
          DivCmb.Enabled = True
          RegCmb.Enabled = True
          OpgDt.Enabled = True
     
          Me.LockDataButton.Enabled = True
          Me.UnlockDataButton.Enabled = False
          
          Beep
         
      Case Else
          Beep
          MsgBox "Invalid Password entered.", _
                 vbExclamation Or vbOKOnly, _
                 ThisWorkbook.Name
         
  End Select ' Select Case (frmPassword.txtPassword)
    
  Unload frmPassword
  
End Sub
Private Sub UserForm_Activate()

  On Error Resume Next
  
  RrtNm.Enabled = True
  Loc.Enabled = True
  Tier.Enabled = True
  GeoCde.Enabled = True
  DivCmb.Enabled = True
  RegCmb.Enabled = True
  OpgDt.Enabled = True

  Me.LockDataButton.Enabled = True
  Me.UnlockDataButton.Enabled = False

End Sub

Open in new window


Finally, I have added this code to the "wbkQ_28246601" (ThisWorkbook) code module;

Option Explicit
Private Sub Workbook_Open()

  On Error Resume Next
  
  frmQ_28246601.Show
  
End Sub

Open in new window


B)ye F)or N)ow,

fp.
Q-28246601.xls
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

gixxer1020Author Commented:
Hi fp,

I'm sure your code should work but it is to complicated for me.
The Userform I have is loaded with code and this much additional code is kind of wreaking havoc on everything =)

Is there no simpler way to achieve my goal?

Thanks,

Edwin
0
[ fanpages ]IT Services ConsultantCommented:
I'm sure your code should work but it is to complicated for me.

If you open the attached workbook, you will see it does work!

The Userform I have is loaded with code and this much additional code is kind of wreaking havoc on everything =)

May I suggest that if you cannot integration the very few lines of code I did write in the sample UserForm, then you upload your own workbook & I can advise further?

Is there no simpler way to achieve my goal?

It is difficult to say without seeing what you have done & continuing to speak conceptually on code I have no knowledge of.
0
gixxer1020Author Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.