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)
Related
This code was running without a hitch, but now getting Error 2585.
I have looked at Gustav's answer and Gord Thompson's answer but unless I am missing something (quite possible!) the first does not work and the second seems inapplicable. I saw on another site a suggestion that there might be a duplicate record ID, but I check for that possibility.
I put a call to DoEvent() in response to this error but it returns zero. I also wait for 10 seconds to let other processes run. Still receive the error.
Private Sub SaveData_Click()
Dim myForm As Form
Dim myTextBox As TextBox
Dim myDate As Date
Dim myResponse As Integer
If IsNull(Forms!Ecoli_Data!DateCollected.Value) Then
myReponse = myResponse = MsgBox("You have not entered all the required data. You may quit data entry by hitting 'Cancel'", vbOKOnly, "No Sample Date")
Forms!Ecoli_Data.SetFocus
Forms!Ecoli_Data!Collected_By.SetFocus
GoTo endOfSub
End If
If Me.Dirty Then Me.Dirty = False
myDate = Me.DateCollected.Value
Dim yearAsString As String, monthAsString As String, dayAsString As String, clientInitial As String
Dim reportNumberText As String
reportNumberText = Me!SampleNumber.Value
Debug.Print "reportNumberText = " & reportNumberText
Debug.Print "CollectedBy Index: " & Me!Collected_By & " Employee Name: " & DLookup("CollectedBy", "Data_Lookup", Me.Collected_By)
Dim whereString As String
whereString = "SampleNumber=" & "'" & reportNumberText & "'"
Debug.Print whereString
On Error GoTo errorHandling
DoCmd.OpenReport "ECOLI_Laboratory_Report", acViewPreview, , whereString
DoCmd.PrintOut
DoCmd.Close acReport, "ECOLI_Laboratory_Report", acSaveNo
Dim eventsOpen As Integer
eventsOpen = DoEvents()
Debug.Print "Number of Open Events = " & DoEvents()
Dim PauseTime, Start, Finish, TotalTime
PauseTime = 10 ' Set duration.
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Loop
Finish = Timer ' Set end time.
TotalTime = Finish - Start ' Calculate total time.
myResponse = MsgBox("Processing Report Took " & TotalTime & " seconds.", vbOKOnly)
myResponse = MsgBox("Do you want to add more data?", vbYesNo, "What Next?")
If myResponse = vbYes Then
DoCmd.Close acForm, "ECOLI_Data", acSaveYes
Error Generated By Line Above and occurs whether response Y or N to MsgBox.
DoCmd.OpenForm "ECOLI_Data", acNormal, , , acFormAdd
DoCmd.GoToRecord , , acNewRec
Else
DoCmd.Close acForm, "ECOLI_Data", acSaveYes
End If
Exit Sub
errorHandling:
If Err.Number = 2501 Then
myResponse = MsgBox("Printing Job Cancelled", vbOkayOnly, "Report Not Printed")
ElseIf Err.Number = 0 Then
'Do nothing
Else
Debug.Print "Error Number: " & Err.Number & ": " & Err.Description
myResponse = MsgBox("An Error occurred: " & Err.Description, vbOKOnly, "Error #" & Err.Number)
End If
If Application.CurrentProject.AllForms("ECOLI_Data").IsLoaded Then DoCmd.Close acForm, "ECOLI_Data", acSaveNo
If Application.CurrentProject.AllReports("ECOLI_Laboratory_Report").IsLoaded Then DoCmd.Close acReport, "ECOLI_Laboratory_Report", acSaveNo
endOfSub:
End Sub
Any idea on what am I missing here? Thanks.
I can't replicate the problem, but the following might help:
I assume you run into troubles because you're closing and opening the form in the same operation. To avoid doing this, you can open up a second copy of the form, and close the form once the second copy is open. This avoids that issue.
To open a second copy of the form:
Public Myself As Form
Public Sub CopyMe()
Dim myCopy As New Form_CopyForm
myCopy.Visible = True
Set myCopy.Myself = myCopy
End Sub
(CopyForm is the form name)
To close a form that may or may not be a form created by this function
Public Sub CloseMe()
If Myself Is Nothing Then
DoCmd.Close acForm, Me.Name
Else
Set Myself = Nothing
End If
End Sub
More information on having multiple variants of the same form open can be found here, but my approach differs from the approach suggested here, and doesn't require a second object to hold references and manage copies.
This line of code
`DoCmd.Close acForm, "ECOLI_Data", acSaveYes`
doesn't save the record you are on, it just saves any changes to the form design.
You should probably use
If Me.Dirty Then Me.dirty = False
to force a save of the current record if any data has changed.
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
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 have a button on a form that opens a report. The report open as it should whenever there is data however when there is no data in the report I get an error and the debugger takes me to this line
DoCmd.OpenReport "FilesToBeReturnedReport", acViewReport, "", "", acNormal
Here is the entire code:
`With Me
If ((IsNull(.StartDate) Or .EndDate = "")) Then
Beep
MsgBox "You must enter report start period", vbOKOnly, "Required Data!"
ElseIf (IsNull(.EndDate) Or .EndDate = "") Then
Beep
MsgBox "You must enter report end date", vbOKOnly, "Reqired Data"
ElseIf (.StartDate > .EndDate) Then
Beep
MsgBox "The report start date must be earlier than the end date", vbOKOnly, "Required Data"
Else
DoCmd.OpenReport "FilesToBeReturnedReport", acViewReport, "", "", acNormal
End If
End With`
I need to complete this by tomorrow so any help will be much appreciated. Thanks
Access reports have a NoData event. You can put the actions you want to run in the case of no data there. If you set Cancel to True inside the NoData event, the report will not open or print. If you leave Cancel as its default False, the report will open as it does now.
That said, you also need to catch this error when using DoCmd.OpenReport:
On Error Goto ReportFail
DoCmd.OpenReport strReport, acViewPreview
ExitHere:
Exit Sub
ReportFail:
If Err=2501 Then
'OpenReport was canceled
Err.Clear
Resume ExitHere
Else
MsgBox Err.Number & ": " & Err.Description
End If
End Sub
I'm not really extremely experienced with vba in access but why don't you try puttting
DoCmd.OpenReport "FilesToBeReturnedReport", acViewReport, "", "", acNormal
after the
End with