Locking Fields

G

Guest

I have a form which I want to lock all the fields unless the Edit button is
clicked. I didn't want to use the onload event on the form because then it
will only apply when it loads the form. I used the following code for the
Forms AfterUpdate Event:

Private Sub Form_AfterUpdate()
Dim c As Control
On Error Resume Next
For Each c In Me.Controls
c.Locked = True
Next
End Sub

and the following for the Edit Button Click Event:

Private Sub Edit_Record_Click()
Dim c As Control
On Error Resume Next
For Each c In Me.Controls
c.Locked = False
Next
End Sub

The problem I'm having is that when I first open the form and add a new
record, then click the add new record button I can't just start typing, I
first have to click on the edit button and then start typing into the fileds.
Is there a way to fix that? Also, if I click on the edit button and I don't
edit the record then click the next button, it doesn't end the command until
I change a record.

Thank you.
 
A

Allen Browne

Could you get away with just setting the form's AllowEdits property to No,
while leaving AddAdditions as Yes?

That may not be enough if you have unbound controls you want to edit,
subforms that need to be set too, or selected controls that should not be
unlocked. For those cases, see:
Locking bound controls on a form and subforms.
at:
http://allenbrowne.com/ser-56.html
 
I

Ian King

You could try me.allowedits = me.newrecord on the load event. Your edit
button will then be me.allowedits = true.

Ian King
 
G

Guest

If I set the AllowEdits property to No, my edit button doesn't work at all.
I followed step by step the instructions from the web page you suggested and
I'm getting a compile error saying:
Expected: Line number or label or statement or end of statement

It's giving me the error on this statement

Private Sub Form_Load()
=LockBoundControls([Form],True)
End Sub

Thanks.
 
A

Allen Browne

Put the statement into the On Load property.
If you want to use a line in the event procedure instead, it would be:
Call LockBoundControls(Me, True)

If you used a command button (not a toggle button), it should be possible to
click it even if the form's AllowEdits property is No.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Mya48 said:
If I set the AllowEdits property to No, my edit button doesn't work at
all.
I followed step by step the instructions from the web page you suggested
and
I'm getting a compile error saying:
Expected: Line number or label or statement or end of statement

It's giving me the error on this statement

Private Sub Form_Load()
=LockBoundControls([Form],True)
End Sub

Thanks.

Allen Browne said:
Could you get away with just setting the form's AllowEdits property to
No,
while leaving AddAdditions as Yes?

That may not be enough if you have unbound controls you want to edit,
subforms that need to be set too, or selected controls that should not be
unlocked. For those cases, see:
Locking bound controls on a form and subforms.
at:
http://allenbrowne.com/ser-56.html
 
G

Guest

Well that solves the locking fileds but the following is still happening.

The problem I'm having is that when I first open the form and add a new
record, then click the next button to add another new record I can't just
start typing again, I have to click on the edit command button and then start
typing into the fields. Is there a way to fix that?
Also, if I click on the edit command button and I don't edit the record,
then click the next button, it doesn't end the command until I change a
record.

Thanks.


Allen Browne said:
Put the statement into the On Load property.
If you want to use a line in the event procedure instead, it would be:
Call LockBoundControls(Me, True)

If you used a command button (not a toggle button), it should be possible to
click it even if the form's AllowEdits property is No.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Mya48 said:
If I set the AllowEdits property to No, my edit button doesn't work at
all.
I followed step by step the instructions from the web page you suggested
and
I'm getting a compile error saying:
Expected: Line number or label or statement or end of statement

It's giving me the error on this statement

Private Sub Form_Load()
=LockBoundControls([Form],True)
End Sub

Thanks.

Allen Browne said:
Could you get away with just setting the form's AllowEdits property to
No,
while leaving AddAdditions as Yes?

That may not be enough if you have unbound controls you want to edit,
subforms that need to be set too, or selected controls that should not be
unlocked. For those cases, see:
Locking bound controls on a form and subforms.
at:
http://allenbrowne.com/ser-56.html

I have a form which I want to lock all the fields unless the Edit button
is
clicked. I didn't want to use the onload event on the form because then
it
will only apply when it loads the form. I used the following code for
the
Forms AfterUpdate Event:

Private Sub Form_AfterUpdate()
Dim c As Control
On Error Resume Next
For Each c In Me.Controls
c.Locked = True
Next
End Sub

and the following for the Edit Button Click Event:

Private Sub Edit_Record_Click()
Dim c As Control
On Error Resume Next
For Each c In Me.Controls
c.Locked = False
Next
End Sub

The problem I'm having is that when I first open the form and add a new
record, then click the add new record button I can't just start typing,
I
first have to click on the edit button and then start typing into the
fileds.
Is there a way to fix that? Also, if I click on the edit button and I
don't
edit the record then click the next button, it doesn't end the command
until
I change a record.
 
G

Guest

Can anyone help? I've got everything else working except when I am entering
records. I can enter one because the form opens up on a new record but when
I click the next record button I can't start typing in a field without having
to click on the edit button. I've used the code below. This is driving me
crazy.

Thanks.
 
G

Guest

Mya,
I use the same type of code for one of my forms and all I did was add a
command button to the form to create a new record. When you click the command
button to create a new record have this code in it:

DoCmd.GoToRecord , , acNewRec
Call LockBoundControls([Form], False)

Anytime I open my form I have it go to the last record. So when I click on
the new record button it creates a new record and unlocks all controls.

HTH
SS
 
G

Guest

Hello

this thread is great. i have used this to allow me to create a form that
uses all of the features of your code and to allow a new record button to
turn off the locked feature.

how would i make changes that allow me to store the locked or unlocked state
per record and change it in the code accordingly. i have a field in my table
that stores the locked or unlocked state but currently doesnt do a thing
because this is all form and code based at the moment. what i want to do is
to make the state change to allow me to cause no editing to be allowed on a
per record basis, but to allow me to change the locked state so that i can
allow editing if necessary.

the problem is this, i added the code that secret squirel suggested to my
"add new record button" that i have on my form.

Call LockBoundControls([Form], False)

this turned off the lock, but leaves it off for all records after i use the
button. is there a way to turn it back on when i got to previous records? or
to records that have been entered?

i think i just thought of it. i should put the same code in my other buttons
but make it true...

if you have another suggestion or better idea let me know here, i will be
trying my solution. thanks!
 
G

Guest

Dawn,
I would try adding this code to the On Current event of your form;

Call LockBoundControls([Form], True)

This way after you add a new record and then scroll to a previous record it
will lock the form up again.The "On Current" event code will fire every time
you move to another record.

As for having a way to toggle back and forth between locking and unlocking
for record updates I would just add a command button to your form that will
lock/unlock your record for editing. Use this code provided by Allen Browne's
tip.

Dim bLock As Boolean
bLock = IIf(Me.cmdLock.Caption = "&Lock", True, False)
Call LockBoundControls(Me, bLock)


DawnTreader said:
Hello

this thread is great. i have used this to allow me to create a form that
uses all of the features of your code and to allow a new record button to
turn off the locked feature.

how would i make changes that allow me to store the locked or unlocked state
per record and change it in the code accordingly. i have a field in my table
that stores the locked or unlocked state but currently doesnt do a thing
because this is all form and code based at the moment. what i want to do is
to make the state change to allow me to cause no editing to be allowed on a
per record basis, but to allow me to change the locked state so that i can
allow editing if necessary.

the problem is this, i added the code that secret squirel suggested to my
"add new record button" that i have on my form.

Call LockBoundControls([Form], False)

this turned off the lock, but leaves it off for all records after i use the
button. is there a way to turn it back on when i got to previous records? or
to records that have been entered?

i think i just thought of it. i should put the same code in my other buttons
but make it true...

if you have another suggestion or better idea let me know here, i will be
trying my solution. thanks!

Allen Browne said:
Could you get away with just setting the form's AllowEdits property to No,
while leaving AddAdditions as Yes?

That may not be enough if you have unbound controls you want to edit,
subforms that need to be set too, or selected controls that should not be
unlocked. For those cases, see:
Locking bound controls on a form and subforms.
at:
http://allenbrowne.com/ser-56.html
 
G

Guest

Hello

ah, that might have been simpler than my solution. i went through each of my
buttons and made them do "Call LockBoundControls([Form], True)" and that
solved my problem. but yours would do it in one fell swoop instead of 5
different buttons i had to do it to. thanks!


Secret Squirrel said:
Dawn,
I would try adding this code to the On Current event of your form;

Call LockBoundControls([Form], True)

This way after you add a new record and then scroll to a previous record it
will lock the form up again.The "On Current" event code will fire every time
you move to another record.

As for having a way to toggle back and forth between locking and unlocking
for record updates I would just add a command button to your form that will
lock/unlock your record for editing. Use this code provided by Allen Browne's
tip.

Dim bLock As Boolean
bLock = IIf(Me.cmdLock.Caption = "&Lock", True, False)
Call LockBoundControls(Me, bLock)


DawnTreader said:
Hello

this thread is great. i have used this to allow me to create a form that
uses all of the features of your code and to allow a new record button to
turn off the locked feature.

how would i make changes that allow me to store the locked or unlocked state
per record and change it in the code accordingly. i have a field in my table
that stores the locked or unlocked state but currently doesnt do a thing
because this is all form and code based at the moment. what i want to do is
to make the state change to allow me to cause no editing to be allowed on a
per record basis, but to allow me to change the locked state so that i can
allow editing if necessary.

the problem is this, i added the code that secret squirel suggested to my
"add new record button" that i have on my form.

Call LockBoundControls([Form], False)

this turned off the lock, but leaves it off for all records after i use the
button. is there a way to turn it back on when i got to previous records? or
to records that have been entered?

i think i just thought of it. i should put the same code in my other buttons
but make it true...

if you have another suggestion or better idea let me know here, i will be
trying my solution. thanks!

Allen Browne said:
Could you get away with just setting the form's AllowEdits property to No,
while leaving AddAdditions as Yes?

That may not be enough if you have unbound controls you want to edit,
subforms that need to be set too, or selected controls that should not be
unlocked. For those cases, see:
Locking bound controls on a form and subforms.
at:
http://allenbrowne.com/ser-56.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

I have a form which I want to lock all the fields unless the Edit button is
clicked. I didn't want to use the onload event on the form because then it
will only apply when it loads the form. I used the following code for the
Forms AfterUpdate Event:

Private Sub Form_AfterUpdate()
Dim c As Control
On Error Resume Next
For Each c In Me.Controls
c.Locked = True
Next
End Sub

and the following for the Edit Button Click Event:

Private Sub Edit_Record_Click()
Dim c As Control
On Error Resume Next
For Each c In Me.Controls
c.Locked = False
Next
End Sub

The problem I'm having is that when I first open the form and add a new
record, then click the add new record button I can't just start typing, I
first have to click on the edit button and then start typing into the
fileds.
Is there a way to fix that? Also, if I click on the edit button and I
don't
edit the record then click the next button, it doesn't end the command
until
I change a record.

Thank you.
 
G

Guest

Glad I could help you out!

DawnTreader said:
Hello

ah, that might have been simpler than my solution. i went through each of my
buttons and made them do "Call LockBoundControls([Form], True)" and that
solved my problem. but yours would do it in one fell swoop instead of 5
different buttons i had to do it to. thanks!


Secret Squirrel said:
Dawn,
I would try adding this code to the On Current event of your form;

Call LockBoundControls([Form], True)

This way after you add a new record and then scroll to a previous record it
will lock the form up again.The "On Current" event code will fire every time
you move to another record.

As for having a way to toggle back and forth between locking and unlocking
for record updates I would just add a command button to your form that will
lock/unlock your record for editing. Use this code provided by Allen Browne's
tip.

Dim bLock As Boolean
bLock = IIf(Me.cmdLock.Caption = "&Lock", True, False)
Call LockBoundControls(Me, bLock)


DawnTreader said:
Hello

this thread is great. i have used this to allow me to create a form that
uses all of the features of your code and to allow a new record button to
turn off the locked feature.

how would i make changes that allow me to store the locked or unlocked state
per record and change it in the code accordingly. i have a field in my table
that stores the locked or unlocked state but currently doesnt do a thing
because this is all form and code based at the moment. what i want to do is
to make the state change to allow me to cause no editing to be allowed on a
per record basis, but to allow me to change the locked state so that i can
allow editing if necessary.

the problem is this, i added the code that secret squirel suggested to my
"add new record button" that i have on my form.

Call LockBoundControls([Form], False)

this turned off the lock, but leaves it off for all records after i use the
button. is there a way to turn it back on when i got to previous records? or
to records that have been entered?

i think i just thought of it. i should put the same code in my other buttons
but make it true...

if you have another suggestion or better idea let me know here, i will be
trying my solution. thanks!

:

Could you get away with just setting the form's AllowEdits property to No,
while leaving AddAdditions as Yes?

That may not be enough if you have unbound controls you want to edit,
subforms that need to be set too, or selected controls that should not be
unlocked. For those cases, see:
Locking bound controls on a form and subforms.
at:
http://allenbrowne.com/ser-56.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

I have a form which I want to lock all the fields unless the Edit button is
clicked. I didn't want to use the onload event on the form because then it
will only apply when it loads the form. I used the following code for the
Forms AfterUpdate Event:

Private Sub Form_AfterUpdate()
Dim c As Control
On Error Resume Next
For Each c In Me.Controls
c.Locked = True
Next
End Sub

and the following for the Edit Button Click Event:

Private Sub Edit_Record_Click()
Dim c As Control
On Error Resume Next
For Each c In Me.Controls
c.Locked = False
Next
End Sub

The problem I'm having is that when I first open the form and add a new
record, then click the add new record button I can't just start typing, I
first have to click on the edit button and then start typing into the
fileds.
Is there a way to fix that? Also, if I click on the edit button and I
don't
edit the record then click the next button, it doesn't end the command
until
I change a record.

Thank you.
 
G

Guest

Hello

i have a question related to this discussion.

how can i get the code to change the word "Unlock" to a red color text?

i found the word in the code, should i just highlight it and format it red?

Thanks!
 
A

Allen Browne

This will be another line of code, to set the ForeColor property of the text
box to vbRed.
 
G

Guest

Hello

ok, but i only want the word "unlock" to be red. i like the lock staying
black. if i change the ForeColor to red wont that do it for both "states" of
the button?
 

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