OnOpen Form - Security

  • Thread starter Thread starter Annie
  • Start date Start date
A

Annie

Hello,

I have a form where I have added code to the OnOpen design
of the form to check certain security levels and usernames
to distinguish if a person has the rights to read the form.

I have added two fields to my employee table Username and
Security Level. The code below works but when there is no
username or security level for a particular person I get
an error saying no current record. The code with the
asterix is what I added in to try and solve this problem
the rest of the code works if there is a username and
level.

I somehow want it to check to see if there is no username
or security level then I want to display a msg and then
just exit the Sub.

Private Sub Form_Open(Cancel As Integer)

Dim db As Database
Dim rs As Recordset
Dim strSQL As String
Dim strUserName As String
Dim strUserPRI As String
Dim intUserSecurityLevel As Integer

strUserName = fOSUserName

strSQL = "SELECT EMPLOYEE.UserName, EMPLOYEE.PRI_SN,
Employee.SecurityLevel" & _
" FROM EMPLOYEE Where Employee.UserName = '" & strUserName
& "';"

Set db = CurrentDb

Set rs = db.OpenRecordset(strSQL, dbOpenDynaset,
dbSeeChanges)

*If IsNull(intUserSecurityLevel) Or IsNull(strUserPRI) Then

*MsgBox "You are not an active member of CFEC. Please
contact the DBA"
*DoCmd.Close
*ExitSub
*End If

strUserPRI = rs!PRI_SN
intUserSecurityLevel = rs!SecurityLevel
strUserName = rs!UserName

If intUserSecurityLevel <> 3 And intUserSecurityLevel
<> 4 Then

DoCmd.Close

MsgBox "You do not have sufficient permissions to open
this form."

Exit Sub

End If

End Sub

Thanks for any help!
 
Hello Annie,

Give the following a try ...

Set rs = db.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges)

rs.MoveLast
If rs.RecordCount = 0 then
MsgBox "You are not an active member of CFEC. Please contact the DBA"
Exit Sub
Else

' Continue with the rest of your code.
End If


hth,
 
Thanks Cheryl,

I still get a msg telling me that there is no current
record and points to this step: rs.MoveLast. So I removed
this line and it works fine.

Is this ok?

Also I added DoCmd.Close because without this the form is
still opening up after the msg.

Thanks..
 
My apologies. MoveLast isn't going to work if there are no records. You
are correct in removing that line.
 
What is to stop anyone linking to your database tables & updating the
security levels to whatever they want?

TC
 
Back
Top