Handling Error 2001 on Cancel - MS Access 2016 - ms-access

I have a form that I am requerying on the click of a button using
DoCmd.Requery
This works and I am able to requery the form based on the criteria I specified in my query table. However, if I cancel the requery, rather than click OK, I get an error (2001). How can I have the form remain as is if the user cancels?
Thanks!

Does the following work?
On Error Resume Next
DoCmd.Requery
On Error GoTo 0

Related

Solution for ms access invalid IDA error 7879

I've made an Access database with a connection to a DWH table. A form with 3 drop-down boxes is made for the user to select the correct value. But the first time a drop-down value is selected the user receives an error (in Dutch). 'An invalid IDA has been passed to Microsoft Access error handling. Please report this error and the steps that caused it.'
After clicking 'Ok' the error doesn't show up when selecting a value form the second drop-down box (and the third) in the same form. Only when the user selects this form again the error pops up when selecting the first drop-down value.
The only VBA code linked to this form is:
Option Compare Database
Private Sub Form_Load()
Me.AantalVanCONTRACT_NUMBER.ColumnWidth = 1.556 * 1440
End Sub
The strange thing is that the form works fine because the selected value gets inputted in the DWH table, but getting this error every time is pretty annoying.

How Can I fix An Access 2010 "You Can't go to specified record" Error

I have inherited an Access 2010 Database and have a problem with a form that stopped working after the backend was migrated to SQL server.
The form is pretty simple it is a subform that opens and shows a grid for comments that have been entered. There is a "Add New" button on the form that should allow the user to insert a new comment. the code for that button looks like this:
Private Sub cmd_new_comment_Click()
On Error GoTo Err_cmd_new_comment_Click
DoCmd.GoToRecord , , acNewRec
DoCmd.GoToControl ("com_date")
Me.ActiveControl = Now()
DoCmd.GoToControl ("com_comments")
Exit_cmd_new_comment_Click:
Exit Sub
Err_cmd_new_comment_Click:
MsgBox Err.Description
Resume Exit_cmd_new_comment_Click
End Sub
When I click the "Add New" button I get the Error Message "You Can't go the the specified record".
I believe the data is not the problem because when I open the comments form I can see all the previous comments it is only an issue when I want to add a new comment.
was the PK field in the Access table type autoNumber? If so, you'll need to set up an identity specification in the design of the table on the SQL Server.

Issue with Re Querying Underlying Data for ComboBox

I'm having an issue refreshing the query that underlies a combobox in a form named 'Site'. What I'm attempting to have happen is for a user to be able to enter a staff member in a form called 'Staff'and then on save have 'Staff' quit, the user be taken back to 'Site' and have the recently entered data be available in the combobox that will be informed by a query based partly upon the information received through 'Staff'.
Everything works so far except I have been unable to properly refresh 'Site' or the particular control being affected; Site.OfficeContactId. I'm using a macro but I converted to VBA for the posting.
Function Macro2()
On Error GoTo Macro2_Err
With CodeContextObject
On Error Resume Next
DoCmd.RunCommand acCmdSaveRecord
If (.MacroError <> 0) Then
Beep
MsgBox .MacroError.Description, vbOKOnly, ""
End If
DoCmd.Close acForm, "Staff"
DoCmd.Requery "Forms!Site.Controls!OfficeContactId"
End With
Macro2_Exit:
Exit Function
Macro2_Err:
MsgBox Error$
Resume Macro2_Exit
End Function
As I understand Do.CmdRequery "Forms!Site.Controls!OfficeContactId" should do the trick but it's not working for me.
Any suggestions?
Thanks
Removed ancillary code that inhibited debugging. Truncated to 3 commands
DoCmd.RunCommand acCmdSave Record
DoCmd.Close acForm "Staff"
DoCmd.Requery "Forms!Site.Controls!OfficeContactId"
Now working to resolve a known issue where the requery command won't run in a non visible form, debugging code to call the form forward prior to the execution of requery.

Add new record to the table by click on the button in Microsoft Access

In my MS Access form I want to implement a separate button, that adds a new record to the table. In order to do that I added a button and attached to this button an event:
Private Sub btnAddRec_Click()
Refresh
With CodeContextObject
On Error Resume Next
DoCmd.GoToRecord , , acNewRec
If Err.Number <> 0 Then
btnAddRec.Enabled = False
End If
End With
End Sub
Everything is OK when you just open the window and click the btnAddRec button, the problem is when you first of all perform the navigation through existed records and only after that click on this button. I got the runtime error:
2105: «You can't go to the specified record. You may be at the end of a recordset».
How to solve the issue? I need to have ability to add the new record on click on specific button, no matter, have I walked or not walked through the records before.
I created a simple form with a field call Description (and AutoNumber) and created a button with the code that follows click event. I filled it with a number of records and navigated through them, then clicked the addNewRec button. The form navigated to the new record without issues. I was also able to click the addNewRec button directly after opening the form successfully.
Private Sub btnAddRec_Click()
On Error GoTo Err1
DoCmd.GoToRecord , , acNewRec
Exit Sub
Err1:
Description.SetFocus
btnAddRec.Enabled = False
MsgBox (Err.Description)
End Sub
The differences from the code you included were removing the refresh and With statement, handing the error, setting focus before disabling the button and showing the user the error description. I do not know if your form is similar, but this should work for you if it is and I understood your problem correctly.

Access: Canceling Report generation causes error 2501

can't believe I am losing so much time on this one.
I have an order form, and when I click on a button "reports", a dialog
pop ups with a list of different reports to chose from. Double-clicking selects
and starts the correspondent report.
On one of these reports, there is an unbound text box I need the user to enter data with.
The ControlSource of this field is set to its Name property.
When the report is started, an input box appears with an OK and a Cancel button. Whenever I enter some data, all is fine.
But when I click on Cancel, the app crashes and I get an errormessage:
"Runtime Error 2501: The Action OpenReport has been canceled" (translated from German).
The Report is called through this code:
DoCmd.OpenReport vBerichtName, nAnsicht
End If
On Error Resume Next
DoCmd.Close acForm, "F_BerichtDrucken"
On Error GoTo 0
1) Why does the error handling not kick in?
2) I googled and found lots of weird solutions for this, like the official Microsoft one saying you need to install/update a printer driver (come on...). None helped.
I am doing this for a friend and I normally work on linux/php,java, etc. I apologize if the solution is somewhat obvious or something like that.
Ditto to Phillipe's answer. you didn't give us the whole procedures but you need to do something like this...
Sub MyButton_Click
On Error Goto myError
DoCmd.OpenReport vBerichtName, nAnsicht
MyExit:
Exit Sub
MyError:
If Err.number = 2501 then goto myExit
msgbox err.description
goto myExit
End Sub
This is a common error but you can catch it like any other error and ignore it if is 2501.
Seth
The error probably comes from the DoCmd.OpenReport line. This is why the error handler does not work.
I guess the value you requested is somehow mandatory in the report. Did you try to put your error management line before the docmd.openReport?
Check your default printer. I was getting the same error & all my code was working properly. Someone using the computer set the default printer to a label printer. Somehow Access was checking the printer & knew it didn't have the correct size settings to print so it stopped the displaying of the report.
I changed the default printer to another full size sheet printer and it worked fine.
I hope this helps someone because I was at a loss when it first occurred.
OK, it works now.
After your suggestions, I put the code like this:
On Error GoTo CancelError
If Not IsNull(vFilter) Then
DoCmd.OpenReport vBerichtName, nAnsicht, , vFilter
Else
DoCmd.OpenReport vBerichtName, nAnsicht
End If
CancelError:
DoCmd.Close acReport, vBerichtName
DoCmd.Close acForm, "F_BerichtDrucken"
Echo True ' this did the trick
Exit Function
As soon as I put Echo True into the error handling, it works
now smoothly, going back to the previous form and allowing
to continue to work - looks like "Echo" is kind of a refresher for the screen...?