urgent:to lock and unlock a form that with a subform

aabbbcc

Registered User.
Local time
Today, 15:02
Joined
Nov 14, 2015
Messages
28
Hi
I would like know a code that lock the entire form and subform(actually it is a inserted table named "ABC" for example)

and also want a button to unlock the above.

i have tried many code that found online.
but all are failed.



Origially i set the properties to not allowded edit of the form.

And the button code is :
__________________________________
Private Sub cmdUnlock_Click()
Me.AllowEdits = True
End Sub
__________________________________

It is worked on main form.( the from is unlocked while i click the unlock button)
but the inserted table doesnt work .

It would be glad that i can received help here.
Thanks
 
Last edited:
If you only have a couple of sfrms on the main form then:

Code:
Dim ctl as control 

For each ctl in me.controls
     Select case ctl.controltype
          Case acSubform
               Ctl.form.allowedits = False 'or true

     End Select 

Next

You could put this in a function and call this recursively if you have sfrms which may have their own sfrms, and pass in the boolean variable.

If not all sfrms should be locked then use the .tag property of the ctl with a phrase that you can test for with inStr().
 
Thanks for promt reply

and I tried the following,It also doesnt work with the subform.

_____________________________________________
Private Sub checkLocks_Click()
Me.AllowEdits = True
Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acSubform
ctl.Form.AllowEdits = False 'or true
End Select
Next
End Sub
_________________________________________________
 
Are you able to upload a copy of your database?
 
I set the property of data
Allow editing :No

and the subform is a linked table.
 

Attachments

  • Untitled2.jpg
    Untitled2.jpg
    63.4 KB · Views: 109
Last edited:
I have put together a quick example database demonstrating how to lock/unlock existing records as well as prevent/allow new records.

The example uses a main form with a subform that has its source set to a table.
 

Attachments

thanks
i m trtying this code.
but employee name in your example does't locked/unlocked when i click the button.
 
It is working correctly for me. It locks with the lock button, and unlocks with the unlock button. Try typing in the field after locking it.

If you want the field to be "disabled" i.e. greyed out, then we need to look at a different procedure.
 
Thanks so MUCH..That is really helpful.!!!!

But other problem pops up.
because i want to appear a new record whenever the form is opned.
and people can edit it on the new record as defaulted
Meanwhile, if the form go to other old record, the form will be defaulted to be locked.
seems that the following code cant be used at the same time.


Private Sub Form_Current()
Call Lock_Records(False)
Call Lock_New_Records(False)
End Sub

Private Sub Form_Load()
DoCmd.GoToRecord , , acNewRec
End Sub
 

Attachments

awesome!
it does really help!! THANKS SO MUCH!!

and what if i wanna add a code to ask people to save it or not after they update their form or subform??

I use the following code for the entire form:
_____________________________________________________
Option Compare Database
Option Explicit
_____________________________________________________
Private Sub Lock_Records(blnLock As Boolean)
Me.AllowEdits = blnLock
Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acSubform
ctl.Form.AllowEdits = blnLock
End Select
Next
End Sub
_____________________________________________________
Private Sub Lock_New_Records(blnLock As Boolean)
Me.AllowAdditions = blnLock
Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acSubform
ctl.Form.AllowAdditions = blnLock
End Select
Next
End Sub
_______________________________________________________
Private Sub cmdUE_Click()
Call Lock_Records(True)
Call Lock_New_Records(True)
End Sub
_____________________________________________________
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strResponse As String
If Me.Dirty Then
Select Case MsgBox( _
Prompt:="Data updated" & _
vbCrLf & "Saved?", _
Buttons:=vbExclamation Or vbYesNo)
Case vbYes
DoCmd.RunCommand acCmdSave
Call Lock_Records(False)
Call Lock_New_Records(False)
Case vbNo
DoCmd.RunCommand acCmdUndo
Call Lock_Records(False)
Call Lock_New_Records(False)
End Select
End If
End Sub
_____________________________________________________
Private Sub Form_Current()
If Me.NewRecord = True Then
Call Lock_Records(True)
Call Lock_New_Records(True)

Else
Call Lock_Records(False)
Call Lock_New_Records(False)
End If
End Sub
_________________________________________________
Private Sub Form_Load()
DoCmd.GoToRecord , , acNewRec
End Sub
_________________________________________________
Private Sub cmdSave_Click()
If Me.Dirty Then
Select Case MsgBox( _
Prompt:="Data updated" & _
vbCrLf & "Saved?", _
Buttons:=vbExclamation Or vbYesNo)
Case vbYes
DoCmd.RunCommand acCmdSave
Call Lock_Records(False)
Call Lock_New_Records(False)
Case vbNo
DoCmd.RunCommand acCmdUndo
Call Lock_Records(False)
Call Lock_New_Records(False)
End Select
End If
End Sub
______________________________________________

But some problems pop up again sadly,

1,it only works on the main form, but ignore the update of subform when ticked the save button

2, when i tick the save button, and i save "Yes", it will pop up the messages box again to ask me save it or not. But No problem when i say No.

3, When i enter data to the entired new record and click save button. There is a message appearing that data cant be saved because of errors.

It is hoped that you could kindly help.
Thanks again!
 
Last edited:

Users who are viewing this thread

Back
Top Bottom