473,378 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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
3 2060
NeoPa
32,556 Expert Mod 16PB
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
  3.  
  4. Public Function IsAdmin() As Boolean
  5.     'Code here which returns True if user = Admin otherwise False
  6. End Function
Mar 16 '12 #2
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
  3.  
  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
NeoPa
32,556 Expert Mod 16PB
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

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

Similar topics

3
by: sea | last post by:
I need to be able to print the current form because the form has an embedded object of a .gif file, so report will not work -- does anyone know the way to do this? I tried some code, not sure what...
4
by: ghadley_00 | last post by:
Hi, Can anyone recommend a piece of VBA code I could attach to a button in a MS Form form that will close the current form and bring the switchboard to foreground. I'm trying to integrate a form...
2
by: MLH | last post by:
Suppose that code running on FormB is moving the focus around on FormA to various textbox controls on FormA - Which form is the current form during this process? Is it FormA, which has the...
3
by: Deacon Dan | last post by:
I want to be able to keep the current form on the current record. That is, the arrow key, the tab or the enter should not cause the form to go to next record or previous record. Preferably loop...
4
by: Jacko7289 | last post by:
Hi, I've got a form (SearchForm1) which searches the student number using the surname, given name, date of birth. Once the student number is shown i want the user to click a button which will close...
2
by: herbiegrey | last post by:
Hello This is my first post on the forum so hello everyone. I have a problem with access that I can't manage to view the current form record as a report ready to be printed. I have a list...
1
by: ismailc | last post by:
Hi, I need help please! I have an hyperlink object which i want the navigateurl to open within the current form below the object. so is there a way to create a frame within the current form &...
8
gcoaster
by: gcoaster | last post by:
Hello Access Guru's After pulling out most of my hair last year getting access to work the way I wanted it to to perform the simplest tasks, I am back at it and aim to not to pull out the rest! ...
6
by: dotnetnovice | last post by:
Hi everybody. I need some help in c# forms problem which is i have three forms naming main form,form1 & form2. main form is the mdiparent of both form1 and form2. i also have a toolstrip in my...
7
by: RG360 | last post by:
Hello. I am new in Access and I need assistance from Pro's I have a data entry form from 2 main tables and they have relationships with other tables. My main table only contains Field ID's,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.