Access - Disable controls when a form loads

I am looking to disable controls on a form once a toggle button control is pressed and also upon form load if a status field named ActionStatus is a 1 in the database. The default value of ActionStatus is 0.

This seems to work but once I change records the controls are disabled even if the Toggle button was not pressed. Also If I close Access and reopen, or close the form and reopen it the controls are enabled again.

Can someone let me know what I could be missing?
 Private Sub Form_Load()
If Me.Toggle54.Value = 1 Then
 Me.COST.Enabled = False
 Me.RootCause.Enabled = False
 Me.EstTime.Enabled = False
 Me.Option50.Enabled = False
 Me.Option52.Enabled = False
Else
 Me.COST.Enabled = True
 Me.RootCause.Enabled = True
 Me.EstTime.Enabled = True
 Me.Option50.Enabled = True
 Me.Option52.Enabled = True
End If
End Sub

Private Sub Toggle54_Click()
Me.ClosedBy = Environ("UserName")
Me.COST.Enabled = False
Me.RootCause.Enabled = False
Me.EstTime.Enabled = False
Me.Option50.Enabled = False
Me.Option52.Enabled = False
End Sub

Open in new window

kwoznica732Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hnasrCommented:
Upload a sample database showing the issue.
0
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPDesigner and DeveloperCommented:
I would move the code to the  On Current Record event. This way it fires with each record change. It also fires after the form load.
0
mbizupCommented:
This is a great reference for what you are doing:
http://office.microsoft.com/en-us/access-help/order-of-events-for-database-objects-HP005186761.aspx

Look specifically under the order of events for form records.

As theHiTechCoach has said, you want the code in the Current Event, which fires when the form opens and also with each record change.

Additionally, the code belongs in the click event of the button in question.

To avoid repeating chunks of code like this, I generally create a procedure in the form's code module:

Sub SetControls()
If Me.Toggle54.Value = 1 Then
 Me.COST.Enabled = False
 Me.RootCause.Enabled = False
 Me.EstTime.Enabled = False
 Me.Option50.Enabled = False
 Me.Option52.Enabled = False
Else
 Me.COST.Enabled = True
 Me.RootCause.Enabled = True
 Me.EstTime.Enabled = True
 Me.Option50.Enabled = True
 Me.Option52.Enabled = True
End If
End Sub

Open in new window

And call it from both places:
Private Sub Form_Current()
   SetControls
End Sub

Open in new window

Private Sub Toggle54_Click()
Me.ClosedBy = Environ("UserName")
SetControls
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPDesigner and DeveloperCommented:
I agree with  mbizup that you should place the code to enable/disable the controls in a sub so that it can be called from multiple places.

I prefer to make the sub generic and pass the value I want to use to set the enabled property. This also simplifies the code

Example:

Sub SetControls(pEnabled As Boolean)

 Me.COST.Enabled = pEnabled
 Me.RootCause.Enabled = pEnabled
 Me.EstTime.Enabled = pEnabled
 Me.Option50.Enabled = pEnabled
 Me.Option52.Enabled = pEnabled

End Sub

Open in new window


I would use something like this in the On Current Event

Private Sub Form_Current()

If Me.NewRecord then
   SetControls True

Else

   SetControls  (Nz(Me.Toggle54, 0) <> 1)

End if
End Sub

Open in new window



or

Private Sub Form_Current()

If Me.NewRecord then
   SetControls True

Else

   SetControls  Not (Nz(Me.Toggle54, 0) = 1)

End if
End Sub

Open in new window




if you do not want to pass the value then I would do something like this:

Sub SetControls()

 Me.COST.Enabled = Not (Me.Toggle54 = 1)
 Me.RootCause.Enabled = Not (Me.Toggle54 = 1)
 Me.EstTime.Enabled = Not (Me.Toggle54 = 1)
 Me.Option50.Enabled = Not (Me.Toggle54 = 1)
 Me.Option52.Enabled = Not (Me.Toggle54 = 1)

End Sub

Open in new window




Or

Sub SetControls()

 Me.COST.Enabled =  (Me.Toggle54 <> 1)
 Me.RootCause.Enabled = (Me.Toggle54 <> 1)
 Me.EstTime.Enabled = (Me.Toggle54 <> 1)
 Me.Option50.Enabled = (Me.Toggle54 <> 1)
 Me.Option52.Enabled = (Me.Toggle54 <> 1)

End Sub

Open in new window

0
hnasrCommented:
A simple database could save time from back and forth requests.

In load

     For Each ctrl In Me.Controls
        If ctrl.ControlType <> acLabel Then
            ctrl.Enabled = False
        End If
    Next
     Me.Controls("yourToggle").Enabled = True

Open in new window



In yourToggle, check for status

    For Each ctrl In Me.Controls
        If ctrl.ControlType <> acLabel Then
            ctrl.Enabled = False
        End If
    Next

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.