Instead of disallowing edits on the whole form just set the locked property of the controls you don't want updated to true or set the disabled property to false.
I locked the fields that I don't want to be edited and selected allow edits for the form properties, but I can't enter a new values in the locked fields for a new record. the form is based on a query and I can add records through the query. any ideas? thanks
You've got conflicting requirements here. In your first sentence you say you want to disallow edits (which includes entering a new value) and then you want to enter a new value.
If you want to enter data initially (as a new record) and then prohibit the changing of the entered data once it has been saved then you will have to enter code in the "On Current" event which checks to see if the current record is a new record in which case the fields are unlocked else lock the required fields.
You don't need to open the recordset as it has already been opened by your forms scource query.
Put the following function in a module. It will also disable the "New Record" button on the record navigation bar at the bottom of your form. (If you have enabled it)
'==================================================
Function IsNewRecord()
'==================================================
'
' stops any form in edit mode from going
' to a blank record
'
' State is True for Enabled or False for Disabled
'
'==================================================
Dim RetVal
On Error Resume Next
RetVal = Screen.ActiveForm.Bookmark
If Err Then
IsNewRecord = True
DoCmd.GoToRecord , , A_LAST
Else
IsNewRecord = False
End If
End Function
===================================================
In the "On Current" event for your form use:
If IsNewRecord = True then
Me!employeeID.locked = false
Me!firstname.Locked = False
Me!lastname.Locked = False
End If
Sorry I couldn't get back to this until this morning and thanks to Tony for picking up the slack. A couple of things I would like to add. I think you need to relock the text boxes if an existing record is accessed, like this:
If IsNewRecord = True Then
Me!employeeID.locked = Flse
Me!firstname.Locked = False
Me!lastname.Locked = False
Else
Me!employeeID.locked = True
Me!firstname.Locked = True
Me!lastname.Locked = True
End If
Secondly, since employeeID looks like a primary key you might be able to use simpler code like this:
If IsNull(Me!employeeID) = True Then
Me!employeeID.locked = Flse
Me!firstname.Locked = False
Me!lastname.Locked = False
Else
Me!employeeID.locked = True
Me!firstname.Locked = True
Me!lastname.Locked = True
End If
Again, this would be in the Form_Current event procedure.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.