locking fields even if they have unsaved changes

penfold1992

Registered User.
Local time
Today, 09:58
Joined
Nov 22, 2012
Messages
169
I have a button on my form which locks (and disables) some fields.
by locking and disabling fields, I am able to change the back color of the fields to a "grayed out" look to warn the user that they cannot change the contents of the field.

some of the fields that I have disabled are in fact option buttons and check boxes which have no back color to change, keep this in mind!

So i have a macro that changes ALL of the fields to disabled and locked, as well as their background color.

This causes an error with the check boxes and option boxes so to bipass them I have got the clause "On Error Resume Next" so that my macro continues to complete and finish locking the rest of the form.

With this "On Error Resume Next", If the code reaches a field which has been changed, another error occurs due to a field being changed which blocks the rest of the code from working however I have no idea why it wont change regardless of the issue, I dont care if the content was saved to a record or not, I just want to put the fields into disabled and locked.

Is there a way I can remove this "You Can't Lock A control while it has unsaved changes" error to ignore that there has been changes? Where the changes just dont matter?
 
With macro it is hard to help, could you change it to VBA so the code would be easier to manipulate?

On Error Resume Next is not a very good programming strategy.. Lookup on Error Handling in VBA..
 
With macro it is hard to help, could you change it to VBA so the code would be easier to manipulate?

On Error Resume Next is not a very good programming strategy.. Lookup on Error Handling in VBA..

Code:
KeyContent = Array(Me.Field1, Me.Field2,Me.Field3,Me.Field4)
 
If Me.Field1.Enabled = True Then
    For Each Var In KeyContent
        Var.Enabled = False
        Var.Locked = True
        Var.BackColor = RGB(217, 217, 217)
    Next Var
Else
    For Each Var In KeyContent
        Var.Enabled = True
        Var.Locked = False
        Var.BackColor = RGB(255, 255, 255)
    Next Var
End If
knowing that Field2 is a tick box, I know it is going to fail because it doesnt have a Backcolor, I want the code to resume here and move onto the next Var so adding Error Resume Next works fine here. I am intentionallly passing though the error.

The problem occurs when the fields have been unlocked and something has been entered in these fields and closed without saving. If there was a way to rid this issue by allowing changes to the fields. I dont want it to write to the tables or create a new record or anything, I just want to put text into the field and then lock it.
 
Why not declare them as Controls so then you can check for what control you are dealing with before setting the control's back color?
Code:
    Dim ctrl As Control
    For Each ctrl In Me.Controls
        If ctrl.ControlType <> acCheckBox Then 
[COLOR=SeaGreen]            'do the code[/COLOR]
       End If
    Next
 
because its not using all of the controls on the form, just some of them. (its a large form)
which is why I looped them like i did. Alternatively I could separate the two out (ones with a backcolor and ones without) and do two loops
however if I could just do something like...
On Error.Number = 438 Resume Next

that would work too.... or If locking didnt require the field to be changed.
 
You could have the Error handling routine to skip the specific error..

Just curious, What is the type you have declared the Array as?
 
Variable.

I tried something liek this:

Code:
    For Each Var In KeyContent
        Var.Enabled = False
        Var.Locked = True
        If Var.ControlType <> 105 Then
            Var.BackColor = RGB(217, 217, 217)
        End If
    Next Var
to try and bypass coloring the option buttons (as well as acOptionButton)
but neither of them appeared to bypass it at all... I guess Var.ControlType didnt return the correct value or something?
 
Declare KeyContent As Controls..

Cant do that because KeyContent must be a variant as it is an array...

so to re-summarize what is trying to be done:

go through a list of fields, lock and disable them and change the BG Color to gray.
If they are a option or tick box, the background cannot be changed therefore those need to be skipped.

Code:
Dim Var As Variant
Dim KeyContent As Variant
 
KeyContent = Array( -a list of fields- )
 
    For Each Var In KeyContent
        Var.Enabled = False
        Var.Locked = True
        If Var.ControlType <> "105" Or Var.ControlType <> "106" Then
            Var.BackColor = RGB(217, 217, 217)
        End If
    Next Var

Runtime Error: 438 Object does not support this property or method on the line "Var.BackColor = RBG()" because it hasnt found the controltype to be an option or tick box...
 
What are the control types that you have on the Form? As you have eliminated the Controls CheckBox and OptionGroup.. Other than that?? Try debugging the code, fins which control is throwing the error.. Might shed some light..
 
the option group is throwing up the error, and the rest of the controls are fine.
 
So check what is the back style for the Option Group, you cannot change the color unless the Back Style is Normal.. I think by default is Transparent.. I might be wrong..
 
option group.
just disable the group (frame) to disable all the buttons. The whole thing greys out

checkbox.
the check box disables, but you cannot detect a different appearance in the check box. however, the associated label greys out.


----
on error resume next is perfectly reasonable to suppress an anticipated error, that you know can be safely ignored.

if having a dirty form causes an issue with this, then the answer is to save the form first

so



Code:
on error goto fail
if me.dirty then me.dirty = false
 
for each ctl in me.controls
      on error resume next
      ctrl.enabled = false
      ctrl.backcolor = whatever 
next
exit sub
 
fail:
    msgbox "save failed"
end sub
 
Last edited:

Users who are viewing this thread

Back
Top Bottom