Login Access for Navigation Form (1 Viewer)

mistyinca1970

Member
Local time
Today, 13:58
Joined
Mar 17, 2021
Messages
117
I have created some user access IDs and I have a login form that transfers the user's access ID into a TempVars.

Here's my issue. I'm using a Navigation form, and I have an Admin tab that only I will have access to. I know the "If" part of my On Load event is working because when I log in as someone without access, it pops up the message box, but after clicking OK it opens the form anyway. I'm suspecting it has something to do with being in the navigation form. Here is the On load code for this form:
Code:
Private Sub Form_Load()
    If Globals.UserAccess(Me.Name) = False Then
        MsgBox "You do not have the required permissions to access this tab."
        DoCmd.Close acForm, Me.Name
    End If
End Sub

AdminTab.PNG
 

theDBguy

I’m here to help
Staff member
Local time
Today, 13:58
Joined
Oct 29, 2018
Messages
16,158
You could try putting that code in the Open event and then Cancel the event (Cancel=True) rather than close the form.

Just a thought...
 

mistyinca1970

Member
Local time
Today, 13:58
Joined
Mar 17, 2021
Messages
117
You could try putting that code in the Open event and then Cancel the event (Cancel=True) rather than close the form.

Just a thought...
I'll dig around that and see if I can figure that out, but if it is the issue of the Navigation form and a naming issue, wouldn't I still encounter the same problem?
 

Ranman256

Well-known member
Local time
Today, 16:58
Joined
Apr 9, 2015
Messages
3,922
when user opens the db, the main menu form will open and grab the userID.
Then lookup that persons rights

Code:
sub form_load()  'you can grab user ID when login form loads
dim vLevel

gvUserID =  Environ("Username")      'get userID from windows
txtUserID  =gvUserid
'gvUserID = txtUserID  'or get it off the form
end sub



sub btnLogin_click()
dim vPass, vLevel

gvUserid=txtUserID  

  'get level from user table
vLevel = Dlookup("[Level]","tUsers","[userID]='" & gvUserID & "'") & ""
vPass= Dlookup("[Password]","tUsers","[userID]='" & gvUserID & "'") & ""

if vPass= txtPass then
   docmd.openform "fMainMenu"
else
   msgbox "Incorrect UserID or Password
endif
end sub
 

mistyinca1970

Member
Local time
Today, 13:58
Joined
Mar 17, 2021
Messages
117
You could try putting that code in the Open event and then Cancel the event (Cancel=True) rather than close the form.

Just a thought...
OK, this worked!
Code:
Private Sub Form_Open(Cancel As Integer)
    If Globals.UserAccess(Me.Name) = False Then
        MsgBox "You do not have the required permissions to access this tab."
        DoCmd.CancelEvent
        Cancel = True
    End If
End Sub
Thanks!
 

theDBguy

I’m here to help
Staff member
Local time
Today, 13:58
Joined
Oct 29, 2018
Messages
16,158
OK, this worked!
Code:
Private Sub Form_Open(Cancel As Integer)
    If Globals.UserAccess(Me.Name) = False Then
        MsgBox "You do not have the required permissions to access this tab."
        DoCmd.CancelEvent
        Cancel = True
    End If
End Sub
Thanks!
Congratulations! Glad to hear you got it sorted out. Good luck with your project.
 

mistyinca1970

Member
Local time
Today, 13:58
Joined
Mar 17, 2021
Messages
117
Thanks again!
I have another question though...
Is there a way to add this to the module so that I don't have to add it to the open event of every form?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 13:58
Joined
Oct 29, 2018
Messages
16,158
Thanks again!
I have another question though...
Is there a way to add this to the module so that I don't have to add it to the open event of every form?
Hi. If you were asking me, which module were you referring to.

You can of course add code to the button that tries to open a form to check first if the user is allowed to or not. For example:

Code:
Private Sub cmdAdmin_Click()

If Globals.UserAccess("AdminForm") = False Then
    MsgBox "You do not have the required permissions to access this tab."
Else
    DoCmd.OpenForm "AdminForm" 'or whatever code you need to open the form in the Navigation Form
End If

End Sub
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:58
Joined
Feb 28, 2001
Messages
20,650
Here is the catch to doing what you asked directly: In a form's event code, Me.control is valid syntax because inside a form, the compiler uses Me. as a self-referential shortcut. In a module, ANY form can call it, but Me. is COMPILE-time, not RUN-time. Therefore, inside the module, the compiler cannot know who Me. is.

There IS a way around this, but the more you try to generalize it, the trickier it gets. You would do better to just have little code snippets hanging around waiting to be copied via cut/paste.

OR if you anticipate having a LOT of forms, you could make a "template" form that contains the common logic for these kinds of tests. I had a project where having the template pre-built and ready to be copied/customized allowed me to save about 40% of the time for EACH FORM that I built this way.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 16:58
Joined
Feb 19, 2002
Messages
32,833
Me.Name = the name of the form. Is that what you are referencing? If you have made the mistake of defining a table column named "Name", now would be a god time to chage it to CustomerName or something meaningful that is NOT the name of a property.

Me.[Name] would be the column named Name.

Good practice is to never include spaces or special characters in your object names and NEVER to use reserved words such as the names of properties or functions.
 

trevermah

New member
Local time
Today, 16:58
Joined
Jun 16, 2020
Messages
9
Thanks again!
I have another question though...
Is there a way to add this to the module so that I don't have to add it to the open event of every form?
If you found a way please share. I have this on every form and have to maintain a form access table. I have no issues with form.close, but might change to cancel=true. Is there any advantage to it?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 16:58
Joined
Feb 19, 2002
Messages
32,833
@mistyinca1970 ,
Did you change the column name from "Name" to something that is NOT the name of a property or a function? If you didn't then Me.Name = the form's name. If you remember to do it, Me.[Name] will be YOUR Name field. However, it is far, far better to change the name than to rely on people remembering to use Me.[Name} in ALL cases where you want to reference YOUR Name column.

If you found a way please share. I have this on every form and have to maintain a form access table.
You can create a function in a standard code module - NOT in a form's class module that holds the common code. If you need to reference the form, you can pass in the form object when you call the procedure.

Code:
Call CommonOpen(Me.Form)
Code:
Public Function CommonOpen(frm as Form)
.. your code.  Use frm. rather than Me. to reference the form's objects
End Function
 

Users who are viewing this thread

Top Bottom