How do I set an object to the current form onload?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ryno Bower
    New Member
    • Nov 2010
    • 76

    How do I set an object to the current form onload?

    Hi,

    I created a procedure which I call back on the onload property of a form.this is to check if the current user logged in is an administrator, and if not to deny access to the form. That code work fine.

    I have a problem in the function.

    I want to get the name of the current form on load.

    This is the procedure I have

    Code:
    Dim MyObject As Object
    Set MyObject as Me.Form.OnLoad
    Then the code follows to check if user is administrator.
    This gives me errors.

    Could someone please help me to define the code. I need to set the object to the current form on load.
    The reason is that I can just call the function on any form I like to check if the user is an administrator. Otherwise should I write a ne function for every form?

    Thanks in advance
    Last edited by NeoPa; Mar 16 '12, 12:59 AM. Reason: Added mandatory [CODE] tags for you.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32633

    #2
    I've just worked out what you're talking about. It wasn't easy. Let me explain as clearly as I can.

    When a form is loaded, or triggers any event indeed, it can point to a procedure defined in its own associated module only. Thus, it is not possible to have common code run on the load of any number of forms. Indeed, the values for the event related properties of a form are strings of the form "[Event Procedure]" (or the name of a Macro, but don't go there, that way be dragons).

    What is possible, is to have some simple code on each form which simply calls a Public procedure from a Standard module (One not associated with any particular object like a Form or Report). The common code in each of your forms might look like :

    Code:
    Private Sub Form_OnLoad(Cancel As Integer)
        Cancel = Not IsAdmin()
    End Sub
    The other module might look like :

    Code:
    Option Comare Database
    Option Explicit
    
    Public Function IsAdmin() As Boolean
        'Code here which returns True if user = Admin otherwise False
    End Function
    Last edited by NeoPa; Mar 16 '12, 11:46 PM. Reason: Added typing of function.

    Comment

    • Ryno Bower
      New Member
      • Nov 2010
      • 76

      #3
      Hi NeoPa,

      I tried what you gave me but still does not work.
      Please see my codes:

      This is my module - DenyAccess
      Code:
      Option Compare Database
      Option Explicit
      
      Public Function IsAdmin()
      If DLookup("IsAdmin", "Users", "UserName ='" & Forms!frmMainScreen!txtUserName & "'") = True Then
      DoCmd.GoToRecord acDataForm, , acNewRec
      Me.FullName.SetFocus
      Else
      MsgBox "You are not an administrator.  Access denied!" & vbCr & vbCr & "Please contact your administrator if you need access to " & Me.Form.Caption & ".", vbCritical, "Access Denied"
      DoCmd.Close
      End If
      End Function
      And this is on the form onload:
      Code:
      Private Sub Form_Load(Cancel As Integer)
      Cancel = Not IsAdmin()
      End Sub
      It still gives me an error.

      I want to check if the user is an administrator with the Dlookup.
      then on the form onload must run the function and if the user is not an administrator have a message box saying Access Denied. Then close the form.

      Please help
      Last edited by NeoPa; Mar 16 '12, 11:41 PM. Reason: Added mandatory [CODE] tags for you.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32633

        #4
        First, see Before Posting (VBA or SQL) Code as I'm shocked that you're still posting code without the tags when you've been here since November 2010.

        Originally posted by Ryno Bower
        Ryno Bower:
        It still gives me an error.
        And you think it's a good idea to play guessing games with me on what the error is? Well, it's your time you're wasting too - Not just mine ;-)

        I just updated my post #2 so as to type the IsAdmin() explicitly as Boolean (Which omission was frankly an oversight). However, it's clearly a function, so needs a return value (as indicated explicitly in the comment found within my function code). A big problem with your code is that it returns no value, so of course, it will never work (logically).

        If it returns an error as well as failing logically, then I can't help without details, but certainly the logic needs fixing.

        Comment

        Working...