Password Protect Forms Code

spectraflame

Well-known Member
Joined
Dec 18, 2002
Messages
830
Office Version
  1. 365
Platform
  1. Windows
I am using the following code to password protect a form. However, if the user does not key anything into the password text box and clicks the OK button, the form still opens.

How do I add a statement to the code that will error out if the text box is blank?

Matthew
____

Private Sub Command3_Click()

On Error GoTo Err_Command3_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "Form1"

If Me.Text0 <> "test" Then
MsgBox "Incorrect Password!"
Exit Sub

Else
DoCmd.Close
DoCmd.OpenForm stDocName, , , stLinkCriteria
'DoCmd.OpenForm FormName = "Form1"
End If

Exit_Command3_Click:
Exit Sub

Err_Command3_Click:
MsgBox Err.Description
Resume Exit_Command3_Click

End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Instead of a negative test, how about an affirmative test.
If the password is correct, do an action -- if it's not do nothing.

Mike

Code:
Private Sub Command3_Click() 

On Error GoTo Err_Command3_Click 
Dim stDocName As String 
Dim stLinkCriteria As String 
stDocName = "Form1" 

If Me.Text0  "test" Then 
  DoCmd.Close 
  DoCmd.OpenForm stDocName, , , stLinkCriteria 
  'DoCmd.OpenForm FormName = "Form1" 
Else 
  MsgBox "Incorrect Password!" 
End If 

Exit_Command3_Click: 
Exit Sub 

Err_Command3_Click: 
MsgBox Err.Description 
Resume Exit_Command3_Click 

End Sub
 
Upvote 0
I caught that oversight when I copied the code. No big deal.

Thanks again,
Matthew
 
Upvote 0

Forum statistics

Threads
1,221,825
Messages
6,162,165
Members
451,750
Latest member
dofrancis

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