Enabling/disabling input into certain fields based on the value of another field

匆匆过客 提交于 2019-12-23 04:54:26

问题


In the data entry form frmlastweeksactivities . When a row has "Gold"or "Silver" or "Bronze" as content in the [“category of activity “] field /column then disable columns/fields[5] & [6]

When a row has "Wood" or "Hay"or "Stubble "as content in the [“category of activity “] field /column then disable columns/fields[8] & [9]

Disable means not allow any data entry in this case turn them red as well would be nice

I am trying to build and expression to use in either the table validation rule properties or in one of the control -txt boxes in the form


回答1:


EDIT: Solution using table-level validation

The table-level solution will not disable fields, all it will do is validate a record as it is added. If the validation rule fails (evaluates to FALSE) then the user will be presented with a warning message and the update will be cancelled (record not added).

The expression for this should be entered in the Validation Rule Table Property as follows:

IIf((([category]="gold" Or [category]="silver" Or [category]="bronze") And (Len([Field5])>0 Or Len([Field6])>0)) 
 Or (([category]="Wood" Or [category]="stubble" Or [category]="hay") And (Len([Field8])>0 Or Len([Field9])>0))
,False,True)

WARNING: MAKE SURE THE BRACKETS LINE UP AS PER THE EXPRESSION ABOVE!!

The expression is messy but it tests whether either of 2 "fail conditions" you describe are true, namely:

1) Category = gold/silver/bronze AND not null value in Field5/Field6

2) Category = wood/stubble/hay AND not null value in Field8/Field9

Then in the Validation Text property of the Table Property Sheet enter the text you wish to notify the user that their record fails and needs correction before the record can be saved. For example:

"If category is gold/silver/bronze then Field5 and Field6 must be blank, if wood/stubble/hay then Field8 and Field9 blank.  Record not saved, please correct before saving."

Here is a screen shot of the table in design view showing the Property Sheet and the 2 properties you need to change.


BETTER: Solution using event-driven validation & control locking within the Form

Given that Access table-level and field-level validation will not do what is required, this solution will utilise event-driven validation on the form. It assumes that you have a form with text box controls called category, Field5, Field6, Field8 and Field9.

With the form in design view, right click on the category field (or press ALT and ENTER simultaneously) to view the properties for the control. In the Property Sheet on the right of the screen click on the Events tab. This displays the possible events for the selected control. See the screenshot below.

You are interested in the After Update event so click in it and then the buttton to the right .... Choosing Code Builder will open up a new VBA module for the form and create an event automatically for you.

Private Sub category_AfterUpdate()

    ' by default enable all four fields 5/6/8/9
    Me.Field5.Enabled = True
    Me.Field6.Enabled = True
    Me.Field8.Enabled = True
    Me.Field9.Enabled = True

    ' test the value entered by user in the category field and hide fields as required
    Select Case Me.category
        Case "Gold", "Silver", "Bronze"
            ' if the user has entered Gold, Silver or Bronze lock Fields 5/6
            Me.Field5.Enabled = False
            Me.Field6.Enabled = False
        Case "Wood", "Stubble", "Hay"
            ' if the user has entered Gold, Silver or Bronze lock Fields 8/9
            Me.Field8.Enabled = False
            Me.Field9.Enabled = False
    End Select

End Sub

So the code above will ensure that when the user updates the category field the code between Sub and End Sub will run and this code sets the Enabled property for the fields you mentioned as per your logic. This Enabled property if set to False will lock the field from becoming active (accepting the cursor) and will grey it out so users are aware it is disabled.

Finally, when the user moves onto a new record you need to add one more piece of code, to reset the 4 fields to being Enabled. So back in the form in design view, access the properties for the Form object itself either by clicking on a part of the form but NOT on a control. For example click in the form header or footer. When you see the event properties for the form you will see an event named Current. This is the one you need to create an event procedure for in much the same way as above with the ... button. This will take you into the VBA window again and you need to make sure that the following code is entered there:

Private Sub Form_Current()

    ' by default enable all four fields 5/6/8/9
    Me.Field5.Enabled = True
    Me.Field6.Enabled = True
    Me.Field8.Enabled = True
    Me.Field9.Enabled = True

End Sub

This is a screenshot of how the VBA screen should look with all of your code in it.

When you now save and run your form it should behave exactly as you wish. Any questions or issues please comment and I'll try to assist.



来源:https://stackoverflow.com/questions/20923650/enabling-disabling-input-into-certain-fields-based-on-the-value-of-another-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!