Disable some controls on a form based on a condition

Amir 181 Reputation points
2021-09-13T19:36:28.193+00:00

I have a form that have several controls on it. I want to disable some of them when the user doesn't have complete authority to them. But when I run the code it says, "Object doesn't support this property or method". I don't know what my fault is. The code is in the open form event.

Private Sub Form_Open(Cancel As Integer)  
'get the information send from main form  
If Not IsNull(Me.OpenArgs) Then  
    Me.txtMainOrderID.DefaultValue = """" & Me.OpenArgs & """"  
End If  
  
'Control the access of the user to form  
  
Dim UserName As String  
UserName = TempVars!tempUserName  
  
If DLookup("frmLithoWorkOrder", "tblUser", "UserLogin='" & UserName & "'") = "Access" Then  
    'Do nothing  
ElseIf DLookup("frmLithoWorkOrder", "tblUser", "UserLogin='" & UserName & "'") = "Limited" Then  
    Me.AllowAdditions = False  
    Me.AllowDeletions = False  
    'Disable all controls on form except path file box and lithography notes  
Dim ctl As control  
  
    For Each ctl In Me.Controls  
        If ctl.Tag = "litho" Then  
            ctl.Enabled = True  
        Else  
            ctl.Enabled = False  
        End If  
      
    Next  
      
    MsgBox "ÔãÇ Èå ÞÓãÊí ÇÒ ÝÑã ÏÓÊÑÓí ÏÇÑíÏ."  
Else  
    MsgBox "ÔãÇ ãÌÇÒ Èå æÑæÏ Èå Çíä ÈÎÔ äãí ÈÇÔíÏ"  
    DoCmd.Close  
End If  
  
End Sub  

131712-001.jpg

Access Development
Access Development
Access: A family of Microsoft relational database management systems designed for ease of use.Development: The process of researching, productizing, and refining new or existing technologies.
859 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karl Donaubauer 1,726 Reputation points MVP
    2021-09-13T22:24:06.74+00:00

    Hi,

    The Else part of your For Each loop is executed for every control in the form that does not have "litho" in the tag. You probably have controls in the form without "Litho" that do not have an Enabled property, e.g. Labels. Hence the error.

    You can work around this e.g. by specifying the appropriate ControlTypes in the If condition in the loop. Have a look at the ControlType documentation and the "AcControlType" enumeration there.

    Servus
    Karl


    http://AccessDevCon.com
    https://www.donkarl.com


0 additional answers

Sort by: Most helpful