Example Code To verify user name and password from table.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Does anyone out there have some sample code that shows ho I could save usernames and passwords to a table and use the list to restrict access to certain forms in a database?
 
Eric,

What you're asking for is certainly doable, but I just wanted to put another
alternative on the table, in case it suits you, as it's somewhat simpler to
implement and friendlier for the user. The assumption here is that your
users work on a Windows based network, and each has their own login name. If
the assumption holds true, then you can use the Environ("UserName") function
to return a user's network login name, and apply privileges / restrictions
on that basis, or filter table records etc. The function works in code as
well as in queries as a criterion. The added advantage is users don't have
to log on to the database, forget their passwords etc, plus you are not
storing any paswords anywhere that a "curious" user could dig up. I've been
using this technique on multi-user databases on a company network and it's
proven very convenient for me.

HTH,
Nikos

Eric said:
Does anyone out there have some sample code that shows ho I could save
usernames and passwords to a table and use the list to restrict access to
certain forms in a database?
 
Are you able to restrict usage to certain forms and the ability to edit data in certain tables using this method? If so, is there some property that has to be set on the tables or forms?
 
Eric,

My proposed method allows you to do whatever you would do with the custom
"security" you had in mind. The way to do what you are asking is to hide the
database window, so users can't access objects directly, and use, for
example, command buttons on forms to handle the objects (or switchboard(s),
the concept is the same). Then you can embed code in your command buttons
that will or will not allow users to carry out the action depending on the
user login name, or you can even show/hide or enable/disable command buttons
based on user login name, through some code in the On Open event of the form
the command buttons sit in. In either case, you could hardcode the user
names (not a very good idea in terms of maintenance) or you could store that
information in tables, which is a lot easier to maintain. For instance, you
could have a table with the user login name (PK) and a yes/no field for each
form, report etc. you want to control access to. I would suggest you do not
provide direct access to tables at all, just through forms.
An example of the first option (check security after command button is
clicked), to open a form:

Private Sub Command1_Click()
Dim vAllow as Boolean
vAllow = DLookup("[fldForm1]","tblSecurity","[fldUser]='" &
Environ("UserName") & "'")
If vAllow = False Then
MsgBox "Sorry, action not allowed", vbCritical, "Security Problem"
Exit Sub
End If
DoCmd.OpenForm ....'etc
....
End Sub

An example of the second option (disable button on form open):

Private Sub Form_Open()
Dim vAllow as Boolean
vAllow = DLookup("[fldForm1]","tblSecurity","[fldUser]='" &
Environ("UserName") & "'")
If vAllow = False Then
Me.CmdForm1.Enabled = False
End If
End Sub
(assumes all command buttons enabled by default)

HTH,
Nikos



Eric said:
Are you able to restrict usage to certain forms and the ability to edit
data in certain tables using this method? If so, is there some property that
has to be set on the tables or forms?
 
Back
Top