On a form load event, I have it check the access level of the user. if it is below a certain threshold or they never signed in, then I have the line
Cancel = true
However, the form loads regardless. Here is the entire form load event.
Private Sub Form_Load()
If IsNull(Forms!frmUsernamePassword!txtUserName) Then
MsgBox "You need to enter a username or password first.", vbOKOnly + vbExclamation
Cancel = -1
ElseIf DLookup("[AccessLevel_ID]", "tblUsernamePasswords", "[txtUserName]= '" & Forms!frmUsernamePassword!txtUserName & "'") = 1 Then
MsgBox "Your Security Level is 1", vbOKOnly
ElseIf DLookup("[AccessLevel_ID]", "tblUsernamePasswords", "[txtUserName]= '" & Forms!frmUsernamePassword!txtUserName & "'") = 2 Then
MsgBox "Your Security Level is 2", vbOKOnly
ElseIf DLookup("[AccessLevel_ID]", "tblUsernamePasswords", "[txtUserName]= '" & Forms!frmUsernamePassword!txtUserName & "'") = 3 Then
MsgBox "Your Security Level is 3", vbOKOnly
ElseIf DLookup("[AccessLevel_ID]", "tblUsernamePasswords", "[txtUserName]='" Forms!frmUsernamePassword!txtUserName & "'") = 4 Then
MsgBox "Your Security Level is 4", vbOKOnly
End If
End Sub
sorry for the formatting issures.
Form load does not have a cancel argument. Use form open for cancel. Setting contents of controls must occur in form load.
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)
I have a form where I don't want the user to be able to enter a contact with the same name.
The form has a save button. To check for a duplicate, part of that button's code is :-
If Me.DataEntry = False Then
Else
'Check for duplicate contact
If DCount("[ContactID]", "tblContactsNew", "[first_name] = " & "'" & Me.FIRST_NAME & "'" & "And [Surname] = " & "'" & Me.SURNAME & "'") <> 0 Then
MsgBox "A contact with these details already exists:-" & vbCrLf, vbOKOnly + vbExclamation
End
End If
End If
which is before :-
DoCmd.RunCommand acCmdSaveRecord
If the count <> 0, the message is displayed. But if I then click my Cancel button, there is still a record added for the duplicate.
The code for the cancel button is :-
If Me.Dirty Then
Me.Undo
End If
DoCmd.Close acForm, Me.Name
How can I prevent the duplicate record being written?
I'd migrate all that code to the forms Form_BeforeUpdate event.
In your save button code, the only thing you need is DoCmd.RunCommand acCmdSaveRecord
Then, in your Form_BeforeUpdate event:
If Me.DataEntry = False Then
Cancel = True 'Don't save the form I guess
Else
'Check for duplicate contact
If DCount("[ContactID]", "tblContactsNew", "[first_name] = " & "'" & Me.FIRST_NAME & "'" & "And [Surname] = " & "'" & Me.SURNAME & "'") <> 0 Then
MsgBox "A contact with these details already exists:-" & vbCrLf, vbOKOnly + vbExclamation
Cancel = True 'Cancel the update
End Sub
End If
End If
You can add a global to the form, set that to True in your cancel button, and don't save if it is True
I have a problem with Microsoft access for code I have written to lookup the USER TYPE field in the ADMINSTRATOR TABLE.It is not able to run due to syntax error 3075 missing expression operator in query expression 'USER TYPE'
It is used for my login menu to differentiate between two different user types and bring them to different forms.
The code works before I implemented the User Security code
Dim UserLevel As Integer
UserLevel = DLookup("USER TYPE", "ADMINSTRATOR TABLE", "USER ID = '" & Me.txtLoginID & "'")
Here is my code:
Private Sub Command1_Click()
Dim UserLevel As Integer
If IsNull(Me.txtLoginID) Then
MsgBox "Please enter Login ID", vbInformation, "Login ID Required"
Me.txtLoginID.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else
If (IsNull(DLookup("[User ID]", "ADMINSTRATOR TABLE", "[User ID]='" & Me.txtLoginID.Value & "' And Password = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Incorrect Password or Login ID", vbInformation, "Login unsuccessful"
Else
UserLevel = DLookup("USER TYPE", "ADMINSTRATOR TABLE", "USER ID = '" & Me.txtLoginID & "'")
DoCmd.Close
If UserLevel = 1 Then
MsgBox "Login Success", vbInformation, "Login Success"
DoCmd.OpenForm "MAIN MENU"
Else
MsgBox "Login Success", vbInformation, "Login Success"
DoCmd.OpenForm "ADMINISTRATOR FORM"
End If
End If
End If
End Sub
Field names and table names containing spaces or special characters (poor design choice) need to surrounded with square brackets.
im using access 2007.
My login form has username and password and i want to pass value of the username from login form to menu form. my code as below
'Login Form'
Private Sub Command1_Click()
If IsNull(Me.txtUserName) Then
MsgBox "Please enter Username", vbInformation, "Username Required"
Me.txtUserName.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else
'process login'
If (IsNull(DLookup("[Login_ID]", "Login_Detail", "[Login_ID] ='" & Me.txtUserName.Value & "' And password = '" & Me.txtPassword.Value & "'"))) Or _
(IsNull(DLookup("[Login_ID]", "Login_Detail", "[Login_ID] ='" & Me.txtUserName.Value & "' And password = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Incorrect"
Else
DoCmd.Close
DoCmd.OpenForm "Menu", acNormal, Me.txtUserName
End If
End If
End Sub
i put the code for the menu form
Me.Label4 = "Hi " & Me.OpenArgs & "!"
i would like the menu form automatically detect what is the username. Example
Hi John!!
The error says invalid argument. qWhere is the error in my code?
DoCmd.OpenForm "Menu", acNormal, Me.txtUserName
OpenArgs is not the third argument, it is the seventh, which may account for the error (the third argument is a filter):
DoCmd.OpenForm "Menu", acNormal, , , , , "Bob"
(or use named arguments)
then
MsgBox "Hello " & Me.OpenArgs
will work.
You can just store the username as a variable in your login form and set the message right after you open the menu:
'Login Form'
Private Sub Command1_Click()
Dim un As String
If IsNull(Me.txtUserName) Then
MsgBox "Please enter Username", vbInformation, "Username Required"
Me.txtUserName.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else
un = Me.txtUserName
'process login'
If (IsNull(DLookup("[Login_ID]", "Login_Detail", "[Login_ID] ='" & Me.txtUserName.Value & "' And password = '" & Me.txtPassword.Value & "'"))) Or _
(IsNull(DLookup("[Login_ID]", "Login_Detail", "[Login_ID] ='" & Me.txtUserName.Value & "' And password = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Incorrect"
Else
DoCmd.Close
DoCmd.OpenForm "Menu", acNormal, Me.txtUserName
Forms!Menu!Label4 = "Hi " & un & "!"
End If
End If
End Sub
If it throws an error, move the DoCmd.Close to the end.
You have 2 possibilities.
call a Public sub in your menu form
In your Menu form, create the following sub :
Public Sub Pass_Login_Info(strUserName As String)
Me.Label4 = "Hi " & strUserName & "!"
End Sub
In the Login form, call the sub of the menu form like this :
DoCmd.OpenForm "Menu", acNormal
Form_Menu.Pass_Login_Info un
Use a Global variables to store the user info
It's the simpler and best approach to me because the Username is something that should be known and retrieved throughout all your application.
Just create a module, and in top of it add a public variable, that will be usable from anywhere
Public USER_NAME as string
In your login form :
USER_NAME = un
In the Load event of your Menu form
Private Sub Form_Load()
Me.Label4 = "Hi " & USER_NAME & "!"
End Sub
I've created a form in Access where I have a series of required fields. Once all of the required fields have been completed and the user clicks a button called Add New Record, I need the form to refresh so they can enter new data. I've created this form by using snipits I've found on Stackoverflow. For the most part the form does what I need it to do. (It checks for missing fields, displays error is fields are correct) The piece I'm missing is is won't refresh the page. It's not displaying any errors. Any insight or suggestions would be appreciated. Below is the code I'm currently using.
Thank you
Private Function CheckAllFields() As Boolean
Dim Ctrl As Control
CheckAllFields = False
For Each Ctrl In Me.Controls
If Ctrl.Tag = "*" And IsNull(Ctrl) Then
MsgBox "Please complete ALL required fields"
CheckAllFields = True
Exit For
End If
Next Ctrl
End Function
Private Sub New_Record_Click()
If CheckAllFields = True Then Exit Sub
If PYMT_AMT < AMT_RECOVERED And CONSOLIDATED_CK = "NO" Then
MsgBox "CONSOLIDATED CHECK ERROR" & vbCrLf & _
"The Amount Recovered is greater than the Exception Amount." & vbCrLf & _
" Please confirm answer to Consolidated Check question"
End If
If PYMT_AMT > AMT_RECOVERED And CONSOLIDATED_CK = "YES" Then
MsgBox "CONSOLIDATED CHECK ERROR" & vbCrLf & _
"The Amount Recovered is less than the Exception Amount." & vbCrLf & _
" Please confirm answer to Consolidated Check question"
End If
If PYMT_AMT > AMT_RECOVERED And PARTIAL_RECOVERY = "NO" Then
MsgBox "PARTIAL RECOVERY ERROR" & vbCrLf & _
"The Amount Recovered is less than the Exception Amount." & vbCrLf & _
" Please confirm answer to Partial Recovery question."
End If
If PYMT_AMT < AMT_RECOVERED And PARTIAL_RECOVERY = "YES" Then
MsgBox "PARTIAL RECOVERY ERROR" & vbCrLf & _
"The Amount Recovered is greater than the Exception Amount." & vbCrLf & _
" Please confirm answer to Partial Recovery question."
End If
If RECOVERY_TYP = "Check Payable to Fiserv" And CHECK_NUM = "" Then
MsgBox "Please enter check number." & vbCrLf & _
" AND Please confirm answer to Partial Recovery question."
End If
If Me.RECOVERY_TYP = "Check Payable to Fiserv" Then
If IsNull(Me.CHECK_NUM) Then
MsgBox "Please Enter Check Number"
Cancel = True
Me.CHECK_NUM.SetFocus
End If
End If
End Sub
Seems like you could just add:
Me.DataEntry =True
...before your "End Sub".
I am not sure if your form is bound to a table, but if so, once you have ensured that the existing form data was validated and persisted, could you not just add the below line to your code? This should bring you to a new table record with the form fields empty.
DoCmd.GoToRecord acDataForm, "Your Form Name", acNewRec