Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    sdel_nevo is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402

    create A function that refrences forms

    Hi guys



    I am stopping users from right clicking on specific forms by placing this code into the forms onload event


    Code:
    If strAccess = "Admin" Then
          Me.ShortcutMenu = True
       Else
          Me.ShortcutMenu = False
       End If
    the code above works as expected, but I would like to replace this and just call it from a module to cut down on coding

    the code I have in my module is this

    Code:
       Public Function RightClickMenu()
    Dim objAllForms As AccessObject, dbs As Object
       Set dbs = Application.CurrentProject
       ' Search for open AccessObject objects in AllForms collection.
       For Each objAllForms In dbs.AllForms
          If objAllForms.IsLoaded = True And strAccess = "Admin" Then
             Me.ShortcutMenu = True
          Else
             Me.ShortcutMenu = False
          End If
            
       Next objAllForms
    end function
    then I want to call this code when a form opens using the forms on open event

    I'm guessing something like this
    Code:
    Private Sub Form_Load()
       
        
       'then we run the following button click events
    
    RightClickMenu
    End Sub

    what I cant get my head around is how I would call the function so that the me.shortcutmenu code works when called from a form

    for the love of me I can't work out how to do this

    many thanks
    Steve

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I would run the function for a single form, the name of which is passed in a string. Then I think this would work.

    Forms(VariableName).ShortcutMenu = True
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    I'm guessing something like this
    What happens? You haven't stated a problem.

  4. #4
    sdel_nevo is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    Hi Guys

    Thanks for the replys

    @BaldyWeb I will take a look at that option

    @davegri when debugging the module I get "compile error Invalid use of ME" error

    Steve

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    You can't use Me in a standard module. Me refers to the object the code is in, and there is no object associated with a standard module.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    sdel_nevo is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    Hi Pbaldy

    yes, that's the part I'm stuck with.

    I'm not sure how to reference the currently loaded form rather than "me." in the module,
    then call the module from a form that's currently loaded

    so for example


    on the frmMainDashboard onload event I call "Rightclickmenu"

    the Rightclickmenu code in the module updates the Me.ShortcutMenu to refrence frmMainDashboard

    steve

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    In my view it's inefficient to loop all forms every time, which is why I suggested running it for a single form. The function would start with:

    Public Function RightClickMenu(strFormName As String)

    and get called like:


    RightClickMenu Me.Name
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    It occurs to me you could use ActiveForm in the function and skip passing the form name.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    sdel_nevo is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    Hi pbaldy

    Many thanks for the help
    When I'm back at my desk in the morning, I will give that a go

    Steve

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Slacker, it's only 10am!

    No problem Steve, post back if you get stuck.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    sdel_nevo is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    Don't you just love 10am 😀
    Many thanks mate, will let you know how it goes

    Steve

  12. #12
    sdel_nevo is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    Hi again

    So something like this ?Screen.ActiveForm.ShortcutMenu

    To replace the "me.shortcutmenu" in the module code


    Steve

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Yes, but you're supposed to be at the pub!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  14. #14
    sdel_nevo is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    Hi mate

    That's great and many thanks, will have a cold one for you 😀

    Steve

  15. #15
    HiTechCoach's Avatar
    HiTechCoach is offline MS MVP - Access Expert
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2010
    Location
    Oklahoma, USA
    Posts
    705
    Quote Originally Posted by sdel_nevo View Post
    Hi guys

    I am stopping users from right clicking on specific forms by placing this code into the forms onload event


    Code:
    If strAccess = "Admin" Then
          Me.ShortcutMenu = True
       Else
          Me.ShortcutMenu = False
       End If
    the code above works as expected, but I would like to replace this and just call it from a module to cut down on coding

    the code I have in my module is this

    Code:
       Public Function RightClickMenu()
    Dim objAllForms As AccessObject, dbs As Object
       Set dbs = Application.CurrentProject
       ' Search for open AccessObject objects in AllForms collection.
       For Each objAllForms In dbs.AllForms
          If objAllForms.IsLoaded = True And strAccess = "Admin" Then
             Me.ShortcutMenu = True
          Else
             Me.ShortcutMenu = False
          End If
            
       Next objAllForms
    end function
    then I want to call this code when a form opens using the forms on open event

    I'm guessing something like this
    Code:
    Private Sub Form_Load()
       
        
       'then we run the following button click events
    
    RightClickMenu
    End Sub

    what I cant get my head around is how I would call the function so that the me.shortcutmenu code works when called from a form

    for the love of me I can't work out how to do this

    many thanks
    Steve
    Steve,

    I do something similar to set the ribbon and shortcut menu based on the user.

    Your original code was very close. To make it work you need to replace the ME with objAllForms .

    Like this:
    Code:
       Public Function RightClickMenu()
    Dim objAllForms As AccessObject, dbs As Object
       Set dbs = Application.CurrentProject
       ' Search for open AccessObject objects in AllForms collection.
       For Each objAllForms In dbs.AllForms
          If objAllForms.IsLoaded = True And strAccess = "Admin" Then
             objAllForms.ShortcutMenu = True
          Else
             objAllForms.ShortcutMenu = False
          End If
            
       Next objAllForms
    end function

    I use a global function to work with forms. I pass a reference to the form object so I can set lots of properties

    I would write your function like this:
    Code:
    Public Function RightClickMenu(frm as Form)
          If strAccess = "Admin" Then
             frm.ShortcutMenu = True
          Else
             frm.ShortcutMenu = False
          End If
            
       
    End Function
    The way I like to write the code would be:

    Code:
    Public Function RightClickMenu(frm as Form)
          
             frm.ShortcutMenu = (strAccess = "Admin")
       
    End Function


    Call it like this:

    Code:
    Private Sub Form_Load()
       
        
       'then we run the following button click events
    
      Call RightClickMenu(Me)
    End Sub
    Since what you want can actually be done with a single line of code, you can save the overhead of calling a function.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. IIF Function in Forms
    By withk001 in forum Forms
    Replies: 3
    Last Post: 10-06-2015, 09:09 AM
  2. Replies: 5
    Last Post: 01-24-2014, 09:05 AM
  3. add refrences by VBA
    By sdel_nevo in forum Programming
    Replies: 3
    Last Post: 08-24-2013, 11:23 AM
  4. Replies: 0
    Last Post: 03-17-2011, 09:57 AM
  5. Search function in forms
    By vCallNSPF in forum Forms
    Replies: 1
    Last Post: 03-14-2010, 06:17 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