Iif Then - Forcing Security for accdb (1 Viewer)

mmmontg

New member
Local time
Today, 10:06
Joined
Jan 17, 2019
Messages
4
I have buttons leading to forms that should only be accessed by Users if their ID appears in tblAdmins. How do I correctly format the bolded line:

Private Sub AllRelBut_Click()
On Error GoTo Err_AllRelBut_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frmButtonsAdminRel"
'Iif Me.UserID is in tbl.Admins Then
DoCmd.OpenForm stDocName, , , stLinkCriteria
Else
MsgBox UserID & " is not authorized to enter the Administrator's section."
Exit Sub
End If
Exit_AllRelBut_Click:
Exit Sub
Err_AllRelBut_Click:
MsgBox Err.Description
Resume Exit_AllRelBut_Click

End Sub
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 11:06
Joined
May 21, 2018
Messages
8,527
See if it is in the table once
if Dcount("*","tblAdmin", "UserID = '" & me.userID & "'") > 0 then
or if user id is numeric
Dcount("*","tblAdmin", "UserID = " & me.userID )
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:06
Joined
Feb 28, 2001
Messages
27,175
I took a variant approach.

When my users launched their copies of the front end, the startup form look up who they were and did all of the lookups required from the Users table, which included a role field. The "Supervisor" role was one of the possible roles.

I created a general "Security" module that held all the public subroutines that did the lookups (thus callable from all forms) and had public variables that all other forms could reference. Then the test was IF glRole = cSupervisor THEN... (where I defined the constants for supervisor, general user, guest, and non-user with a public ENUM block so THEY could be used in tests.)
 

mmmontg

New member
Local time
Today, 10:06
Joined
Jan 17, 2019
Messages
4
Thanks so much for the quick reply, I'm new and have followup questions :)
In the tblAdmin, the field is "LANID"; but the corresponding ID on the current form is "UserID". If the current form's "UserID" is listed as a "LANID" in tblAdmin, I need it to count. Could you please clarify based on this info? Thanks again!

if Dcount("*","tblAdmin", "UserID = '" & me.userID & "'") > 0 then
 

mmmontg

New member
Local time
Today, 10:06
Joined
Jan 17, 2019
Messages
4
General database access is not restricted for most personnel and Users are constantly added and removed through our IT group.
I only want to restrict access to this one form for a group of Admins, without setting access levels for all personnel.
Thanks so much for your quick reply.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 11:06
Joined
May 21, 2018
Messages
8,527
Sorry should have added a link
https://support.office.com/en-us/article/dcount-function-f6b5d78b-ad0b-4e42-be7a-11a64acbf3d3

There are three arguments
The field Name (I used "*" meaning all fields)
The table or query name
Then a where statement without a where so if you want to see if the lanID in tblAdmin = somevalue and on your form that value in in field UserID then
"LanID = '" & me.userID & "'"

this would turn into a literal when it is past like
"LanID = 'Abc123'"
or whatever value is in me.userid
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 11:06
Joined
May 21, 2018
Messages
8,527
should say "when the string is passed" not "past"
 

Users who are viewing this thread

Top Bottom