Create new records and then lock a field from further editing

S

Shelly

I am trying to create a new record and then lock a particular field from being edited. I believe I have to write a macro for this. Can anyone walk me through this? If a macro is required where is the macro inserted in the form properties.
 
S

Steve Schapel

Shelly,

You can use a macro using the SetValue action...

Action: SetValue
Item: [NameOfControl].[Enabled]
Expression: No
Action: SetValue
Item: [NameOfControl].[Locked]
Expression: Yes

.... and assign this on the BeforeInsert event of the form. And then
make another macro, the same except with the Yes and No reversed, on the
AfterInsert event.
 
S

Shelly

Thanks for the macro. I cannot get it to work, I must me entering the wrong information. Could you please confirm what I am to enter for the [NameOf Control]. I have been using the name of the field that I want to be locked after a new record has been created.
 
S

Steve Schapel

Shelly,

If the name of the textbox on your form is the same as the name of the
field in the table that it is bound to, then yes, I think what you are
doing is correct. If the textbox on the form, and the field in the
table, are named differently, then I think you need to use the name of
the control rather than the field. Is anything happening at all with
the macro, or something is happening but not working right, or you get
an error message? If you are using the AfterInsert event, as I
suggested, the "locking" will only apply to a new record, and won't
happen until after you move off that record on the form. Also, in the
design of the form, you will need to have the Enabled property of the
control set to No and the Locked set to Yes as your "starting point".
If none of this helps, try using the full control reference in the
macro, e.g.
[Forms]![NameOfYourForm]![NameOfControl]
 
S

Shelly

I went ahead and used the full control reference in the Macro. When I try and overwrite the data in the field I am trying to lock it allows it. When I go to create a new record and I enter data in the same field I get the error message "You can't disable a control while it has the focus".
 
S

Steve Schapel

Shelly,

The meaning of this error message is that the control we are talking
about has the focus at the time the macro is trying to change its
Enabled property. The easiest way around this is to put a GoToControl
action in the macro before the existing actions, to shift the focus to
any other control on the form.
 
S

Steve Schapel

Shelly,

It is not correct to usde the GoToControl action in this way. Just
remove them completely, I would say, or if it is important to control
where the cursor is, put it somewhere other than the control (job#)
whose Enabled property you are trying to change.

By the way, as a complete side issue, it is not a good idea to use a #
as part of the name of a field or control or database object.

--
Steve Schapel, Microsoft Access MVP

I do not know what I am doing wrong. My lack of knowledge in macros and expressions is not helping any. Below is what I have entered to set this up:

Before insert (macro):
Action: GoToControl; control name: job#
Action: SetValue; Item: [Forms]![Submittal Requirements]![Job#].[Enabled]; Expression: No
Action: GoToControl; control name: job#
Action: SetValue; Item: [Forms]![Submittal Requirements]![Job#].[Locked]; Expression: Yes

After insert (macro):
Action: GoToControl; control name: job#
Action: SetValue; Item: [Forms]![Submittal Requirements]![Job#].[Enabled]; Expression: Yes
Action: GoToControl; control name: job#
Action: SetValue; Item: [Forms]![Submittal Requirements]![Vender#].[Locked]; Express: No

With it setup like this I am still able to change the job# field after the record has been created and when I create a new record and I try to enter information in the job# field I get the error message "you can't disable a control while it has focus". Can you see what I am doing wrong?



----- Steve Schapel wrote: -----

Shelly,

The meaning of this error message is that the control we are talking
about has the focus at the time the macro is trying to change its
Enabled property. The easiest way around this is to put a GoToControl
action in the macro before the existing actions, to shift the focus to
any other control on the form.

--
Steve Schapel, Microsoft Access MVP
I went ahead and used the full control reference in the Macro. When
I try and overwrite the data in the field I am trying to lock it
allows it. When I go to create a new record and I enter data in the
same field I get the error message "You can't disable a control while
it has the focus".
 

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