A button question!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In the current event on my main form, some fields are locked and have a
particular back colour. I have created a button that unlocks the fields and
changes the backcolour to white so the fields are editable (this is so fields
are not changed by mistake).

I also have another button for creating a new record and I want do the same.
Although when I press the button it goes to a new record, I have to press
this button a second time to unlock the fields.
 
Hi

When you press the button to create the new record this record becomes the
current record thus firing the on current event thus running your code to
"lock" the fields.

In the on current event you will need to test if the record is new. If it
isn't then lock your fields as normal but if it is new then don't lock the
fields.

Use something like...

If Me.NewRecord = False Then...

to decide whether to lock the fields or not.


Regards

Andy Hull
 
It isn't working for me.

The code I have is

Private Sub Form_Current()

Me.FName.Locked = True
Me.Work.Locked = True
Me.WorkStat.Locked = True
Me.Dept.Locked = True
Me.Subdept.Locked = True
Me.TimeStat.Locked = True
Me.CHours.Locked = True
Me.FName.BackColor = 10079487
Me.WorkStat.BackColor = 10079487
Me.Dept.BackColor = 10079487
Me.Subdept.BackColor = 10079487
Me.TimeStat.BackColor = 10079487
Me.CHours.BackColor = 10079487

If Me.NewRecord = True Then

Me.FName.Locked = False
Me.WorkStat.Locked = False
Me.Dept.Locked = False
Me.Subdept.Locked = False
Me.TimeStat.Locked = False
Me.CHours.Locked = False
Me.FName.BackColor = 16777215
Me.WorkStat.BackColor = 16777215
Me.Dept.BackColor = 16777215
Me.Subdept.BackColor = 16777215
Me.TimeStat.BackColor = 16777215
Me.CHours.BackColor = 16777215

End If

End Sub
 
You could save a lot of code:
Private Sub Form_Current()
Dim blnSetLocks As Boolean
Dim lngColor As Long


With Me
blnSetLocks = Not .NewRecord
If blnSetLocks Then
lngColor = 10079487
Else
lngColor = 16777215
End If

.FName.Locked = blnSetLocks
.Work.Locked = blnSetLocks
.WorkStat.Locked = blnSetLocks
.Dept.Locked = blnSetLocks
.Subdept.Locked = blnSetLocks
.TimeStat.Locked = blnSetLocks
.CHours.Locked = blnSetLocks
.FName.BackColor = lngColor
.WorkStat.BackColor = lngColor
.Dept.BackColor = lngColor
.Subdept.BackColor = lngColor
.TimeStat.BackColor = lngColor
.CHours.BackColor = lngColor
End With

End Sub
 
Hi again

Very odd! It's working for me!

You could try only locking the fields if the record isn't new
(as opposed to locking them by default then unlocking them if they are new)

If me.newrecord = false then
<lock fields>
end if

Andy Hull
 
I am almost there.

If I click on the button it doesn't work but if I click on the new record
navigation button it does work but I would rather use the button.

In the design all the fields are unlocked and enabled, I inserted your code
into the form's current event and the new record button works. I am not sure
what could be missing.
 
Back
Top