Access: Canceling Report generation causes error 2501 - ms-access

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...?

Related

VBA Spell Check in O365 (Access 2016) Crashing Access

I ran the following code On Lost Focus event in Access 2010 with no problems.
Dim strSpell
strSpell = txtMyField
If IsNull(Len(strSpell)) Or Len(strSpell) = 0 Then
Exit Sub
End If
With txtMyField
.SetFocus
.SelStart = 0
.SelLength = Len(strSpell)
End With
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdSpelling
DoCmd.SetWarnings True
After upgrading to O365 (Access 2016), the spell check works with no problems unless there's multiple spelling corrections. If the user choose to Change All or change more than 1 word Access crash and the user need to restart the program. I tried to change the code to run On Exit event but it didn't help. I also decompile the database and found no erros.
The only way I can run spell check is using the spell check on the ribbon. I hide the ribbon to prevent the users from messing around so this option is not going to work. Also, I would like to limit spell check to one field and force it after the user exit the field.
I'm guessing that Access is crashing because more than one correction trigger the event again and since spell Check is already running the program crash. Any idea how to fix it ?
UPDATED NOTES FROM COMMENTS
In my testing I found that when you hit Enter several time when you
type in the field, it cause the program to crash.
Text field is LONG TEXT
Form field is bound to text field
I was able to repeat the hanging process "Not Responding" message with a Long Text field and 3500 characters and LOTS of items with 'spelling errors`.
Using your code it would crash with LostFocus code or even if I put it in a command button.
I started wondering if it was the whole text selection that was causing an error with how the spell checker tried to do its job - and whether that was even needed.
When I moved your code out of the LostFocus event and replaced it with this code in the Exit event it worked reliably
EDIT: Moved code to Exit event and specified specific textfield
Private Sub txtMyField_Exit(Cancel As Integer)
With txtMyField
'If the textbox has data run the Spell Checker
If Len(.Value & "") > 0 Then
.SetFocus
.SelStart = 0
.SelLength = Len(.Value)
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdSpelling
DoCmd.SetWarnings True
.SelLength = 0
End If
End With
End Sub

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.

DoCmd.CopyObject throws runtime error 3021

I have a large MS Access 2013 application, and while adding some new logic I've come come upon an obstacle that no amount Googling has allowed me to overcome.
When I run the following code I get a
runtime error 3021: No current record.
error message when executing the last statement:
Docmd.Close acForm,"Picking"
DoCmd.DeleteObject acForm, "Picking"
DoCmd.CopyObject, "Picking", acForm, "Picking On Tablet"
The two forms listed in the code both exist and are proper. The first form (Picking) is being deleted properly in the 2nd statement. (The name has been removed from the list of form objects showing in the nav pane.)
My guess is that I need to somehow refresh the objects list before I run the 3rd statement but can't figure out how to do that. As a lark, I did try DoCmd.RefreshRecord since runtime error 3021 says "no current record" but of course this didn't work.
Am I on the right track? Any other ideas? I'm completely stumped.
You don't mention where your code is located (it can't be in form 'Picking'), but I got the following to work in a Module. You can delete whatever 'RefreshDatabaseWindow' commands you want until you find the culprit...
Option Compare Database
Option Explicit
Sub Do_Something_With_Forms()
RefreshDatabaseWindow
DoCmd.Close acForm, "Picking"
RefreshDatabaseWindow
DoCmd.DeleteObject acForm, "Picking"
RefreshDatabaseWindow
DoCmd.CopyObject , "Picking", acForm, "Picking On Tablet"
RefreshDatabaseWindow
End Sub
Thanks to all for your help. The answers all provided perspective. Based on your feedback I decided to check my work elsewhere. Sure enough, code elsewhere was the source of the issue.
It's times like this when I wish Access would provide stack visibility. What I discovered by hitting F8 some 50-75 times was that I was erroneously executing code to re-establish ODBC links for all my tables, while at the same time essentially, the code in question was manipulating the picking form.
The picking form links to two of the tables and this most likely caused my issue.
After correcting my logic error now the code works fine (yes, it does run in a separate module).

Error on entering data in a form in VBA access

Hello i am using a form in ACCESS to enter new data for a company. First of all the form shows me the current data with a query , and when i want to introduce a new data in the fiels in a line i press Tab many times and when ends it gave me an Error:
the data was added to the database but the data won't be displayed in the form because it
doesn't satisfy
and i close the form and when i open again the form for that company the values that i entered are there...
how can i solve this error , any ideas?
Add some rudimentary error trapping. If your code doesn't already contain it, you really need to read up on it. Any code without some error trapping is just flat out poorly written code.
To start, use a shell like this and modify it appropriately:
Sub|Function SomeName()
On Error GoTo Err_SomeName ' Initialize error handling.
' Code to do something here.
Exit_SomeName: ' Label to resume after error.
Exit Sub|Function ' Exit before error handler.
Err_SomeName: ' Label to jump to on error.
MsgBox Err.Number & Err.Description ' Place error handling here.
Resume Exit_SomeName ' Pick up again and quit.
End Sub|Function
If it hits an error, it will jump to the Err_SomeName: line and then display a messagebox with the error number and description. It will then exit the Sub/Function. What you really want to do is instead of just popping up a messagebox, do something that actually solves the problem. This is what error handling is all about, and it's critical to ensure your program functions exactly the way it's supposed to.

Disabling msgbox in access

I am trying to make a small form in MS Access 2003 SP3, I use some function that some other people made and these function has msgbox in it. I would like to disable msgbox while I am running the form. Is it possible in Access to disable msgbox?
I created my finction called msgbox. Seems like its working. Thanks everyone for your help.
Public Function MsgBox(Prompt, Optional Buttons As VbMsgBoxStyle = vbOKOnly, Optional Title, Optional HelpFile, Optional Context) As VbMsgBoxResult
If myProcedureisRunning then
VBA.MsgBox Prompt
else
debug.print prompt
endif
End Function
If in fact these message boxes are produced from VBA code, then comment them out. However, if they are Access generated, such as the message box when inserting or updating records, you need to use the command DoCmd.SetWarnings False in order to suppress them. Just make sure to turn warnings off only when needed, then turn them back on. Otherwise, ALL message boxes from Access will be off, even in "design mode".
Do a CTRL-F and find for MSGBOX and comment it. I guess that's the only way you can do it.
Press Alt+F11 to open the Visual Basic IDE, then press CTRL+F to search. Type msgbox into the find, select "Replace" and type'msgbox into the "replace with" box (note the apostrophe). This will comment out all msgbox statements in the project.