Lock Fields till a Edit button is pressed

vapid2323

Scion
Local time
Today, 07:12
Joined
Jul 22, 2008
Messages
217
This seems simple but when I searched with google and also local on this site I could not fine the info.

I just want all my fields to be locked/Diabled till a user presses a button. I just need a little VBA tip.

Thanks for the help!
 
Set form properties Add, Edit, Deletions, data entry all to no,

Then in vba behind the button change them to yes (Click event)

Then on the form close event change them back to no.

Ex:

Code:
Me.Forms.AllowEditions = True
Me.Forms.AllowDeletions = True
'etc....
 
They are several things wrong with Kryst51's code:

AllowEditions is incorrect; it should be AllowEdits.

Me.Forms.AllowDeletions = True

is also incorrect. The proper syntax would be

Me.AllowDeletions = True

without the Form being in there.

Setting the form properties AllowAdditions, AllowEdits, etc to No in the Property pane makes changing them back, after setting them to Yes in code, unnecessary in the Form_Close event. These properties will be 'reset' each time the form opens, automatically.

Making these corrections, Kryst51's suggestions will work, assuming you want all records to be editable once the button is clicked.

If, on the other hand, you only want the current record to be editable, with the form returning to Read-Only on moving to another record, you need to set the appropriate "Allow" properties to No in the Form_Current event.

Which situation suits your needs, and do you want to also control the ability to add new records with this button as well or just prevent editing of existing records?

Linq ;0)>
 
They are several things wrong with Kryst51's code:

AllowEditions is incorrect; it should be AllowEdits.

Me.Forms.AllowDeletions = True

is also incorrect. The proper syntax would be

Me.AllowDeletions = True

without the Form being in there.

Setting the form properties AllowAdditions, AllowEdits, etc to No in the Property pane makes changing them back, after setting them to Yes in code, unnecessary in the Form_Close event. These properties will be 'reset' each time the form opens, automatically.

Making these corrections, Kryst51's suggestions will work, assuming you want all records to be editable once the button is clicked.

If, on the other hand, you only want the current record to be editable, with the form returning to Read-Only on moving to another record, you need to set the appropriate "Allow" properties to No in the Form_Current event.

Which situation suits your needs, and do you want to also control the ability to add new records with this button as well or just prevent editing of existing records?

Linq ;0)>

Hey Linq, thanks for the corrections, I was trying to remember on the fly how to do it. :)
 
I tend to structure this kind of thing rather than applying it directly to the button code.

This kind of structure is especially useful when the mode is switched on or off by multiple triggers.

Code:
Private Sub EditMode(Allow as Boolean)
 
   With Me
      .AllowEdits = Allow
      .AllowDeletions = Allow
      .AllowAdditions = Allow
   End With
 
End Sub
 
Private btnEdit_Click()
 
   EditMode True
 
End Sug

An extension to the concept includes a parameter for the form reference allowing it to be reused.
 
Has anyone written a tutorial in the code repository on reusing code?
 
or, you can toogle booleans

me.allowedits = not me.allowedits
 
Has anyone written a tutorial in the code repository on reusing code?

Reusing code can be something of an art but basically it involves adding arguments to the Sub to direct the action to the target objects.

For example, if you had multiple subforms you might use this in the main form's module.

Code:
Private Sub EditMode(SubformControlName as String, Allow as Boolean)

   With Me.Controls(SubformControlName).Form
      .AllowEdits = Allow
      .AllowDeletions = Allow
      .AllowAdditions = Allow
   End With

End Sub
'_____________________

Private btnEditSomeSubform_Click()

   EditMode Me.SomeSubform.Name, True

End Sub
'______________________
 
Private btnEditAnotherSubform_Click()

   EditMode Me.AnotherSubform.Name, True

End Sub

Reuse can be extended right across the project by placing the code in a Standard Module and including an argument to pass the form name using Screen.ActiveForm

The name of the button that triggers the action can be passed by Screen.ActiveControl

Alternatively these values can also be read directly by the Sub.

This information can then be used to guide the actions performed by the sub using for example the button Name, Caption or Tag property.

The Caption can be changed by the procedure. In the above code example perhaps Edit could be changed to Lock.

Another trick is to store the alternative caption text in the Tag property. This can simply swap the Tag for the Caption but multiple captions can be stored in the Tag and parsed to extract the desired one for that button. This works well when a consistent cycle of captions is required for that button. The same Sub can be used to trigger the steps for any control without having to store the captions itself.

Using a Function adds more possibilities. The new Caption can be returned by looking up a table for example. The Function also allows the return of an error code to the calling line so it can substitute a failsafe caption rather than #Error. This is a good policy especially as the complexity of the reusable code increases.
 
Thanks Galaxiom.

I reuse code a lot. I do not like using macros and prefer VB (especically when having yes/no message boxes). This will go a long way to redusing the clutter in my code.
 
Whoa, thank you all for the help!!! I was sick the last few days and just got back to work.

Thanks! Works just fine now!
 

Users who are viewing this thread

Back
Top Bottom