Locking/Unlocking Option Box

Kapellu2

Registered User.
Local time
Today, 03:51
Joined
Sep 9, 2010
Messages
50
Hey everyone,

I had a question regarding option boxes. I have a form in my program used to edit information. I would like this form to be locked upon opening to ensure that the user doesnt change a value on one of the records by accident. At the top of the form I would like to have two option boxes, one for locking the form and one for unlocking it.

What would the code look like to accomplish this?
 
The key here is you can't use the AllowEdits feature because if you use that do disable edits then you can't change the option box on the form.

So you either need to:

1. Use a main form/subform where the main form is just an unbound form so you can have your option box and then the subform is an unlinked subform for having the normal main form in it. Then the code would be:
(you would set the normal AllowEdits of the subform to NO)
Code:
Me.SubformControlNameHere.Form.AllowEdits = True
where SubformControlNameHere is the name of the subform control on the main form which HOUSES the subform and not the name of the subform itself UNLESS they both share the exact same name. The .Form. part is important because that states that you want a property of the form and not of the subform control.

or

2. You can put a tag (for example the word LOCK in the TAG property) in each of the controls you want locked and you can then use this to unlock:
Code:
Dim ctl As Control
 
For Each ctl In Me.Controls
    If ctl.Tag = "Lock" Then
       ctl.Locked = False
    End If
Next

And then you can use the same code but setting to TRUE to unlock.

And in order to unlock and then lock again before navigating to another record, you MUST include a save of the record otherwise just changing to unlock will not lock it again until you do.

So,
Code:
If Me.Dirty Then Me.Dirty = False
 
1. Use a main form/subform where the main form is just an unbound form so you can have your option box and then the subform is an unlinked subform for having the normal main form in it. Then the code would be:
(you would set the normal AllowEdits of the subform to NO)

Code:
Me.SubformControlNameHere.Form.AllowEdits = True

Hmm since I have a large number of controls I think the first option would probably be my best bet. I am a little bit confused as to how you designate everything with the option boxes as the main form and everything without them as a sub form.
 
I am a little bit confused as to how you designate everything with the option boxes as the main form and everything without them as a sub form.

That would be because you need to create a new, blank form which has the options on it and then you drag and drop your current form (minus the options) on to that new form and then you have yourself a main form and subform with the subform not linked. The main form should NOT have a record source. It is just there to provide the means to lock and unlock the OTHER form so that the controls on the main form remain usable when the form is locked.
 
Haha alright that makes a little bit more sense then. I tried doing the tag method as it seemed a little more straight forward. Right now i have the code:

Code:
Private Sub OptionLock_GotFocus()
Dim ctl As Control
 
If OptionLock = 2 Then
For Each ctl In Me.Controls
    
    If ctl.Tag = "Lock" Then
       ctl.Locked = False
    End If       
    Next
         
Else: ctl.Locked = True
End If
End Sub

But I get an error saying I have entered an expression which has no value.
The debug option takes me to the line "If OptionLock = 2 Then"
 
Also, what I recommend rather than assign a tag at design time, is expose a custom array of the controls on the form you want to enumerate. This self documents the list of controls that are involved in the operation and provides a way way way more efficient loop...
Code:
private m_controls as variant

Property Get MyControls as Variant
[COLOR="Green"]  'exposes a custom array of controls as listed below[/COLOR]
  If IsEmpty(m_controls) then 
[COLOR="Green"]    'array is created once when first referenced[/COLOR]
    m_controls = Array(Me.SomeControl, Me.Control1, Me.OptionBox)
  End If
  MyControls = m_controls
End Property

Sub LockMyControls(State as Boolean)
[COLOR="Green"]' Sets the locked state of controls is the custom array[/COLOR]
  For Each ctl In Me.MyControls
    ctl.Locked = State
  Next
End Sub
 
Hmm well im not really too familiar with arrays. I think the other method should work though if I can get the kinks worked out. I modified it a little to:

Code:
Private Sub OptionLock_GotFocus()

Private Sub OptionLock_GotFocus()
Dim ctl As Control
If Me.OptionLock.OptionValue = 2 Then
For Each ctl In Me.Controls
    
  If ctl.Tag = "Lock" Then
   ctl.Locked = False
  End If
           
  Next
        
Else: ctl.Locked = True
End If
End Sub
End Sub

When I check and uncheck the option box nothing happens though.
 
It would go in the OptionLock After Update event and I'm not sure which option 2 is (that would seem to indicate that you have at least 3 options. Option groups are zero-based so the first one is 0 and the second is 1.

So, assuming that Option 1 is 0 and is for Locking and Option 2 is 1 and is for unlocking you would use this:
Code:
Private Sub OptionLock_AfterUpdate()
Dim ctl As Control
 
For Each ctl In Me.Controls
 
  If ctl.Tag = "Lock" Then
    ctl.Locked = (Me.OptionLock.OptionValue = 1)
  End If
 
Next
 
End Sub
 
Last edited:
Hmm, I tried that code out but didn't seem to have any luck. I have the Tag property on all of the controls on the page as Lock but it doesn't seem to make any changes when i check and uncheck the lock button
 
Can you upload a copy of your database (with bogus data so we can test)?
 
It's usually pretty strict here when it comes to uploading data (even the bogus kind) unfortunitly. I'll play around with it though and keep you posted if I figure out what i'm going wrong.
 

Users who are viewing this thread

Back
Top Bottom