[AccessD] Record security

Stuart McLachlan stuart at lexacorp.com.pg
Sun Apr 2 05:48:59 CDT 2006


On 2 Apr 2006 at 6:19, John Eget wrote:

> In this database, each record entry has an individual field for name.   I
> would like anyone to view another individuals records but I am trying to
> prevent another individual from editing or deleting a record that does not
> belong to them.  Does someone have an example of a login process and then
> disabling a record from being deleted or edited by another individual. 


Here's how I would do it using the Windows login name:
Assuming that the field in the record is called "UserName"

1. Create a function in a module to read the Windows login name:
Option Compare Database   'Use database order for string comparisons
Option Explicit
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal 
lpBuffer As String, nSize As Long) As Long
Function Username()
Dim strUname As String * 32
Dim lngResponse As Long
lngResponse = GetUserName(strUname, 32)
If Len(strUname) > 1 Then
    Username = Left$(strUname, InStr(strUname, Chr$(0)) - 1)
Else
    Username = "No logged In User"
End If
End Function

When the record is created, automatically fill the UserName field 
(a hidden bound text box on the form with a default value of "=Username()" 
will do it).


In the Form_Delete and Form_BeforeUpdate events include:

If Me.UserName <> Username() Then
   Msgbox "You can't alter someone else's records"
  Canel = True
  Exit Sub
End If

-- 
Stuart





More information about the AccessD mailing list