Protecting Fields

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

Guest

I have a field that I want to protect from modification but I still need a
new record to be added. The field is the reference number for the record.
 
aMack,

Are you setting this field programmatically or is the user able to enter the
value for a new record? If it's the former, simply set the control's Enabled
property to False in form design view. If the latter, use the OnCurrent
event and the NewRecord property of the Form object to determine if you're at
a new record, and set the Enabled property accordingly:

' Form's OnCurrent event procedure code
Dim intNewRec as Integer
intNewRec = Me.NewRecord
If intNewRec Then
Me![YourControl].Enabled = True
Else
Me![YourControl].Enabled = False
End If

Hope that helps.

Sprinks
 
In the Current event of the form, you could set the Locked property of the
control, based on the NewRecord property of the form:

Private Sub Form_Current()
If Me.[NameOfYourFieldHere].Locked = Me.NewRecord Then
Me.[NameOfYourFieldHere].Locked = Not Me.NewRecord
End If
End Sub
 
I appreciate your assistance in this matter.

The field is added through a macro/query combination.

As I am a new user your instructions are well above me head. Could you be a
little more specific with regards to where I would "Set the control's enabled
property to False."

If this means setting the fields' text box "Enabled" to "NO" - this will
protect it but then not allow a new record.

Thanks again for your help.
--
A MACKENZIE, CMA, MBA


Sprinks said:
aMack,

Are you setting this field programmatically or is the user able to enter the
value for a new record? If it's the former, simply set the control's Enabled
property to False in form design view. If the latter, use the OnCurrent
event and the NewRecord property of the Form object to determine if you're at
a new record, and set the Enabled property accordingly:

' Form's OnCurrent event procedure code
Dim intNewRec as Integer
intNewRec = Me.NewRecord
If intNewRec Then
Me![YourControl].Enabled = True
Else
Me![YourControl].Enabled = False
End If

Hope that helps.

Sprinks


aMack said:
I have a field that I want to protect from modification but I still need a
new record to be added. The field is the reference number for the record.
 
aMack,

See my response below.

aMack said:
I appreciate your assistance in this matter.

The field is added through a macro/query combination.

As I am a new user your instructions are well above me head. Could you be a
little more specific with regards to where I would "Set the control's enabled
property to False."

If this means setting the fields' text box "Enabled" to "NO" - this will
protect it but then not allow a new record.

Yes, that's why I suggest you test for whether it is a new record or not.
As you didn't understand my response, forgive me if I cover basics you
already understand--I just want to make sure you can implement the solution.

Access is event-driven, that is, there are a number of events that occur at
which point you can, by defining an event procedure, cause code to be
executed. Events occur both for forms, and for controls on the forms. For
example, the OnCurrent event occurs whenever the focus is placed on a
different record. Events for controls include the OnGotFocus event, the
AfterUpdate event, etc.

What I was suggesting was to write an event procedure for the OnCurrent
event of the form, which would test whether the record you were on was a new
record or an existing one. If it were a new record, it would change the
Enabled property programmatically to allow the user to enter a value. If it
were an existing one, it would disable the control to prevent the user from
changing it.

To enter an OnCurrent event procedure, load the form in design view, and
select View, Properties from the menu. Choose the Event tab; you should be
looking at the events available for the form, confirmed by "Form" on the
window header. If not, click the small box at the top left corner of the
design view window to show the form properties.

Click into the OnCurrent field, and select the ellipsis that appears to the
right of the field. Select Code Builder if given the choice (depending on
your configuration, Code Builder may launch automatically). Access will
create the shell of an OnCurrent event procedure for you:

Private Sub Form_Current()

End Sub

Between the Sub and End Sub lines, enter the code previously given, leaving
you with:

Private Sub Form_Current()
Dim intNewRec as Integer
intNewRec = Me.NewRecord
If intNewRec Then
Me![YourControl].Enabled = True
Else
Me![YourControl].Enabled = False
End If
End Sub

Change "YourControl" between the brackets to the name of your control.

Walking through the code, Enabled can be either True or False, corresponding
to an integer value of -1 or 0. The first line assigns the value of the
NewRecord property of the current form (whether you're on a new record or
not) to an integer variable. The next line tests whether the value is True
(-1), if so, it sets the Enabled property of your control to True, otherwise,
it sets it to False.

Hope that helps.

Sprinks
Thanks again for your help.
--
A MACKENZIE, CMA, MBA


Sprinks said:
aMack,

Are you setting this field programmatically or is the user able to enter the
value for a new record? If it's the former, simply set the control's Enabled
property to False in form design view. If the latter, use the OnCurrent
event and the NewRecord property of the Form object to determine if you're at
a new record, and set the Enabled property accordingly:

' Form's OnCurrent event procedure code
Dim intNewRec as Integer
intNewRec = Me.NewRecord
If intNewRec Then
Me![YourControl].Enabled = True
Else
Me![YourControl].Enabled = False
End If

Hope that helps.

Sprinks


aMack said:
I have a field that I want to protect from modification but I still need a
new record to be added. The field is the reference number for the record.
 
Back
Top