View Full Version : Locking lookup fields in form


rdev
05-19-2009, 08:35 AM
I have a table named tblShipmaster with fields
IMO_Num(Primarykey),Ship_Name,Vessel_type.
The Vessel_type field picks up value from a lookup field of a table called tblshipcategory
I have a related form to enter data in the tblShipmaster, the form is known as frmshipmaster.
After a few records are entered directly in the table . I go to the form frmshipmaster to enter few other records. However, in the form I can edit the field Vessel_type , this should not be possible for records I have already entered. when I am picking this field (Vessel_type) in other queries , I can edit the field for the related record. I should not be able to do this . How can I lock this

Thanks\
Dev

HiTechCoach
05-19-2009, 08:41 AM
When you open a table or query, you can edit data. If you want to be able to lock control from being edited, you should always use a form for data entry.

You can use a form's On Current event to lock control for existing records.

Simple example:


Private Sub Form_Current()


If Me.NewRecord Then
Me.txtMyControlName.Locked = False

Else
Me.txtMyControlName.Locked = True

End If

rdev
05-19-2009, 08:44 AM
Thanks
But when I open the form , I can edit the previously entered data, so a user can change my previously entered data. how can I stop this

rdev
05-19-2009, 08:52 AM
Thanks for the code , but how do I integrate it in the database, I really new and do not know how to handle macros currently

HiTechCoach
05-19-2009, 09:46 AM
If you want to lock a record, not jsut some controls then try:


Private Sub Form_Current()

If Me.NewRecord Then
Me.AllowEdits = True

Else
Me.AllowEdits = False

End If

End Sub



or



Private Sub Form_Current()

Me.AllowEdits = Me.NewRecord

End Sub




Both will work. The second version is just more concise and might be harder to read/understand.