Solved

Trying to lock certain controls by calling function

Posted on 2016-11-14
7
56 Views
Last Modified: 2016-11-14
In the oncurrent event of a form I am trying to lock certain controls depending on the users security level while leaving others unlocked.

I have this in the oncurrent event:

Private Sub Form_Current()
    DisableControls
End Sub

Open in new window

and this in a private sub:

Private Sub DisableControls()
On Error GoTo Err_DisableControls

    If DLookup("[SecurityLevel]", "LOCALtblCurrentUser") > 2 Then

        Dim ctl As Control

        For Each ctl In Me.Controls

            Select Case ctl.ControlType
                Case acComboBox
                    If ctl.Name <> "cboFindRecord" Or ctl.Name <> "cboPartN" Then ctl.Locked = True
                Case acCommandButton
                    If ctl.Name <> "cmdMainMenu" Or ctl.Name <> "cmdGridView" Then ctl.Locked = True
                Case acTextBox
                    If ctl.Name <> "txtCompanyID" Then ctl.Locked = True
            End Select

        Next

    End If

Exit_DisableControls:
    Exit Sub

Err_DisableControls:
    MsgBox "Error Number: " & Err.Number & vbCrLf & "Error Description: " & Err.Description & vbCrLf & "Error Source: " & Err.Source
    Resume Exit_DisableControls

End Sub

But when the form opens I get an error...

Error #438.  Object doesn't support this property or method.

Open in new window

0
Comment
Question by:SteveL13
  • 4
  • 3
7 Comments
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 41886443
try this revision


Dim ctl As Control
        For Each ctl In Me.Controls

            Select Case ctl.ControlType
                Case acComboBox
                    If ctl.Name = "cboFindRecord" Or ctl.Name = "cboPartN" Then
                    ctl.Locked = False
                    Else
                    ctl.Locked = True
                    End If
                Case acCommandButton
                    If ctl.Name = "cmdMainMenu" Or ctl.Name = "cmdGridView" Then
                    ctl.Locked = False
                    Else
                    ctl.Locked = True
                    End If

                Case acTextBox
                    If ctl.Name = "txtCompanyID" Then
                    ctl.Locked = False
                    Else
                    ctl.Locked = True
                    End If
            End Select

        Next

Open in new window

0
 

Author Comment

by:SteveL13
ID: 41886470
Same error message.  All I did was add

If DLookup("[SecurityLevel]", "LOCALtblCurrentUser") > 2 Then

and end if at the end of the code.

And if I comment those 2 lines out I still get the error.
0
 

Author Comment

by:SteveL13
ID: 41886475
And it only give me an error if DLookup("[SecurityLevel]", "LOCALtblCurrentUser") > 2
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 119

Expert Comment

by:Rey Obrero
ID: 41886484
what are the fields in table "LOCALtblCurrentUser" ?

you need to add a criteria to your dlookup()

If DLookup("[SecurityLevel]", "LOCALtblCurrentUser","UserName = '" & user & "'") > 2 Then
0
 

Author Comment

by:SteveL13
ID: 41886498
I got it with:

    Dim ctl As Control
    
    If DLookup("[SecurityLevel]", "LOCALtblCurrentUser") = 3 Then
    
        For Each ctl In Me.Controls

            Select Case ctl.ControlType
                Case acComboBox
                    If ctl.Name = "cboFindRecord" Or ctl.Name = "cboPartN" Then
                    ctl.Locked = False
'                    Else
'                    ctl.Locked = True
                    End If
                Case acCommandButton
                    If ctl.Name = "cmdMainMenu" Or ctl.Name = "cmdGridView" Then
                    ctl.Enabled = True
                    Else
                    ctl.Enabled = False
                    End If
                Case acTextBox
                    If ctl.Name <> "txtCompanyID" Then
                    ctl.Locked = True
                    Else
                    ctl.Locked = False
                    End If
            End Select

        Next
        
    End If

Open in new window


It seems some control can be locked, other enabled perhaps?
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 41886501
oh yeah command buttons can not be locked, just enable/ disable.
0
 

Author Comment

by:SteveL13
ID: 41886503
You definitely led me in the right direction.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Encryption for Business Encryption (https://en.wikipedia.org/wiki/Encryption) ensures the safety of our data when sending emails. In most cases, to read an encrypted email you must enter a secret key that will enable you to decrypt the email. T…
In 2017, ransomware will become so virulent and widespread that if you aren’t a victim yourself, you will know someone who is.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now