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
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)
'Check to see if the recordset actually contains rows
If Not (recordSet.EOF And recordSet.BOF) Then
recordSet.MoveFirst 'Unnecessary in this case, but still a good habit
'See if credentials match data
Do
If (recordSet!User_ID = PERSAL And recordSet!Password = Password) Then
'Open Menu form
DoCmd.OpenForm "Menu", acNormal, "", "", , acNormal
recordSet.Close 'Close the recordset
Set recordSet = Nothing 'Clean up
'Close Login form
DoCmd.Close acForm, "Login"
Exit Do
End If
recordSet.MoveNext
If (recordSet.EOF Or recordSet.BOF) Then
MsgBox "Your credentials are incorrect or you are not registered."
Exit Do
End If
Loop
'Match the values entered for PERSAL nr. and password fields with a row in User table
Else
MsgBox "There are no records in the recordset."
recordSet.Close 'Close the recordset
Set recordSet = Nothing 'Clean up
End If
Form_Login.txtUser_ID.SetFocus
I have tried every solution I've found for the past 2 days but nothing has worked to solved my error 2501:
I did:
Decompiling
Compact and repair
Tackling the MSCOMCTL.OCX file
Importing to a new access file
I am spent.
For the MSCOMCTL.OCX file I'm thinking to download a 2007 Office update SP 3.
The exception/error appears on this line:
DoCmd.OpenForm "Menu", acNormal, "", "", , acNormal
Your syntax is not correct. Try:
DoCmd.OpenForm "Menu", acNormal, , , , acWindowNormal
I'm a beginner using MS Access 2016.
I have a save button on a form that does some last minute input validation that is meant to be very direct and helpful to the end-user. I'm using a series of textboxes to step through the validation when they click the save button, this way they can complete then entire form and if there is an error it takes them directly to that field with the error.
My code appears to work,
it detects an error,
sets the focus to the text179,
sees there is an error in the JobNo field,
gives a message saying "Please enter a 5 digit job number"and
sets the focus to JobNo.
Then and only then, do I get a second prompt saying there is an error.
But why is it saying it can't set the focus to text179 when it clearly already has done so and should no longer be trying at that point?
Here is my code:
Private Sub SaveRecord_GotFocus()
If Me.JobNo & "" Like "#####" And Me.ItemNo & "" <> "" Then
Exit Sub
Else
Me.Text179.SetFocus
End If
End Sub
Private Sub Text179_GotFocus()
If Me.JobNo & "" Like "#####" Then
Me.Text181.SetFocus
Exit Sub
Else
MsgBox "Please enter a 5 digit Job Number", vbOKOnly
Cancel = True
Me.JobNo.SetFocus
Exit Sub
End If
End Sub
Error:
Run-time error '2110':
Microsoft Access can't move the focus to the control Text179.
Let me know if any additional information is needed.
I was able to solve this with a much cleaner method, using just VB code without trying to use setfocus. It just took me a while to figure out how to nest my if statements correctly since I'm new with Access/VB. I'll post the code below.
Private Sub SaveRecord_GotFocus()
If Me.JobNo & "" Like "#####" Then
GoTo Eval_ItemNo
Else
MsgBox "Please enter a 5 digit Job Number", vbOKOnly
Me.JobNo.SetFocus
Exit Sub
End If
Eval_ItemNo:
If Me.ItemNo & "" <> "" Then
GoTo Eval_QtyFinished
Else
MsgBox "Please enter an Item Number", vbOKOnly
Me.ItemNo.SetFocus
Exit Sub
End If
Eval_QtyFinished:
If Me.QtyFinished & "" <> "" Then
GoTo Eval_WorkCenter
Else
MsgBox "Please enter a Qty Finished", vbOKOnly
Me.QtyFinished.SetFocus
Exit Sub
End If
Eval_WorkCenter:
If Me.WorkCenter & "" <> "" Then
Exit Sub
Else
MsgBox "Please enter a WorkCenter", vbOKOnly
Me.WorkCenter.SetFocus
End If
End Sub
I can click once to launch the following Event Procedure, but if I return to the form to click twice, Access locks up to the point I have to kill the task in TaskManager?
Any advice on this one?
Private Sub Command372_Click()
On Error GoTo ErrorHandler
gTroop = Str(Me.Troop)
DoCmd.SendObject acSendReport, "GirlScoutandTroopMatchGlobalVariable", "PDF", Me.TroopLeaderEmailAddress
MsgBox "Message Sent Successfully."
Cleanup:
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 2501
MsgBox "Email message was Cancelled."
Case Else
MsgBox Err.Number & ": " & Err.Description
End Select
Resume Cleanup
End Sub
This is my first attempt to create a login form. I've read up a few forums about it and tried them myself. However, I've encountered the error when trying out the form.
"Run time error '2001': You canceled the previous operation."
Here's my code! The error highlighted is the DLOOKUP statement. When I move my cursor to LanID, it appears to be 0. (I guess it got something to do with it?)
Option Compare Database
Option Explicit
Private intLoginAttempts As Integer
Private Sub cmdLogin_Click()
'Check mandatory fields
If IsNull(Me.txtLanID) Or Me.txtLanID = "" Then
MsgBox "Lan ID is required.", vbOKOnly, "Required Data"
Me.txtLanID.SetFocus
Exit Sub
End If
If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
MsgBox "Password is required.", vbOKOnly, "Required Data"
Me.txtPassword.SetFocus
Exit Sub
End If
'Compare input password and database password
If Me.txtLanID <> "" Then
If Me.txtPassword = DLookup("Password", "tblUser", "[LanID]=" & Me.txtLanID) Then
LanID = Me.txtLanID
'Close Login Page
DoCmd.Close acForm, "frmUserLogin", acSaveNo
'Check whether user is an admin
If Me.txtAdmin = DLookup("Administrator", "tblUser", "[LanID]=" & Me.txtLanID) Then
If Me.txtAdmin = -1 Then
DoCmd.OpenForm "frmMenu"
Else
DoCmd.OpenForm "frmBack"
End If
End If
Else
MsgBox "Invalid Password. Try again.", vbOKOnly, "Invalid Entry!"
Me.txtPassword.SetFocus
End If
End If
'If user enters incorrect password 3 times
intLoginAttempts = intLoginAttempts + 1
If intLoginAttempts = 3 Then
MsgBox "You do not have access to the database. Please contact system administrator.", vbCritical, "Restricted Access!"
Application.Quit
End If
End Sub
If tblUser.LanID is text data type, enclose Me.txtLanID in quotes to avoid an error with your DLookup() expression. (You don't need .Value there because it's the default property.)
DLookup("Password", "tblUser", "[LanID]='" & Me.txtLanID & "'")
If that was not the explanation, test your DLookup() expression in the Immediate window (go there with Ctrl+g) to see whether it throws an error or what value it does return.
Note, when you test DLookup() in the Immediate window, use the current value of Me.txtLanID. For example, if Me.txtLanID contains the text "foo", test it like this ...
? DLookup("Password", "tblUser", "[LanID]='foo'")