shiv chhabra
shiv chhabra

Reputation: 39

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 !

Upvotes: 0

Views: 390

Answers (2)

Harshit kyal
Harshit kyal

Reputation: 350

You can't disable label control that's why that error is coming. You have to check the type of control.

 For Each ctrl In Me.Controls
    With ctrl
     a = TypeName(ctrl)
    Select Case .ControlType

        Case acLabel

        Case acEmptyCell

        Case Else
            ctrl.Enabled = False
    End Select
    End With


 Next ctrl

Apply disable only when it's not label.

Upvotes: 1

KenSquare
KenSquare

Reputation: 300

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

Upvotes: 1

Related Questions