Page 3 of 5 FirstFirst 12345 LastLast
Results 31 to 45 of 65
  1. #31
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274

    There are a few problems with that code. It's taking a recordset as a parameter, but you want to control AllowEdits and AllowDeletions, which are part of the Form object, not the Recordset object. Furthermore, there is no way to get to the form from a recordset, so we need to make a few changes. Instead of taking a recordset, we'll take a Form as parameter. From a form, if it has a recordset, you can access its recordset. Here's how it should look:
    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
                    .btnl6DeleteOne.Enabled = False
                    .btnOSPrograInming.Enabled = False
                Case 57
                    .AllowEdits = True
                    .AllowDeletions = True
                    .btnl6DeleteOne.Enabled = True
                    .btnOSProgramming.Enabled = False
                Case 56
                    .AllowEdits = True
                    .AllowDeletions = True
                    .btnl6DeleteOne.Enabled = True
                    .btnOSProgramming.Enabled = True
            End Select
    
    End Function
    Note: I was talking about the recordset because of the context of this thread, but this particular code does not need the recordset.

    I also got rid of the if/then/else blocks which can be better solved with a Select Case. This should work if the form where it is applied has a control called btnl6DeleteOne and another called btnOSProgramming. Beware of the typos, I got your code with OCR.

    You can call this from a form with:
    UserPermission Me

    Knowing how to inspect your objects will give you a lot of power for this. For that:
    1. In your VBA Window, make sure your Locals Window is enabled
    2. On the Form_Load or Form_Open event, Add the Stop keyword
    3. Open the form
    4. The Stop will let you view the Me object, which is a Form object
    5. Expand the plus sign and view its contents to know what properties belong to each object that loads with the Form
    Last edited by Edgar; 06-04-2023 at 12:02 AM. Reason: Added important info

  2. #32
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    Here shows an error about AllowEdits without Dim. After I dimmed it, the error stopped. My experience or skill falls short here. Need help with the fix. It worked well on the form, that I could manage. In a separate Module it is a litle different. I will try what Edgar sent me.

    Click image for larger version. 

Name:	230604b.png 
Views:	22 
Size:	32.7 KB 
ID:	50312

  3. #33
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    I copied your suggested code in #31 to separate Module.
    I added "UserPermission Me.Recordset" to form_open, also tried form_load. The error is Type Mismatch, Runtime error 13 when opening the form.

  4. #34
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    While you were doing your tests, I wrote extra stuff. You have to call it like this:
    UserPermission Me

    AllowEdits and AllowDeletions are properties of a Form object, Me is a form object. Your buttons become members of the Form object if they exist in the form that you pass there, so you don't need to declare them, they come with the Form, that's why I removed them. Since you are using a With instruction, you can refer to those properties writing a period/dot before. Think of objects in VBA as a tree structure, try to do the inspection exercise in my previous post to visualize this tree. Once you visualize it, you can use dot notation to access the different properties. For example, the AllowEdits property is part of a Form, so, if you were writing in the Form code window, you could use Me.AllowEdits = False or just AllowEdits = False. If you were writing in a module and you had to refer to the same property, then you first need to get the Form object, let's say the form object is a variable called MyForm, then you could use MyForm.AllowEdits = False

  5. #35
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    I called "UserPermission Me"
    Click image for larger version. 

Name:	230604c.png 
Views:	21 
Size:	11.4 KB 
ID:	50313Click image for larger version. 

Name:	230604d.png 
Views:	21 
Size:	18.8 KB 
ID:	50314

  6. #36
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    should that be btnDeleteOne or btn16DeleteOne?

    looks like btnOSProgramming is mis-spelled also. (btnOSPrograInming)
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  7. #37
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    Hi. btn16DeleteOne is correct, but btnOSProgramming is wrong. It is btn05Programming.

  8. #38
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Have you walked through your code?
    What shows for frm?

    I tend to call functions as UserPermisson(Me)
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #39
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    I corrected the spelling error. The error message is the same. I will walk through the code.

  10. #40
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    The error message is now "Type mismatch" Runtime error 13; again.

  11. #41
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    That was not your previous error?
    What line is the error on now.
    See post 35. At least something is not what type it is meant to be.

    It is like trying to get blood out of a stone helping you?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  12. #42
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    That same error did come up previously.
    Hi Mr. Welshgasman. Smile, of course I have no intention of making it difficult. Through 7 years, I got these things to work well if the code is on the form. It meant many hundreds of extra hours, against if I knew how to do this, but because of respect to guys like you I walked the long walk. I am passionate and keen to learn how to do it. The mountain is higher than what I initially assumed. I also experienced a lot of BST(blood, sweat and tears) in the journey of life. The show must go on. Even though I have more than 500 clips downloaded, now after 60 time will not allow to watch them all again, and I requested in this post if someone can refer me to training on this. Maybe in 6 hours' time I will post a small part of DB.
    Thank you.

    The error does not refer to the Module, but to the form now.
    Click image for larger version. 

Name:	230604e.png 
Views:	17 
Size:	10.1 KB 
ID:	50315

  13. #43
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Yes, but you are chainging errors all the time? :-(

    I do not *think* the controls are available at Form Open?
    I always use Form Load.

    Try your call there?. Otherwise DEBUG your code and look at type of variable/control for where error occurs.
    https://support.microsoft.com/en-au/...3-2b894389e82d
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  14. #44
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    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.

  15. #45
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    Where I am it is 21H40. But I will put in a few hours from now. I will now apply your latest suggestions. If it does not work, I will post a small part of my test DB even if it is tomorrow. Thank you.

Page 3 of 5 FirstFirst 12345 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. General module calling public function in an open form
    By GraeagleBill in forum Programming
    Replies: 3
    Last Post: 09-30-2022, 02:08 PM
  2. form onload event when calling standard module sub generates err
    By Synergy.ron@gmail.com in forum Access
    Replies: 6
    Last Post: 04-16-2021, 03:29 PM
  3. Calling a module into a form
    By CraigR in forum Modules
    Replies: 3
    Last Post: 12-12-2018, 08:04 PM
  4. Replies: 5
    Last Post: 11-25-2017, 03:45 AM
  5. Calling A Module Function To Open A Form
    By orcinus in forum Modules
    Replies: 3
    Last Post: 09-29-2010, 04:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums