Search code examples
ms-accessvbams-access-2010userform

Unable to disable all fields of a ms-access userform on load


I have lots of controls on my userform and I want to disable all of them on load except for "login button" and "Shift Date" textbox, so i am using the code below:

Private Sub Form_Load()
Call GetValues
Me.WlcmLabel.Caption = "Hi " + GetUserName + " ! "

Dim ctrl As Control

For Each ctrl In Me.Controls
    ctrl.Enabled = False
Next
Me.ShiftDate.Enabled = True
Me.LoginBtn.Enabled = True
Set ctrl = Nothing
End Sub 

but this gives me an error on load saying "object does not support this property or method."

all the controls will get enabled as soon as the user clicks on login button.

What would be the mistake in my code ?

Please ask if any other information required. Thank You !


Solution

  • Try locking the control instead. I have used the locked property below, but I use the tag property of each control to identify it as lockable. So the controls you don't want to lock will not get anything in the tag property, and therefore will not lock. For your application, you could flip the logic since you only want leave the two controls unlocked.

    For Each ctlCurr In Me.Controls
            If ctlCurr.Tag = "Lockable" Then
            ctlCurr.Locked = True
            End If
    Next