Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

Make field on a sub-form not enabled if checkox on main form is false

I have a form with a sub-form.  If a checkbox on the main form is false then I want a text box on the sub-form to NOT be enabled.

I am trying this but get an error...  "Application-defined or object-defined error"

The main form is frmQuoteOrderDetail
The sub-form is subfrmOrderDetailsSizes
The checkbox on the main form is chkbxYSqty
The textbox on the sub-form is txtYSqty

My code so far:

    If Forms!frmQuoteOrderDetail.Form!subfrmOrderDetailsSizes.Form.chkbxYSqty = False Then
        Me.txtYSqty.Enabled = False
    End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Paul Cook-Giles
Paul Cook-Giles
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'll just add that the Me keyword is optional, a form know the controls it hold without needing to refer to itself:
If chkbxYSqty = False Then
   subfrmOrderDetailsSizes.Form.txtYSqty.Enabled = False
Else
   subfrmOrderDetailsSizes.Form.txtYSqty.Enabled = True
End If

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As I already explained, Gus' solution will work ONLY if chkbxYSqty is updated.  If you scroll to an existing record, this code won't run.  That may be fine with you but I'm just pointing out the potential issue.

Also, updating the subform from the mainform may cause an unintended consequence.  I haven't tested it but it seems like moving focus to the subform to post a value in a control will cause Access to save the mainform record.  Depending on your validation code, this may raise strange errors if  there are required fields that follow the check box in the tab order which are not yet filled when the suggested code runs.
As I stated in my original response:

"You would need to put this in the Form_Current event as well as in the checkboxes Checked event. "

Dale
Avatar of SteveL13

ASKER

I got it with: (in the on current event of the form, "frmQuoteOrderDetail"

    If Forms!frmQuoteOrderDetail.subfrmOrderDetailsSizes.Form.chkbxYS = False Then
        Me.txtYSqty.Enabled = False
        Else
        Me.txtYSqty.Enabled = True
    End If

Open in new window

If a checkbox on the main form is false then I want a text box on the sub-form to NOT be enabled.

That's what you asked. It doesn't correlate with what you have "got it with" ...
I think I mixed up the question originally.  My mistake.
Dale,
Let me get this right.  You are proposing code in two events and are willing to take the potentially adverse consequences of forcing Access to save a record before it is completed by moving focus to the subform rather than moving the code to a single event in the subform?

Steve13,
OK, putting the code in the main form's current event solved the problem I told you that you would have for existing records.  What happens if you change the checkbox?  If you don't have code ALSO in the click event, the subform won't change.  Put  the code in a dozen events if you want or put it in one.
The checkboxes are locked and the user can't check/uncheck them.
OK, then the Current event of the main form or the Current event of the subform will both work.  I would still use the subform's Current event because that way if the main form is later changed to allow the user to update the flag, then no one will have to remember to change the code.  But that's my style - defensive.  When it is 6 of 1, half dozen of the other, I try to choose the safest option or the option that will provide the most flexibility in the future..  You also never said whether the user can change other items on the main form.