Question

I'm kinda new to Microsoft Access (first time using it), and I'd like to give it a try considering that it's like a mashup of Microsoft Excel and SQL Server, and I'm quite familiar with the latter. What I want to do is to have a field (e.g. Category) to be read-only after inserting a new row. This Category field is a lookup vlue, and a data macro will create a record in the corresponding child table with that particular category upon creation. And I don't want it to change after creation because that will result in a lot of trouble.

Is there any way I can do that in a Form? Any way of restricting the user? Or is there a workaround for this? Let me know, I'll consider all options, thank you very much!

Was it helpful?

Solution

Use the form's current event. That event fires each time a record becomes the active record. At that time, if the current record is the "new" record, enable the text box. But for existing records, disable the text box to prevent the user from changing its stored value.

So if the text box is named txtFoo, your Form_Current procedure could be something like this ...

Private Sub Form_Current()
    If Me.NewRecord = True Then
        ' allow edit
        Me.txtFoo.Enabled = True
    Else
        ' prevent edit
        Me.txtFoo.Enabled = False
    End If
End Sub

Or the same thing more concisely ...

Private Sub Form_Current()
    Me.txtFoo.Enabled = Me.NewRecord
End Sub

In addition to the Enabled property, also consider the Locked property. Either alone could do what you need. Or you can use both. Test the various combinations to see which you prefer.

OTHER TIPS

What would be the best/easiest way to restrict a user's access would depend on what aspects of the application you are giving them access to in the first place.

From your situation and description it seems like your macros are actually performing the updates/manipulation of the data tables and your user accesses that data via a form? For most applications this is the ideal technique because you can have the entire table display in a form but in the design options of the form you can customize the view and access the user has.

If you are using that technique where you are having your users access the data via a form gives you very easy ability to restrict user ability to manipulate the data of the tables without having to intervene using any VBA. If you have limited experience with Access I'd think that it might be easier for you to modify this access with the form options which you can change with the properties menu. This would also prevent you from having to modify your form to have the "has module" property.

To make a form field read only you would simply

  1. Open your form
  2. Switch to either Layout view or Design view (you can modify the properties in either)
  3. Open the properties sheet (via the Property Sheet button in the Design ribbon)
  4. Select the field you want to make read only
  5. In the Data tab of the Property Sheet there is a property called "Locked". Switch this property to "Yes"

That will make it so the user cannot change the field! (From that form)

Option two:

If you want your users to just view the table in the ordinary datasheet view (not in a form) you could have them open via a macro which uses "OpenTable" (database objects list) and set the Data Mode to "Read Only". IMPORTANT: This would make it so the user could not edit any fields!! If you need them to be able to edit the other fields you cannot use this option.

HansUp's solution: IMPORTANT to note (since you are a new user) To use this VBA you would need to make the following changes in the Property Sheet of that form:

  1. Select "Form" as the object
  2. Change the "HasModule" property (in the Other tab of the Property Sheet) to "Yes"

Furthermore I was not able to find an easy way to get to the Form Current property. An alternative would be to use the "On Got Focus" (in the Events tab) to set the property of that text box to "Disabled" via the VBA procedure he suggested.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top