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

How to Password Protect a Checkbox? 2

Status
Not open for further replies.

Buckar00B0nzai

Technical User
Jun 17, 2009
50
US
I have a form that tracks project status, and the individuals who use the form are not supposed to move from phsae 1 to phase 2 (all the way through phase 5) without approval from management. I have a checkbox for each phase (I-V) listed on the bottom of the form, and would like these to be password protected (for management only). If someone checks (or "un-checks) the box and enters the correct password, the check (or "un-check") should stay. If they enter the incorrect password, I would like their Check (or "un-check") to be reversed to the original setting. I am a novice and am wondering if anyone can help. Here is the code that I have so far in the "On Click" event for the checkbox named "Phase I".

Private Sub CtlPhase_I_Click()
If InputBox("Enter Password to Delete Record") <> "Password" Then
Exit Sub
Else
GoTo Err_CtlPhase_I_Click
End If

On Error GoTo Err_CtlPhase_I_Click

Exit_CtlPhase_I_Click:
Exit Sub

Err_CtlPhase_I_Click:

If Me.CtlPhase_I.Value = -1 Then
Me.CtlPhase_I.Value = 0
Else
Me.CtlPhase_I.Value = -1

End If

End Sub

Please help.

Thanks in advance.
 
Complete. Phase checkboxes have been renamed:

chkPhase1, chkPhase2, chkPhase3, chkPhase4, and chkPhase5

Thanks again.

We're getting closer now.
 
---------------------------
PHV,
---------------------------
PHV said:
Why not using the strPhaseNumber parameter in your procedure ?

I had him add that earlier, but did not actually use it - yet.

And if he makes the suggested controlname changes, then that will be more like intPhaseNumber rather than strPhaseNumber going forward....

So....

Your part of the code could now be something like:
Code:
With Forms!Frm_Edit.Controls("CtlPhase_" & [BLUE]int[/BLUE]PhaseNumber)
  .Value = Not .Value
End With

And yeah, that'd be a great way to do it - referring to the control in a With statement rather than creating a variable of that, so fewer objects to deal with.

----------------------------
Buckar00B0nzai,
----------------------------

Code:
Public Sub ChangePhase([blue]int[/blue]PhaseNumber As [blue]Integer[/blue])
[blue]'[/blue][green]On Error GoTo Err_ChangePhase[/green]

  [blue]'[/blue]  [green]Dim ctl As Control '--May not need at all[/green]
  [blue]'[/blue]  [green]Dim frm As Form '--May not need at all[/green]

  [blue]'[/blue]  [green]Set frm = Forms!Frm_Edit '--May not need at all[/green]

    If InputBox("Enter Password to Delete Record") = "Password" Then
[GREEN]'************************************************[/GREEN]
[green]'PHV's Code[/GREEN]
    With Forms!Frm_Edit.Controls("CtlPhase_" & [BLUE]int[/BLUE]PhaseNumber)
      .Value = Not .Value
    End With
[GREEN]'************************************************[/GREEN]
    Else
        GoTo Exit_ChangePhase
    End If
    
Exit_ChangePhase:
    [BLUE]'[/BLUE][GREEN]Set ctl = Nothing[/GREEN]
    [BLUE]'[/BLUE][GREEN]Set frm = Nothing[/GREEN]
    Exit Sub

Err_ChangePhase:
    MsgBox "An Error Occured! " & Chr(13) & _
        "Error Number = " & Err.Number & Chr(13) & _
        "Error Description = " & Err.Description
    Resume Exit_ChangePhase

End Sub

--

"If to err is human, then I must be some kind of human!" -Me
 
Cleaned up code based on checkbox renames, and based on totally removing unnecessary code:
Code:
Public Sub ChangePhase(intPhaseNumber As Integer)
[green]'On Error GoTo Err_ChangePhase[/green]

	If InputBox("Enter Password to Delete Record") = "Password" Then
		[green]'************************************************
		'PHV's Code[/green]
		With Forms!Frm_Edit.Controls("CtlPhase_" & intPhaseNumber)
			.Value = Not .Value
		End With		[green]'************************************************[/green]
	Else
		GoTo Exit_ChangePhase
	End If
    
Exit_ChangePhase:

    Exit Sub

Err_ChangePhase:
    MsgBox "An Error Occured! " & Chr(13) & _
        "Error Number = " & Err.Number & Chr(13) & _
        "Error Description = " & Err.Description
    Resume Exit_ChangePhase

End Sub

And once you verify it's working with no errors, you can uncomment the "On Error" line.

--

"If to err is human, then I must be some kind of human!" -Me
 
Fooey! I didn't change the control name, but you get the idea... just in case:
Code:
Public Sub ChangePhase(intPhaseNumber As Integer)
[green]'On Error GoTo Err_ChangePhase[/green]

	If InputBox("Enter Password to Delete Record") = "Password" Then
		[green]'************************************************
		'PHV's Code[/green]
		With Forms!Frm_Edit.Controls("chkPhase" & intPhaseNumber)
			.Value = Not .Value
		End With		[green]'************************************************[/green]
	Else
		GoTo Exit_ChangePhase
	End If
    
Exit_ChangePhase:

    Exit Sub

Err_ChangePhase:
    MsgBox "An Error Occured! " & Chr(13) & _
        "Error Number = " & Err.Number & Chr(13) & _
        "Error Description = " & Err.Description
    Resume Exit_ChangePhase

End Sub

Then call it with each checkbox:
Code:
Private Sub chkPhase1_Click()
    ChangePhase(1)
End Sub

Private Sub chkPhase2_Click()
    ChangePhase(2)
End Sub

[green]'...Etc[/green]

--

"If to err is human, then I must be some kind of human!" -Me
 
The code works - but it does the opposite. It removes the "check" (or adds it) when the password ("Password") is entered correctly and does not change the "check" or "uncheck" action when the password is entered incorrectly.
 
Well, the code is only going to do exactly what you tell it to do. Right now, you're telling it this:

"If the user inputs "Password" into the box, then change the checkbox to the opposite of what it is.... But if they don't enter "Password", then don't do a thing...

And actually, I know what your problem is now.. DUH - Me!

You're using this in the On Click event... You should be using it in the Before Update event...

--

"If to err is human, then I must be some kind of human!" -Me
 
So, if you use the BeforeUpdate, then the choices could be totally different... And I've got an idea there.. just a moment..


--

"If to err is human, then I must be some kind of human!" -Me
 
Alright, and I may be forgetting a piece of the Cancel portion, but I think it's correct... Basically, by using the Before Update event will cancel out any actions if Cancel = [blue]True[/blue].

Code:
Public Function ChangePhase(intPhaseNumber As Integer) As Boolean
On Error GoTo Err_ChangePhase

	If InputBox("Enter Password to Delete Record") = "Password" Then
		ChangePhase = False
	Else
		ChangePhase = True
	End If
    
Exit_ChangePhase:
    Exit Function

Err_ChangePhase:
    MsgBox "An Error Occured! " & Chr(13) & _
        "Error Number = " & Err.Number & Chr(13) & _
        "Error Description = " & Err.Description
    Resume Exit_ChangePhase

End Function

Then Call it with: (and make sure the correct "event" is pointing to the code in the controls' properties...
Code:
Private Sub chkPhase1_BeforeUpdate(Cancel As Boolean)
    Cancel = ChangePhase(1)
End Sub

Private Sub chkPhase2_BeforeUpdate(Cancel As Boolean)
    Cancel = ChangePhase(2)
End Sub

--

"If to err is human, then I must be some kind of human!" -Me
 
Somehow the OnCurrent events that I have the form itself perofrming are interfering with this latest code: I have the form pull and combine a few fields for a summary title OnCurrent.

The error message reads: The expression OnCurrent you entered as the event propoerty setting produced the following error: Procedure declaration does not match description of event or procedure having the same name.

Is there anyway to keep the event "On Click" and rework it to do the opposite of its current function (relating to accepting changes if the password is correct)?

 
By the way - I should hav caught this - the message box should read "Enter Password to Approve". I updated this on my end. Sorry if this created any confusion.
 
I have the form pull and combine a few fields for a summary title OnCurrent.

Well, if it referenced any of the same control names that you updated, then the references to those control names will need to be changed as well.

The easiest way to make sure all are updated is to do use the Find and Replace feature - [b[]<Ctrl>[/b] and <H> on your keyboard when you have the VB Editor window active. Change the scope to "Project" instead of "Module" or "Selection"... Put the old control value in the top text box, and the new control name in the bottom text box, and tell it to replace all... you'll have to do this for each control name you changed, if there is any code tied to it. That will help make sure you don't miss any of them.

--

"If to err is human, then I must be some kind of human!" -Me
 
That's the weird part. These checkboxes (1-5) are new fields. They aren't referenced in any other events whatsoever.
 
Thanks to help from both of you - I've got it.

The public module should be:

Public Sub ChangePhase(intPhaseNumber As Integer)
'On Error GoTo Err_ChangePhase

If InputBox("Enter Password to Approve") <> "Password" Then
'************************************************
'PHV's Code
With Forms!Frm_Edit.Controls("chkPhase" & intPhaseNumber)
.Value = Not .Value
End With '************************************************
Else
GoTo Exit_ChangePhase
End If

Exit_ChangePhase:

Exit Sub

Err_ChangePhase:
MsgBox "An Error Occured! " & Chr(13) & _
"Error Number = " & Err.Number & Chr(13) & _
"Error Description = " & Err.Description
Resume Exit_ChangePhase

End Sub

I simply changed the password evaluation from "=" to "<>".

I call the procedure in the OnClick event for the checkbox with:

Private Sub chkPhase1_Click()
ChangePhase (1)

End Sub

Thank you both. Stars for you dudes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top