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

Locking a form with a checkbox

Status
Not open for further replies.

tcapp

Programmer
Apr 18, 2000
9
US
I am trying to lock a form with a checkbox to toggle editing on/off to prevent record corruption. I have been using the Recordlocks property but have not been successful in making it work. I would appreciate any idea's or recommendations on another way to make this work.
 
Set the form's Allow Edit, Allow Deletions and Allow Additions to No in the form's property window of the design view.

In the check box's Click event put...

If CheckBox = True Then
Me.AllowEdit = True
Me.AllowAdditions = True
Me.AllowDeletions = True
Else
Me.AllowEdit = False
Me.AllowAdditions = False
Me.AllowDeletions = False
End If

If this is not what you are looking for, give us more detail on how it will be determined if a person will be able to edit and what type of editing.

B-)
ljprodev@yahoo.com
ProDev
MS Access Applications
 
I am trying to use the code show here to prevent edits when a form is loaded. I try putting the code on the forms open event but it doesn't seem to work. I even went so far as to tell the form
on load
me.allowedits= false
me.allowadditions= false
me.allowdeletions=false

But I can still edit data. I have a checkbox called "finalize" on my form that I am referring to in the code. Zorro
 
Yes it looks just like that, then I pasted what you typed in place of mine and still I can type whatever I please. I don't get it. Zorro
 
i use the form's "OnCurrent" property and set the AllowEdits and AllowAdditions properties to "False". This resets the properties each time the form is refreshed or the table is skipped through. i also create a button to allow edits only with the following "OnClick" code:
Me.AllowEdits = Not Me.AllowEdits
Me.Edit_Lbl.Visible = Not Me.Edit_Lbl.Visible
i have a label at the top of the form (in contrasting colors) that alerts the user that they are editing the data.
Dave
 
To All . . . . .

For [blue]full toggle control[/blue] of locking/unlocking individual records ([purple]locked records stay locked[/purple]) try this:
Code:
[blue]The following scenario allows the user to [blue][b]Lock/UnLock[/b][/blue] editing for individual records of a form or subform, wether that form is in [blue]single or continuous view.[/blue]

Note: in any code that follows, you substitute names in [purple][b]purple[/b][/purple].

1) Add a CheckBox to the table holding the records of interest. Name it [blue][b]LockEdit[/b][/blue].

2) Make sure the CheckBox is included in the [blue]RecordSource[/blue] of the form! [purple][b]Do not insert the checkbox on the form as it works in the background![/b][/purple]

3) Add the following code to the [blue]On Current[/blue] Event of the form:
[code][blue]   If Me.NewRecord Or Not Me!LockEdit Then
      Me.AllowEdits = True
   Else
      Me.AllowEdits = False
   End If[/blue]
4) Add the following code to the [blue]Code Module[/blue] of the form:

Code:
[blue]Public Sub SavRec()
   DoCmd.RunCommand acCmdSaveRecord
End Sub[/blue]
5) Add the following to a module in the module window:
Code:
[blue]Public Function tglEdit()
   
   If IsOpenFrm("[purple][b]MainFormName[/b][/purple]") Then
      Dim frm As Form
      
      Set frm = Forms![purple][b]MainFormName[/b][/purple]

      [green][b]'Add the following line only if controlling subForm:[/b][/green]
      Set frm = frm![purple][b]SubFormName[/b][/purple].Form
      
      If Not frm.NewRecord Then
         If frm!LockEdit Then
            frm.AllowEdits = True
            frm!LockEdit = False
            Call frm.SavRec
         Else
            frm!LockEdit = True
            Call frm.SavRec
            frm.AllowEdits = False
         End If
      End If
   End If
      
End Function

Function IsOpenFrm(frmName As String) As Boolean
   Dim cp As CurrentProject, Frms As Object
   
   Set cp = CurrentProject()
   Set Frms = cp.AllForms
   
   If Frms.Item(frmName).IsLoaded Then
      If Forms(frmName).CurrentView > 0 Then IsOpenFrm = True
   End If
   
   Set Frms = Nothing
   Set cp = Nothing

End Function
[/blue]
[blue]Edit Control Options:[/blue]
You can use a Command Button, a key combination macro in AutoKeys, or call the routine [blue]tglEdit[/blue] from anywhere you like. Just [purple]remember you can't use a control in a record or you'll be able to lock it but not unlock.[/purple] I perfer the HotKey method.

Special Note: If you have [blue]Access 2000 or higher[/blue], [blue]Conditional Formatting[/blue] will provide a nice visual que of those records that are locked!

[blue]Cheers! . . . .[/blue]

See Ya! . . . . . .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top