new record event

R

Roland Alden

I have some controls in a form locked because changing them is uncommon (the
name fields in a record) and it's sort of easy to accidently edit them. I
have a little button the user can hit if they really want to be able to edit
them but this is off by default.

However, when the user hits the new record * in the navigation I want to
unlock these controls until the point where they save the record at which
point I'd like to go back to the locked state. I am guessing the After
Insert event would work for the latter but I'm not sure about the former.
Before Insert? On Load?
 
A

Arvin Meyer

Here's some code to put in a standard module to Lock all the controls. To
change all of them to unlocked, write a new function with ctl.Locked set to
False:

Public Sub LockControls(frm As Form)
On Error Resume Next
Dim ctl As Control

For Each ctl In frm.Controls
With ctl

Select Case .ControlType
Case acTextBox
ctl.Locked = True

Case acComboBox
ctl.Locked = True

Case acListBox
ctl.Locked = True

Case acCheckBox
ctl.Locked = True

Case acToggleButton
ctl.Locked = True

Case acSubform
ctl.Locked = True

Case acOptionGroup
ctl.Locked = True

Case acOptionButton
ctl.Locked = True

End Select
End With
Next ctl
Set ctl = Nothing
Set frm = Nothing

End Sub

Then use a bit of code like this in the form's Current event (aircode):

Sub Form_Current()

If Me.NewRecord = True Then
UnlockControls(Me)
Else
LockControls(Me)
End If

End Sub
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 
R

Roland Alden

Thanks very much. It was the Me.NewRecord field that I was searching for.
Perfect.

I was wondering about the case statement in the loop that visits every
control. I gather it is necessary to hit only certain types of controls?
What would happen if you just did this:

For Each ctl In frm.Controls
ctl.Locked = True
Next ctl


If I want to remember the preferred state of each control where is the best
place to store that? Is there am (easy) way to attach some data to each
control?
 
A

Arvin Meyer

Answers in line:

Roland Alden said:
Thanks very much. It was the Me.NewRecord field that I was searching for.
Perfect.

I was wondering about the case statement in the loop that visits every
control. I gather it is necessary to hit only certain types of controls?
What would happen if you just did this:

For Each ctl In frm.Controls
ctl.Locked = True
Next ctl

Some controls, like command buttons and tab controls don't have a locked
property. You could just ignore the error (like my code does) and go on, but
I also use a version of that code to disable controls and to read the Tag
property for different actions.
If I want to remember the preferred state of each control where is the best
place to store that? Is there am (easy) way to attach some data to each
control?

You can set the state in the property sheet and the form will open with
those values. Setting the recordsource of the form to a table, query, or sql
string, and then binding the controls to fields will attach data to each
control by records. To set "values" to accomplish certain actions, you can
use the TAG property.
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 
R

Roland Alden

Ah, the tag property. Just what I was looking for. Thank you.

Now, one other question. I see that subforms have a Locked property that is
apparently forced on the underlying form. This is nice because it means you
don't have to do a "deep walk" of the whole tree to lock all the controls.
However, I do like to set the background color of locked controls so the
user has a visual cue. I could probably put some general code in each
subform's current to walk the controls and color them or some such.

But my real question is why am I walking the top level form at all? Can I
just set the top level forms' Locked to true?

If a subform object on the top level has a Locked property is there some way
to access its state from the underlying subform?
 
A

Arvin Meyer

In Access, a subform is a control, like a textbox, until it has focus. Then
it becomes a form.

The locked property is a control property, not a form property. Controls
have a Parent property. The parent of a subform is a form. So you can
traverse up and down the levels, but you can only lock controls, not forms.
(I hope that makes some sense)

Forms can have their editing attributes disabled.
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top