I am using the following VBA code:
Private Sub btnStatistics_click()
On Error GoTo Err_Handler
Dim strPasswd As String
strPasswd = InputBox("Please Enter Password", "Password Required")
If strPasswd = Format(Now, "Hh") * 2 Then
DoCmd.Close acForm, "frmCustomer", acSaveYes
DoCmd.OpenForm "frmStatistics", acNormal, "", "", acEdit, acNormal
Exit Sub
Else
MsgBox "Incorrect password!", vbOKOnly, "Password Info"
End If
Exit_This_Sub:
Exit Sub
Err_Handler:
MsgBox "Error #: " & Err.Number & " " & Err.Description
Resume Exit_This_Sub
End Sub
I am using this VBA code in many buttons in different forms to do different things. I want to move the part strPasswd = Format(Now, "Hh") * 2 into a module so I can update/change it in one place.
If it is only the test of the password that you want to move, create a Function that returns a Boolean:
Function PasswordOK(strPwd As String) As Boolean
PasswordOK = strPwd = Format(Now, "Hh") * 2
End Function
and then you can use it as:
If PasswordOK(strPasswd) Then
DoCmd.Close acForm, "frmCustomer", acSaveYes
DoCmd.OpenForm "frmStatistics", acNormal, "", "", acEdit, acNormal
'Exit Sub '<-- this isn't needed, because the next
' statement after this one is also Exit Sub
Else
MsgBox "Incorrect password!", vbOKOnly, "Password Info"
End If
Or, if appropriate, you could move even more of the code into the common routine by passing a few more parameters:
Sub ChangeForm(oldForm As String, newForm As String)
Dim strPasswd As String
strPasswd = InputBox("Please Enter Password", "Password Required")
If strPasswd = Format(Now, "Hh") * 2 Then
DoCmd.Close acForm, oldForm, acSaveYes
DoCmd.OpenForm newForm, acNormal, "", "", acEdit, acNormal
Else
MsgBox "Incorrect password!", vbOKOnly, "Password Info"
End If
End Sub
and use it as
Private Sub btnStatistics_click()
ChangeForm "frmCustomer", "frmStatistics"
End Sub
Or perhaps somewhere between the two, putting just the input of the password, and its testing, into the common routine:
Function PasswordOK() As Boolean
Dim strPasswd As String
strPasswd = InputBox("Please Enter Password", "Password Required")
If strPasswd = Format(Now, "Hh") * 2 Then
PasswordOK = True
Else
MsgBox "Incorrect password!", vbOKOnly, "Password Info"
PasswordOK = False
End If
End Function
and use it as
Private Sub btnStatistics_click()
On Error GoTo Err_Handler
If PasswordOK() Then
DoCmd.Close acForm, "frmCustomer", acSaveYes
DoCmd.OpenForm "frmStatistics", acNormal, "", "", acEdit, acNormal
End If
Exit_This_Sub:
Exit Sub
Err_Handler:
MsgBox "Error #: " & Err.Number & " " & Err.Description
Resume Exit_This_Sub
End Sub
Related
So essentially what im trying to do is add my Employees first name to a welcome message. Currently my code looks a bit like this:
If txtPassword.Value = DLookup("EmplPassword", "Employees", "[EmplID]=" & cboEmployee.Value) Then
MyEmplID = cboEmployee.Value
DoCmd.Close acForm, "frmLogin", acSaveNo
If MyEmplID = 5 Then
MsgBox "Welcome Admin"
DoCmd.OpenForm "Switchboard Main", acNormal
Else
MsgBox ("Welcome " & EmplFname.Text)
DoCmd.OpenForm "Switchboard Main", acNormal
End If
and this is the part im trying to make work:
MsgBox ("Welcome " & EmplFname.Text)
But I keep getting a "Run time error 424: Object Required" error.
Note: This is for a school project, security in terms of the passwords are not part of the brief so Im not too worried about that
You probably have the employee name in a column of the combobox, so this should do:
If Nz(txtPassword.Value) = Nz(DLookup("EmplPassword", "Employees", "[EmplID]=" & cboEmployee.Value)) Then
MyEmplID = cboEmployee.Value
EmplFname = cboEmployee.Column(n) ' adjust n as needed.
DoCmd.Close acForm, "frmLogin", acSaveNo
If MyEmplID = 5 Then
MsgBox "Welcome Admin"
Else
MsgBox "Welcome " & EmplFname
End If
DoCmd.OpenForm "Switchboard Main", acNormal
End If
Try this
MsgBox "Welcome " & DLookup("EmplFname", "Employees", "[EmplID]=" & cboEmployee.Value)
My intent is to deny users that do not meet a certain access level access to forms. I initially had issues with error code 3265 while writing the code for:
TempVars("EmployeeType").Value = rs!EmployeeType_ID.Value
This is no longer an issue; however, I cannot get access to the form even when the appropriate user is trying to enter. I've checked the spelling of table and column names multiple times as well.
Below is my code for the login (where I'm using the tempvars), followed by the code in form Load().
Option Compare Database
Option Explicit
Private Sub btnLogin_Click()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("Employees", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='" & Me.txtUserName & "'"
If rs.NoMatch = True Then
Me.lblWrongUser.Visible = True
Me.txtUserName.SetFocus
Exit Sub
End If
Me.lblWrongUser.Visible = False
If rs!Password <> Me.txtPassword Then
Me.lblWrongPass.Visible = True
Me.txtPassword.SetFocus
Exit Sub
End If
If IsNull(Me.txtUserName) Or IsNull(Me.txtPassword) Then
MsgBox "You must enter password or login ID.", vbOKOnly + vbInformation, "Required Data"
Me.txtUserName.SetFocus
Exit Sub
End If
Me.lblWrongPass.Visible = False
If rs!EmployeeType >= 4 Then
Dim prop As Property
On Error GoTo SetProperty
Set prop = CurrentDb.CreateProperty("AllowBypassKey", dbBoolean, False)
TempVars("UserName").Value = Me.txtUserName.Value
TempVars("EmployeeType").Value = rs!EmployeeType_ID.Value
CurrentDb.Properties.Append prop
SetProperty:
If MsgBox("Would you like to turn on the bypass key?", vbYesNo, "Allow Bypass") = vbYes Then
CurrentDb.Properties("AllowBypassKey") = True
Else
CurrentDb.Properties("AllowBypassKey") = False
End If
End If
Me.Visible = False
DoCmd.OpenForm "frmMain"
Globals.LoggingSignOn "Logon"
End Sub
Private Sub Form_Load()
Me.txtUserName = Null
Me.txtPassword = Null
Me.txtUserName.SetFocus
End Sub
Private Sub Form_Unload(Cancel As Integer)
Globals.LoggingSignOn "Logoff"
End Sub
Private Sub Form_Load()
If Nz(DLookup("HasAccess", "tbl9EmployeeAccess", "EmployeeType_ID=" & TempVars("EmployeeType") & " FormName='" & Me.Name & "'"), False) = False Then
MsgBox "You do not have access to access this location."
DoCmd.Close acForm, Me.Name
End If
End Sub
Thank you for your time, to anybody that looks into this.
I am creating a simple login form for my Database. When I click login, the message "Compile Error: Method or data member not found" appears. How do I fix that? Thanks! Code is below
Option Compare Database
Option Explicit
Private Sub btnLogin_Click()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("TBL:Staff", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='" & Me.txtUserName & "'"
If rs.NoMatch = True Then
Me.lblWrongUser.Visible = True
Me.txtUserName.SetFocus
Exit Sub
Me.lblWrongUser.Visible = False
If rs!Password <> Nz(Me.txtPassword, "") Then
Me.lblWrongPass.Visible = True
Me.txtPassword.SetFocus
Exit Sub
End If
Me.lblWrongPass.Visible = False
DoCmd.OpenForm "FRM:Customer"
DoCmd.Close acForm, Me.Name
End Sub
Try this.
Check username and password values have been provided, then see if they exist in the database by a simple DCount.
If the username/password exist it will return > 0 and if not, it will return 0.
Private Sub btnLogin_Click()
With Me
'Username/Password - value provided?
If IsNull(.txtUserName.Value) Or IsNull(.txtPassword.Value) Then
MsgBox "Both fields required.", vbExclamation
Exit Sub
End If
'Username exists in Table?
If DCount("*", "Staff", "UserName='" & .txtUserName.Value & "'") = 0 Then
.lblWrongUser.Visible = True
.txtUserName.SetFocus
Exit Sub
End If
'Password exists in Table?
If DCount("*", "Staff", "UserName='" & .txtUserName.Value & _
"' And Password='" & .txtPassword.Value & "'") = 0 Then
.lblWrongPass.Visible = True
.txtPassword.SetFocus
Exit Sub
End If
End With
'Code will reach here only if supplied username and passowrd are correct
With DoCmd
.OpenForm "Customer", acNormal, , , acFormPropertySettings, acWindowNormal
.Close acForm, Me.Name, acSavePrompt
End With
End Sub
i used this code to protect my log in, but i had a problem with the password, for example i've two username and two password...when i used Username both of password can log in...can anyone help...here the code i used...
Private Sub cmdmsk_Click()
Dim UserLevel As Integer
Me.cmdmsk.SetFocus
If IsNull(Me.txtuser) Then
MsgBox "plis enter username", vbInformation, "Username needs to Login"
Me.txtuser.SetFocus
ElseIf IsNull(Me.txtpass) Then
MsgBox "plis enter you password", vbInformation, "Password needs to login"
Me.txtpass.SetFocus
Else
'process the job
If (IsNull(DLookup("UserLogin", "tblUser", "UserLogin ='" & Me.txtuser.Value & "'"))) Or _
(IsNull(DLookup("Password", "tblUser", "Password ='" & Me.txtpass.Value & "'"))) Then
MsgBox "wrong pass or username"
Else
UserLevel = DLookup("UserSecurity", "tblUser", "UserLogin = '" & Me.txtuser.Value & "'")
If UserLevel = "1" Then
MsgBox "Cangratulations ^_^"
DoCmd.Close
DoCmd.OpenForm "MENU"
Else
DoCmd.Close
DoCmd.OpenForm "INPUT"
End If
End If
End If
End Sub
Do one look up only:
Private Sub cmdmsk_Click()
Dim UserLevel As Variant
Me!cmdmsk.SetFocus
If IsNull(Me!txtuser.Value) Then
MsgBox "Please enter your username.", vbInformation, "Missing Username"
Me!txtuser.SetFocus
ElseIf IsNull(Me!txtpass.Value) Then
MsgBox "Please enter your password", vbInformation, "Missing Password"
Me!txtpass.SetFocus
Else
'process the job
UserLevel = DLookup("UserSecurity", "tblUser", _
"[UserLogin] = '" & Me!txtuser.Value & "' And [Password] = '" & Me!txtpass.Value & "'")
If IsNull(UserLevel) Then
MsgBox "Incorrect username or password.", vbInformation, "Login"
Else
If UserLevel = "1" Then
DoCmd.OpenForm "MENU"
Else
DoCmd.OpenForm "INPUT"
End If
DoCmd.Close
End If
End If
End Sub
Option Compare Database
Private Sub Command1_Click()
Dim UserLevel As Integer
If IsNull(Me.txtLoginID) Then
MsgBox "Please enter LoginID", vbInformation, "LoginID Required"
Me.txtLoginID.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else
'process the job
If (IsNull(DLookup("UserLogin", "tbluser", "UserLogin='" & Me.txtLoginID.Value & "'"))) Or _
(IsNull(DLookup("UserLogin", "tbluser", "Password='" & Me.txtPassword.Value & "'"))) Then
MsgBox "Incorrect LoginID or Password"
Else
UserLevel = DLookup("UserSecurity", "tbluser", "UserLogin = '" & Me.txtLoginID.Value & "'")
DoCmd.Close
If UserLevel = 1 Then
MsgBox "LoginID and Password correct"
DoCmd.OpenForm "ali"
Else
DoCmd.OpenForm "customer"
End If
End If
End If
End Sub
This is code for login form.I have created. When I close my MS Access and open again my project login form cannot work.