Error Handling MS Access - ms-access

Hi what I am trying to achieve is saving a record from a form with error handling:
MSGBOX "Success! Do you want to add a new record?" [Yes/No] form is saved.
New record, clear field Me.Serial
MSGBOX "Something went wrong, please complete relevant fields"
What i have tried so far is:
On Error GoTo Err_cmdCloseForm_Click8
DoCmd.RunCommand acCmdSaveRecord
MsgBox "item successfully checked out"
DoCmd.Close
Exit_cmdCloseForm_Click:
Exit Sub
Err_cmdCloseForm_Click8:
MsgBox "Save was unsuccessful, please try again", vbInformation, "Warning"
Resume Exit_cmdCloseForm_Click
Field [Serial] is required, yet when i click save with no information entered into any field i receive a success message. Any thoughts or links would be greatly appreciated.
Regards
J

Use the BeforeUpdate event to validate and possibly to prevent an update, and the AfterUpdate event to display a confirming message.

Related

When click Cancel button on form, do not run On Exit code on a field

On my form, the first field has to be entered, so I have an On Exit command. Since it also checks to see if the value exists in my table, I want it to check right away - is it blank (thus require data to be put into field) OR does it already exist. My question has to do with the first part - is it blank.
On this form, I also have a Cancel button. When the user click Cancel, I want it to close the form without saving any of the data.
Problem - If the user doesn't enter anything in the first field and clicks Cancel, it's running the On Exit code requiring data to be entered into the first field - which they don't have to because they are cancelling. I'm getting a Run-time error '2585'.
Is there anyway to have the Cancel code stop the On Exit code from running? Any other ideas?
Here is my code for Cancel:
Private Sub CancelFormButton_Click()
Me.Undo
DoCmd.OpenForm "fmuMainMenu"
DoCmd.Close acForm, "frmVetNewMainForm"
End Sub
Here is part of my On Exit code for the field:
If IsNull(Me.txtSSN) Then
strMsg = "Social Security Number Must Not Be Left Blank!" & vbCrLf
strMsg = strMsg & "Do you want to add new veteran's record?" & vbCrLf
If MsgBox(strMsg, vbQuestion + vbYesNo, "Go to Record?") = vbYes Then
Me.txtSSN.SetFocus
Exit Sub
Else
Me.Undo
DoCmd.OpenForm "fmuMainMenu"
DoCmd.Close acForm, "frmVetNewMainForm"
End If
Else
'RUNS THE REST OF CODE
End if
OnExit events occur everytime you leave the textbox, even a simple tab or click to go to another combo box or select some text will trigger it.
You may want to try the code in the AfterUpdates.
and try a ,nosave after the close form command...
In addition, any checks to ensure a field is filled/has data is usually encouraged to be performed in the Form_BeforeUpdate section... Or so I have been advised

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.

What VBA event is triggered when closing a form?

If I don't fill out all the required fields in my form, I get an ugly error when I click the "x" in the upper right corner.
I would like to override the default error with a custom error message, but I can't figure out which VBA event to associate this code with. the Form_Close event doesn't seem to work when I put an error handler in there.
Access 2010
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_Unload ' Initialize error handling.
'insert routine
Exit_Unload: ' Label to resume after error.
Exit Sub ' Exit before error handler.
Err_Unload: ' Label to jump to on error.
'MsgBox Err & " " & Error$ ' Place error handling here.
Resume Exit_Unload
End Subenter code here
Still receiving errors even while using this code.
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_Unload ' Initialize error handling.
' Code to do something here.
Exit_Unload: ' Label to resume after error.
Exit Sub ' Exit before error handler.
Err_Unload: ' Label to jump to on error.
'MsgBox Err & " " & Error$ ' Place error handling here.
Resume Exit_Unload
End Sub
When you attempt to close a form which has unsaved changes to the form's current record, Access will first attempt to commit those changes. You can't intercept that action from Form_Unload or Form_Close.
BTW, although you didn't mention it, I would expect you to see the same thing when navigating to a different record if the record you're navigating away from includes unsaved changes with missing values for required fields.
In the case where your form's record source includes a table with an autonumber field, use the form's before update event to check whether required values are present. If there is no autonumber field involved, consider doing the checking from both the form's before insert and before update events.
However if the whole point of this is to get a friendlier message for a missing value in a required field, see whether changing the table's properties is satisfactory. For example, you could set Required to No, then use Is Not Null for the field's Validation Rule which would allow you to assign your friendly text message as the field's Validation Text property.
From http://office.microsoft.com/en-us/access-help/order-of-events-for-database-objects-HP005186761.aspx,
Similarly, when you close a form, the following sequence of events
occurs:
Exit (control) → LostFocus (control) → Unload (form) → Deactivate
(form) → Close (form)
If you've changed data in a control, the BeforeUpdate and AfterUpdate
events for both the control and the form occur before the Exit event
for the control.
What's likely causing your error is the BeforeUpdate event when the form tries to save your changes, but fails because you haven't filled in required fields. You can create some validation code and run it in the BeforeUpdate event for the form to make sure everything is OK and send the appropriate message to the user.
You might like to use:
Private Sub Form_Unload(Cancel As Integer)
End Sub
There is actually a form event called On Error, which has two arguments: what the error is and what response Access should perform.

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