I have a database with a table called activeusers and 3 fields. "UserID" "Password" and "Admin". Admin is a yes/no field. I would like to use this field to enable certain buttons on the switchboard screen. how can I use vba to check wether the Admin feild is checked or not?
Advanced Login System with Admin Privilege?
Collapse
X
-
In the switchboard program, you will want to either set the .visible property or the .enabled property of the buttons you want to turn on or off.
The code will be
You might want to put this code in the OnLoad event of the form and also on the click event of whatever buttons moves to another user ID. Or maybe you want to put it in after the password is checked. Probably there is best, after they login you will turn the buttons on or off.Code:if me!nameofadmincheckbox = true then ' do true stuff else ' do false stuff endif
Jim -
I apologize I may not have been as clear as I needed to be. There are two forms I have: one a login and one a menu. I want certain buttons enabled to certain users and other buttons not enabled to certain users. Is there a way I set the current record through the login screen? Here is the code I have so far:In the switchboard program, you will want to either set the .visible property or the .enabled property of the buttons you want to turn on or off.
The code will be
You might want to put this code in the OnLoad event of the form and also on the click event of whatever buttons moves to another user ID. Or maybe you want to put it in after the password is checked. Probably there is best, after they login you will turn the buttons on or off.Code:if me!nameofadmincheckbox = true then ' do true stuff else ' do false stuff endif
Jim
Code:Private Sub Command8_Click() Username.SetFocus If Username = DLookup("UserID", "ActiveUsers", "[password] = '" & Password & "'") Then DoCmd.Close DoCmd.OpenForm "MainMenu" Else MsgBox "Inncorrect Username or Password" End If End SubComment
-
You need to reconsider your code. It is not reliable as the [Password] is not defined as unique (I hope).
Perhaps, killing two birds with one stone, the following could help :
Code:Private Sub Command8_Click() Dim strFilter As String, strAdmin As String Call Username.SetFocus strFilter = "(([UserID]='%U') AND ([Password]='%P'))" strFilter = Replace(strFilter, "%U", Me.UserName strFilter = Replace(strFilter, "%P", Me.Password strAdmin = Nz(DLookup("CStr([Admin])", _ "[ActiveUsers]", _ strFilter), "Fail") If strAdmin = "Fail" Then Call MsgBox("Incorrect Username or Password") Else Call DoCmd.OpenForm(FormName:="MainMenu" _ ,OpenArgs:=strAdmin) Call DoCmd.Close End If End SubComment
-
Neopa,You need to reconsider your code. It is not reliable as the [Password] is not defined as unique (I hope).
Perhaps, killing two birds with one stone, the following could help :
Code:Private Sub Command8_Click() Dim strFilter As String, strAdmin As String Call Username.SetFocus strFilter = "(([UserID]='%U') AND ([Password]='%P'))" strFilter = Replace(strFilter, "%U", Me.UserName strFilter = Replace(strFilter, "%P", Me.Password strAdmin = Nz(DLookup("CStr([Admin])", _ "[ActiveUsers]", _ strFilter), "Fail") If strAdmin = "Fail" Then Call MsgBox("Incorrect Username or Password") Else Call DoCmd.OpenForm(FormName:="MainMenu" _ ,Openargs:=strAdmin) Call DoCmd.Close End If End Sub
I tried your code and it works!
However, how would make the distinction of and Admin vs. a regular user?
I.E.:
I want certain buttons, like addnewbtn, or, updatebtn, only enabled to Administrators, not regular users. How could this be accomplished?Comment
-
If you look at line #15 in the code you'll see this is already passed to the form.Originally posted by NeoPaNeoPa: You need to reconsider your code. It is not reliable as the [Password] is not defined as unique (I hope).
Perhaps, killing two birds with one stone, the following could help :
Code:Private Sub Command8_Click() Dim strFilter As String, strAdmin As String Call Username.SetFocus strFilter = "(([UserID]='%U') AND ([Password]='%P'))" strFilter = Replace(strFilter, "%U", Me.UserName strFilter = Replace(strFilter, "%P", Me.Password strAdmin = Nz(DLookup("CStr([Admin])", _ "[ActiveUsers]", _ strFilter), "Fail") If strAdmin = "Fail" Then Call MsgBox("Incorrect Username or Password") Else Call DoCmd.OpenForm(FormName:="MainMenu" _ ,OpenArgs:=strAdmin) Call DoCmd.Close End If End Sub
To use this you need to set a variable to this value as soon as the form is opened (as it is lost very soon afterwards). If nothing is passed then OpenArgs is Null.
The code in your called form should be something like :
Code:Private strFrom As String Private Sub Form_Open(Cancel As Integer) strFrom = Nz(OpenArgs, "") End SubComment
Comment