r.sa
r.sa

Reputation: 67

Need to lock/disable button in access

I need to disable/lock two buttons. I have a form which has a subform which in turn has another subform. Two buttons in the third subform that I need to disable/lock depending on a condition in the OnCurrent() of the second subform. The third subform is locked subform3.locked=true. Everything in the third subform gets locked (txtboxes,comboboxes) except the two buttons i have added.

I have tried to place this code right below the code where the third subform is locked:
.subform.locked=true

Forms![subform2]![subform3].Form![buttonname].visible=false

But this has not worked. Also, in the OnLoad of subform3 tried to run an if condition that checks if subform3 is locked and if true make button.visible=false. This has not worked either.

Very new to access. Appreciate the help.

Upvotes: 2

Views: 15475

Answers (1)

Darren Bartrup-Cook
Darren Bartrup-Cook

Reputation: 19847

I find this site the best reference: http://access.mvps.org/access/forms/frm0031.htm

If your code is in the main form then:

Private Sub btnOnMainForm_Click()
    Me.btnToDisableOnMainForm.Enabled = False
    Me.SubForm1.Form.btnToDisableOnSubForm1.Enabled = False
    Me.SubForm1.Form.SubForm2.Form.btnToDisableOnSubForm2.Enabled = False
End Sub

If it's in the first subform then:

Private Sub btnOnSubForm1_Click()
    Me.btnToDisableOnSubForm1.Enabled = False
    Me.Parent.btnToDisableOnMainForm.Enabled = False
    Me.SubForm2.Form.btnToDisableOnSubForm2.Enabled = False
End Sub

If it's in the second subform then:

Private Sub btnOnSubForm2_Click()
    Me.Parent.Parent.btnToDisableOnMainForm.Enabled = False
    Me.Parent.btnToDisableOnSubForm1.Enabled = False
    Me.btnToDisableOnSubForm2.Enabled = False
End Sub

If it's not on the form then:

Sub LockControlsFromNormalModule()
    Forms.MainForm.btnToDisableOnMainForm.Enabled = False
    Forms.MainForm.SubForm1.Form.btnToDisableOnSubForm1.Enabled = False
    Forms.MainForm.SubForm1.Form.SubForm2.Form.btnToDisableOnSubForm2.Enabled = False
End Sub

To turn the enabled property on and off with the same code you can use:

Private Sub btnOnMainForm_Click()
    With Me
        With .btnToDisableOnMainForm
            .Enabled = Not .Enabled
        End With
        With .SubForm1.Form
            With .btnToDisableOnSubForm1
                .Enabled = Not .Enabled
            End With
            With .SubForm2.Form.btnToDisableOnSubForm2
                .Enabled = Not .Enabled
            End With
        End With
    End With
End Sub

Use the WITH command here is the same as writing:

Private Sub btnOnMainForm_Click()
    Me.btnToDisableOnMainForm.Enabled = Not Me.btnToDisableOnMainForm.Enabled
    Me.SubForm1.Form.btnToDisableOnSubForm1.Enabled = Not Me.SubForm1.Form.btnToDisableOnSubForm1.Enabled
    Me.SubForm1.Form.SubForm2.Form.btnToDisableOnSubForm2.Enabled = Not Me.SubForm1.Form.SubForm2.Form.btnToDisableOnSubForm2.Enabled
End Sub

Note - MainForm, SubForm1 and SubForm2 are the names I saved the forms as.
txtboxOnMainForm, txtboxOnSubForm1 and txtboxOnSubForm2 are the names I gave to the controls on the forms.

Upvotes: 1

Related Questions