By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,052 Members | 1,456 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,052 IT Pros & Developers. It's quick & easy.

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

P: 76

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

Expand|Select|Wrap|Line Numbers
  1. Dim MyObject As Object
  2. 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
Mar 15 '12 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 15k+
P: 31,494
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 :

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_OnLoad(Cancel As Integer)
  2.     Cancel = Not IsAdmin()
  3. End Sub
The other module might look like :

Expand|Select|Wrap|Line Numbers
  1. Option Comare Database
  2. Option Explicit
  4. Public Function IsAdmin() As Boolean
  5.     'Code here which returns True if user = Admin otherwise False
  6. End Function
Mar 16 '12 #2

P: 76
Hi NeoPa,

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

This is my module - DenyAccess
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  4. Public Function IsAdmin()
  5. If DLookup("IsAdmin", "Users", "UserName ='" & Forms!frmMainScreen!txtUserName & "'") = True Then
  6. DoCmd.GoToRecord acDataForm, , acNewRec
  7. Me.FullName.SetFocus
  8. Else
  9. 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"
  10. DoCmd.Close
  11. End If
  12. End Function
And this is on the form onload:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load(Cancel As Integer)
  2. Cancel = Not IsAdmin()
  3. 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
Mar 16 '12 #3

Expert Mod 15k+
P: 31,494
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.

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.
Mar 16 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.