Hey there, I had gone to sleep. There were several typos in the code I posted, that is because the OCR program (Capture2Text) read the text like that and I didn't check, but this version should have the typos fixed and I also had forgotten about closing the With instruction, My bad!
Code:
Function UserPermission(frm As Form)
Dim permissionNr As Long
permissionNr = DLookup("Uspele_IDb", "q5LoggedInUser")
With frm
Select Case permissionNr
Case 58
.AllowEdits = False
.AllowDeletions = False
.btn16DeleteOne.Enabled = False
.btn05Programming.Enabled = False
Case 57
.AllowEdits = True
.AllowDeletions = True
.btn16DeleteOne.Enabled = True
.btn05Programming.Enabled = False
Case 56
.AllowEdits = True
.AllowDeletions = True
.btn16DeleteOne.Enabled = True
.btn05Programming.Enabled = True
End Select
End With
End Function
To make the "UserPermission" function do its thing, you have two ways to call it:
UserPermission Me
Just write "UserPermission Me" and you're good to go. This tells VBA to run the "UserPermission" function, passing the current object (referred to as "Me") as an argument. It's the shorter and more common way to do it.
Call UserPermission(Me)
If you want to be extra clear, you can use the "Call" keyword like this: "Call UserPermission(Me)". It's a bit longer, but it explicitly tells VBA that you're calling the "UserPermission" function and passing "Me" as an argument.
Now, here's a tricky part: if you accidentally write "UserPermission(Me)" without the "Call" keyword, VBA gets confused and thinks you meant "UserPermission (Me)" with parentheses. As a result, it throws an error. So, be sure to either use "UserPermission Me" or "Call UserPermission(Me)" to call the function without causing any errors.
Remember, you're just telling VBA to do something with the function, and you don't need it to give you anything back for later use in the code. That's all it is, just a matter of VBA's particular syntax! For instance, if you wanted the function to return something, say, the name of the form, then you would write the function like this:
Code:
Function UserPermission(frm As Form) As String
' Do stuff here
' Now let the function know what it must return:
UserPermission = frm.Name
End Function
And when you invoke this function, you must assign it to something that expects a string, like a textbox value or a variable of that type, like this
SomeTextBox = UserPermission(Me) 'returns the name of the form in the textbox
SomeVariable = UserPermission(Me) 'returns the name of the form in the variable for later use
If you still get an error with that, complaining about btn16DeleteOne or btn05Programming not existing, make sure your form does have these controls. If you still get an error after that, let me know as there might be extra stuff to take care of. I just tested this code with some random form and it works.