Record saved confirmation message - ms-access

MS ACCESS: I have a 'save record' button on one of my forms. When clicked, I want it to show a confirmation message box saying something like "Your loan is approved", only if it saves the record without any error messages. Is this possible?

You can use error handling to do what you want if you place your code in an event procedure. When there is an error the message you set in the error handler will be displayed not the record saved message. Basic error handling would look like:
Private Sub SaveButton()
On Error GoTo PROC_ERRORHANDLER
//////Your Save Code//////
MsgBox "Record Saved", vbInformation, "Save"
Exit Sub
PROC_ERRORHANDLER:
MsgBox "Error: (" & Err.Number & ") " & Err.Description, vbCritical
End Sub

Related

How to keep formula in form field when add new record using form in access

I have a form to entry new data to table in Access 2010.
I want Request ID field to be Max(Req_Number)+1 from current table, whenever user add new record and want it to be filled automatically, also Req_Number is the key. When use Data Mode all fields are blank so I wonder how to lock one filed with a function when entry data to a table using form in access?
The RequestID text box should be Locked and Disabled. Add code to the form's Before Insert event as follows:
Private Sub Form_BeforeInsert(Cancel As Integer)
On Error GoTo PROC_ERR
RequestID = Nz(DMax("Req_Number", "myTable"), 0) + 1
PROC_EXIT:
On Error Resume Next
Exit Sub
PROC_ERR:
MsgBox "Error " & Err.Number & " - " & Err.Description
Resume PROC_EXIT
End Sub

MS Access Error '2110' Can't move the focus to the control

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

MS Access 2010 - MsgBox to Run Query & Delete Record Not Working

I have a button on a Continuous subform with the below VBA attached -
Private Sub del_Click()
On Error GoTo del_Click_Err
Dim LResponse As Integer
On Error Resume Next
DoCmd.GoToControl Screen.PreviousControl.Name
Err.Clear
If (Not Form.NewRecord) Then
LResponse = Eval("MsgBox('You are about to delete a record.'" & _
"'#If you click yes, you won't be able to undo this delete operation.' & Chr(13) & Chr(10) & " & _
"'Are you sure you want to delete this record?##', 276, 'Are you sure?')")
If LResponse = vbYes Then
DoCmd.SetWarnings False
DoCmd.OpenQuery "qry_DeleteSpecific"
DoCmd.RunCommand acCmdDeleteRecord
DoCmd.SetWarnings True
Else
End If
End If
If (Form.NewRecord And Not Form.Dirty) Then
Beep
End If
If (Form.NewRecord And Form.Dirty) Then
DoCmd.RunCommand acCmdUndo
End If
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If
del_Click_Exit:
DoCmd.SetWarnings True
Exit Sub
del_Click_Err:
MsgBox Error$
Resume del_Click_Exit
End Sub
The button was originally the auto-generated 'Delete Record' button and I have since modified it to include the query activation as well as my own msgBox.
The 'delete' button obviously worked fine before modification. The query also works as intended and I tested a button on a seperate form that just included the MsgBox code, which also worked.
But bringing it all together has failed somehow. When I click the button I don't get the MsgBox and it doesn't delete a record or run the query.
DoCmd.GoToControl Screen.PreviousControl.Name
This line appears to have fired correctly though.
Could anybody explain what may be going wrong with this?
---Edit---
Replaced
Error Clear
With
On Error GoTo del_Click_Err
I now get the error message -
The expression you entered contains invalid syntax
So it turns out it was all in the Quotes. The quotation issue is still a bit of a mystery to me, but I managed to get my code working.
Here's the change -
LResponse = Eval("MsgBox(""You are about to delete a record."" & " & _
"""#If you click yes, you won't be able to undo this delete operation."" & Chr(13) & Chr(10) & " & _
"""Are you sure you want to delete this record?##"", 276, ""Are you sure?"")")
Single quotes seemed to work when it was just the msgBox code by itself but not in the extended code. Anyway, I'll know to look out for this again.
#Andre your comment helped me debug this. I initially left 'Error Clear' as that was what the wizard put in for the delete button.

"No Current Record" error on acCmdSaveRecord when BeforeUpdate is cancelled

I'm using RunCommand acCmdSaveRecord to save a record, and I'm trying to use the form's BeforeUpdate event to validate certain fields that I don't want to be left blank before the record is saved.
I'm a bit new to using BeforeUpdate so I'm not sure if I'm using it correctly; here's my BeforeUpdate code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.[First Name]) Or IsNull(Me.LastName) Or IsNull(Me.DateOfBirth) Or IsNull(Me.CourseStartDate) Then
MsgBox "Before the enrolment can be saved, you must provide:" & vbCrLf & vbCrLf _
& "- First Name" & vbCrLf _
& "- Last Name" & vbCrLf _
& "- Date of Birth" & vbCrLf _
& "- Start Date" & vbCrLf & vbCrLf _
& "You must also attach a course. This can be done by selecting " _
& "the appropriate course in the Prospectus Search and clicking " _
& "the Use Prospectus Code button." & vbCrLf & vbCrLf _
& "If your course is not currently in the prospectus, you can " _
& "add it first by clicking the Add Course button.", vbOKOnly Or vbInformation
Cancel = True
Else
Me!EnrolmentID = "Enr" & Format(Me!ID, String(12 - Len("Enr"), "0"))
End If
End Sub
So this basically tests to see if certain fields have been left blank, if they have been left blank then a message box is displayed showing what fields need data and then the update is cancelled. Otherwise it assigns a custom primary key and allows the record to update.
It's throwing an error when cancelling the update though stating "No Current Record" and highlighting the RunCommand acCmdSaveRecord line of code.
What do I need to do to avoid the error? I've also tried Me.Dirty = False, but still get the same error.
#Johnny Bones:
Here's a simplified test I've tried on the answer you gave me:
Public ShouldRun As Boolean
Private Sub Command7_Click()
If ShouldRun = True Then
MsgBox ShouldRun
RunCommand acCmdSaveRecord
End If
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.Field1) Or IsNull(Me.Field2) Then
MsgBox "This is the true"
ShouldRun = False
Else
MsgBox "This is the false"
ShouldRun = True
End If
End Sub
Basically none of the MsgBox functions are firing and the record is being saved regardless of whether a field is being left blank or not. I tried this as a test because the MsgBox in my real code was not firing in the BeforeUpdate event either. Can't see why though.. if a user leaves one of the fields blank this should trigger the true part of the if statement in the BeforeUpdate event, right?
The only thing I can think of is that the BeforeUpdate event is not being triggered by RunCommand acCmdSaveRecord, despite MSDN saying:
"if you then move to another record or save the record, the form's BeforeUpdate event does occur."
Not sure what the order is here... does VBA run the RunCommand acCmdSaveRecord then the BeforeUpdate event? If this is the case then the ShouldRun variable won't have any assignment at the first step (RunCommand acCmdSaveRecord) because it only gets that at the second (BeforeUpdate).
I would set a global variable like this:
Global ShouldRun as Bool
You throw that at the very top of the code page, up where you declare Options Explicit. Then, in the If/Then/Else statement you provided above, replace the line:
Cancel = True
with
ShouldRun = False
and in the Else part add the line
ShouldRun = True
Finally, your command button's OnClick event should look more like this:
If ShouldRun = True then
RunCommand acCmdSaveRecord
End If
By doing this, it will never even try to save the record if it shouldn't.
I know it's late, but have you tried to use DoCmd.RunCommand acCmdSaveRecord
instead?
Because for me it's firing the Before Update event well, when RunCommand acCmdSaveRecord wasn't!
But after cancelling the event you may get an execution error '2759', because the record can not be save anymore.
So you can handle it this way :
Private Sub Command7_Click()
On Error GoTo Command7_Click_Error
DoCmd.RunCommand acCmdSaveRecord
Command7_Click_Exit:
Exit Sub
Command7_Click_Error:
If Err.Number=2759 Then
'ignore the error
Else
MsgBox Err.Description
End If
Resume Exit_Here
End Sub
If you are using Me.Dirty = False instead, you can handle the error '2101'.
Hope it helps

Handling an output error in Access

I'm generating a query and report through VBA. I have an option of asking the user if they want to output the report as a snapshot. I first ask them if they want to make a snap shot. If they say no, nothing happens. If they say yes, they get a prompt asking where they want to save it.
Everything works great except if they say yes and then click Cancel on the prompt, it raises a runtime error 2501 saying the report action was cancelled. Here is the code.
DoCmd.OpenReport "CONCERNS", acViewPreview, lstFee.Value & " DETAILS"
If MsgBox("Do you wish to create a snapshot of this report?", vbQuestion + vbYesNo) = vbYes Then
DoCmd.OutputTo acReport, "CONCERNS", "SnapshotFormat(*.snp)", ""
End If
This is also the end of my procedure so I don't really care if an error happens here since all the important stuff happened already. I just know some monkey somewhere will flip if they ever see it. Is there a way to handle this error? On Error Resume Next is not an option because that would make debugging a nightmare in the future. It sounds like I'm looking for something like a Try/Catch but I don't think VBA supports that.
On Error GoTo errHandler
....
Exit Sub
errHandler:
If (Err.Number = 2501) Then
Resume Next
End If
End Sub
All you need is to handle the error ie:
On Error Goto HandleErr
DoCmd.OpenReport "CONCERNS", acViewPreview, lstFee.Value & " DETAILS"
If MsgBox("Do you wish to create a snapshot of this report?" _
, vbQuestion + vbYesNo) = vbYes Then
DoCmd.OutputTo acReport, "CONCERNS", "SnapshotFormat(*.snp)", ""
End If
ExitHere:
Exit Sub 'or Function
HandleError:
Select Case Err.Number
Case 2501 'Report Was Cancelled
If MsgBox ("Did you really want to cancel saving the report?", _
vbYesNo + vbDefaultButton2 ,"Please Confirm") = vbNo then
Resume
Else
Resume ExitHere
End if
Case Else
Msgbox "An Unexpected error Occurred " & Err.Description, _
vbExclamation,"Error"
Resume ExitHere
End Select
This will give the user the option to undo the cancel and let them know what they did.
There are (at least) two ways to handle this.
1> get the filename and handle possible cancellation in a step before sending the report snapshot. I haven't done this recently but there's another way to generate the snapshot report than the DoCmd.OutputTo command, or some variation that doesn't require the command itself to use a file dialog. I generated report snapshots in an old application and didn't have to ask the user for a filename. I'll try to find the code and show an example.
2> use On Error Resume Next, but only right before the DoCmd.OutputTo routine, then see if there's an error, then turn it back off:
If MsgBox("Do you wish to create a snapshot of this report?", _
vbQuestion + vbYesNo) = vbYes Then
On Error Resume Next
DoCmd.OutputTo acReport, "CONCERNS", "SnapshotFormat(*.snp)", ""
if Err.Number = 2501 Then
'' log or ignore error
Else
'' log or warn other unexpected errors
End If
On Error Goto 0
End If