Read-only, protect a form...

calestra

Registered User.
Local time
Today, 03:13
Joined
Jan 23, 2003
Messages
15
Hello..... I want to add a check box on a data entry form that will change the form to 'read only' when checked, and released when unchecked.

Sorry... I'm new to this and have not found my answer anywhere!

TIA

Vince :)
 
Try:

if me!Yourcheckbox = true then
me.allowedits=false ' no edits allowed
else
me.allowedits=true 'edits allowed
end if

"AllowEdits" is a form property.
 
You know, I tried the above example previously, but then you can't un-check the box, because your form is 'read-only'. So, here is what I came up with:

Code:
Private Sub Check1_Click()
Dim fld As Control

    If Me.Check1 = True Then
        For Each fld In Me.Controls
            If fld.ControlType <> acLabel And fld.Name <> "Check1" Then
                fld.Enabled = False
                If fld.ControlType <> acCommandButton Then fld.Locked = True
            End If
        Next
    Else
        For Each fld In Me.Controls
            If fld.ControlType <> acLabel And fld.Name <> "Check1" Then
                fld.Enabled = True
                If fld.ControlType <> acCommandButton Then fld.Locked = False
            End If
        Next
    End If
    
End Sub
 
My solution works for an unbound checkbox.

You're solution works, but what if you have 100 objects on your form. But of course, this is quibbling.

I also enable/disbale the the detail section of a form to keep users from inadvertently entring data into the form before a recordsource criteria is entered in the form header section.
 
This isn't quibling, it is information exchange. If I am wrong, please do point it out. I have been wrong before, and I am sure I will be again. :)

I just tried this on an unbound checkbox and I was unable to change the value of the checkbox back to false and open up the form again to edits.

As far as having 100 controls on the form, that is why I have the For ... Next loop. To go through all of the controls, no matter the count.
 
You're checking 100 controls and 100 labels. I was quiblling with the amount of checkng you're doing.

I tried my posting before I posted it and it worked ok. I tried it on another for and it did not work.

I checked for the code I used in a program and here it is:

Private Sub LockControls()
Dim ctl As Control
Dim iCnt As Integer
For Each ctl In Me
If ctl.ControlType <= 103 Then GoTo Skip2Next
If ctl.ControlType >= 112 Then GoTo Skip2Next
If ctl.Name = "txtHoldFocus" Then GoTo Skip2Next
If ctl.Name = "cboxClientGroupID" Then GoTo Skip2Next
If ctl.Name = "cboxClientID" Then GoTo Skip2Next
If ctl.Name = "cboxFileGroupID" Then GoTo Skip2Next
If ctl.Name = "cboxFileID" Then GoTo Skip2Next
If ctl.Name = "cmdCloseForm" Then GoTo Skip2Next
iCnt = iCnt + 1
ctl.Enabled = False
Skip2Next:
Next
End Sub

There's an analagous UnlockControls routine. I check ControlType because I have all kinds of controls on my form.

The bottom line, your solution works. I thought that I recalled a better mousetrap. I'm the same mouse as you.

One of the first projects I ever had was to maximize code efficiency because it was going to be and was used zillions of times. Nanoseconds used to count. Just having something work is usually ok and of no significant concern. Ultility is often better than elegance.

Sorry for the confusion.

Regards
 
Thanks for your replys.... I'm missing something here! I enter this code under 'on click'... and just get errors when I try it.
 
What error are you getting? Have you changed the name of the checkbox used in the code to be the name of your checkbox? ... hmmm, let's see ... How about the name of the procedure? Does it read Private Sub yourcheckboxname_Click() ?


llkhoutx, you are absolutly right. It is not the most efficient code, as it does check labels, but, it does check all the other controls, too. The best way, for efficiency sake, would be to type out each control explicitly and set it's value. But, I, for one, would rather do this and spend the extra half second. I started my programming days on a UNIX back in '85 when several hundred users shared a processor. You DEFINATELY had to have efficient code then, no matter the cost of trouble to the person typing out the code. Fortunately(or unfortunately), we don't have to worry about those days. Take care.
 
This is what I entered in 'design view' under 'on click'

Private Sub Readonly_Click()
Dim ctl As Control
Dim iCnt As Integer
For Each ctl In Me
If ctl.ControlType <= 103 Then GoTo Skip2Next
If ctl.ControlType >= 112 Then GoTo Skip2Next
If ctl.Name = "txtHoldFocus" Then GoTo Skip2Next
If ctl.Name = "cboxClientGroupID" Then GoTo Skip2Next
If ctl.Name = "cboxClientID" Then GoTo Skip2Next
If ctl.Name = "cboxFileGroupID" Then GoTo Skip2Next
If ctl.Name = "cboxFileID" Then GoTo Skip2Next
If ctl.Name = "cmdCloseForm" Then GoTo Skip2Next
iCnt = iCnt + 1
ctl.Enabled = False
Skip2Next:
Next

End Sub

This is the error message:

The expression on click you entered as the event property setting produced the following error: A problem occured while Tele Address - Business data was communicting with the OLE server or ActiveX control.

* The expression may not result in the name of a macro, the name of a user-defined function, or [Event Procedure]
* There may have been an error evaluating the function, event, or macro.
 
Ok.... I see where the error was coming from! Stoopid me! However when I check the box I am still able to mess around with the data, and save the changes. Also when I close then re-open the form the check mark has gone and needs ticking again....
 
Do you have controls with these names?

If ctl.Name = "txtHoldFocus" Then GoTo Skip2Next
If ctl.Name = "cboxClientGroupID" Then GoTo Skip2Next
If ctl.Name = "cboxClientID" Then GoTo Skip2Next
If ctl.Name = "cboxFileGroupID" Then GoTo Skip2Next
If ctl.Name = "cboxFileID" Then GoTo Skip2Next
If ctl.Name = "cmdCloseForm" Then GoTo Skip2Next

Did you send your DB to llkhoutx, and he wrote this for your purpose? I thought this was from a DB he wrote previously, and you would need to put the names of controls that you still want enabled in this list. I'm guessing the name of your checkbox control is Readonly, so you would at least need to change one to this:

If ctl.Name = "Readonly" Then GoTo Skip2Next
 
If you want the form to always remember what the "ReadOnly" status is, then you are going to have to store that in a table, reference the value when you open the form, and update that value when you close the form.
 
No... the "stoopid me" bit realised my errors...

I used your code, and made sure the check-box was named Check_1

I got no errors but then no function either.....


Private Sub Check1_Click()
Dim fld As Control

If Me.Check1 = True Then
For Each fld In Me.Controls
If fld.ControlType <> acLabel And fld.Name <> "Check1" Then
fld.Enabled = False
If fld.ControlType <> acCommandButton Then fld.Locked = True
End If
Next
Else
For Each fld In Me.Controls
If fld.ControlType <> acLabel And fld.Name <> "Check1" Then
fld.Enabled = True
If fld.ControlType <> acCommandButton Then fld.Locked = False
End If
Next
End If

End Sub
 
Is this really what I want? I'm begining to wonder! I just want an easy way to protect the data shown in a form ie: I can't accidentally delete or overwrite something, but I want to be able to switch it on and off if I want to add or edit etc...

Thanks for your patience....
 
Remember that you must also go to the properties sheet and make sure the OnClick event says Event Procedure in order to fire off the code. And, it is Check1, not Check_1. Check these out.
 
The techique that I use is that on the OnCurrent event I lock all the bound objects in my form and change their background color to a light red, then if I want to edit the any of the bound objects, I double click any one of two or three conspicuously marked fields (with a heaver border that usual) on any which event I unlock the bound controls and change their background color to a light green. This is fast any easy for the users, even though the code is long. It's fast and effective, not elegant.

Also, on an object's GotFocus event I change it's background color, or its label's color if a check box, etc., to a bright green indicating that field has focus, then back to its original color on LostFocus event. The user always knows where the cursor is on a complicated form. Screen.Activecontrol.Background= for data fields and for labels, I use a Select Case to select the proper label name. It's cumbersome, by fast and effective. It wows the users.
 
Last edited:
Thanks for your advise everyone! Rich... I have tried your method but am stumped. I cannot find an 'On Enter' event anywhere on my original main form (now the sub form) in 'design view'.

You deleted your post?
 
Last edited:
FYI

The OnEnter event refers to controls on forms. I never saw Rich's post, so ...

We're you still unable to get my routine to work?
 
Hello Pdx_Man.... I have tried all the example's so far and have been unable to get any working. Now... I know this is due to my lack of expeience! I have copied your instructions to the letter, but only get the error as shown earlier, when I tick the box.
 
What error was that? You indicated that you had no functionality, but no error. If you right-mouse click on the check-box and select Build Event ... , it should take you to my procedure. Does it? If not, then the code is not tied to the check-box. If it does, try re-running the form and tell me any error it gives you.

I know debugging can be tedious, we'll all get it figured out! :)
 

Users who are viewing this thread

Back
Top Bottom