First, Access code is written in VBA, not C++, important if you do any google searches.
Next, please post the current button's click event procedure, so we can see how things are setup and offer you a solution to your question.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
In a database which I am working on I want to have a delete record button on a form. I also want this delete record button to close the form once a record is deleted. I only want the form to close if the record is deleted though. Under the design tab, I found a control button and placed it on my form. I then used the button wizard to create a delete record button. By default, when clicked the button comes up with a confirmation message. I only want the form to close if the user selects yes to delete the record. If the user selects no, the form should not close. I have little experience with VBA coding but have a bit of experience with C++. I opened the macro for the button, and found the if statement which contained the delete record command. I assumed that this was the if statement that runs when the user selects yes in the confirmation message that they want to delete the record. I placed a CloseWindow command within that if statement. For whatever reason, this does not work. It closes the window regardless of what I select within the confirmation message. This really is baffling me due to the fact that the DeleteRecord command is not running, but another action within that if statement is running consistently.
Any advice that anyone has to offer would be much appreciated.
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
First, Access code is written in VBA, not C++, important if you do any google searches.
Next, please post the current button's click event procedure, so we can see how things are setup and offer you a solution to your question.
Hi Daniel! Thank you for the help!
Yes, I do realize Access code is written in VBA, I should have elaborated that what I meant by that was I know how if statements work in C++. If a given condition is true, it executes the body of that if statement, if the condition is not true, the if statement
is ignored. I was under the understanding that if statements work nearly the same for all programming languages, and found it strange that in the macro I edited, that only part of an if statement was evaluating. Attached is a screenshot of the macro that
I edited to close the window after a deletion.
In the Close Window Action you do not specify the type or name of the object you want to close.
But frankly, I would do this in a VBA rather than a macro.
Plus generally records aren't delete, they are simply marked inactive.
You'll find an example of a customized record deletion routine, which substitutes your own message for the default system message, as DeleteDemo.zip in my public databases folder at:
https://onedrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169
If you have difficulty opening the link copy its text (NB, not the link location) and paste it into your browser's address bar.
In this little demo file, if you amend the Click event procedure of the Delete button in the form as follows it will close the form on deletion of the current record:
Private Sub cmdDelete_Click()
Const DELETE_CANCELLED = 2501
Const CANT_DELETE = 2046
Const MESSAGETEXT = "Are you sure you wish to delete the current record?"
If Me.Dirty Then
If Me.NewRecord Then
If MsgBox(MESSAGETEXT, vbYesNo + vbQuestion, "Confirm") = vbYes Then
Me.Undo
DoCmd.Close acForm, Me.Name
End If
Exit Sub
End If
End If
On Error Resume Next
RunCommand acCmdDeleteRecord
Select Case Err.Number
Case 0
' no error
DoCmd.Close acForm, Me.Name
Case DELETE_CANCELLED
' anticipated error, so ignore
Case CANT_DELETE
' anticipated error, so ignore
Case Else
' unknown error, so inform user
MsgBox Err.Description & " (" & Err.Number & ")", vbExclamation, "Error"
End Select
End Sub