PDA

View Full Version : Trying to cancel ADD Record in form



bdsii
03-01-2011, 09:27 AM
Hello all, using Access 2007....I have a form for the user to input data. There is a cancel button on the form and I have disabled the Close button to force using the cancel button.

The first two fields for entry are combo boxes for which I have used code to require an entry. The code is below and is basically the same for both combo boxes.

Private Sub OrigIDCombo_Exit(Cancel As Integer)
If Me.OrigIDCombo.ListIndex = -1 Then
MsgBox "Please select Name"
Me.OrigIDCombo.SetFocus
Cancel = True
End If
End Sub


The problem is the cancel button will not work when the user has not selected anything in this combo box. I keep getting the message to select a Name from the combo box. This seems to happen only when the combo boxes have focus.

What adjustments should I make in the code to allow the cancel to work properly even if nothing has been selected in either of the combo boxes ?



Second question - what code works best for the Cancel button to cause the Add Record to end without adding the record, even if a portion of the data has been supplied by the user and then the cancel button is clicked ? I have had a couple of instances where after some data is entered and the cancel button is clicked I still get a record added with the partial data. i think through correct VBA, I could prevent that. Currently I am using the code below for lack of anything better to use


DoCmd.RunCommand acCmdUndo
DoCmd.Close acForm, "frmEntry"



Any advice would be appreciated.

Thanks !

OBP
03-03-2011, 03:44 AM
When you manually cancel a record you need to press the Escape key twice, once for the field entry and once for the record entry.
So maybe a second Undo command would work.

To control the entry of the Combos you need some code in the Form's Before Update event to check them, because unless they move in and out of the combo your code will not be activated.

bdsii
03-03-2011, 01:28 PM
Thanks for the suggestion OBP. I tried the second Undo and it didn't work. But I think I found the solution to the problem cancelling the add. In searching more abou this, I found the Me.Undo command that causes the form not to be Dirty and then I run the close Event macro which does not cause a record to be inadvertantly added.

I have also found this Me.Undo command to be useful when I going through records and some I edit and save and possibly the next I get to I hit Cancel. Before using the Me.Undo command, the record I first saved would be changed back to the original data by the RunCommand Undo macro.

Now on to my other question....any idea how to be able to cancel an add if the combo box is empty as described in the original post ? I used a work around but would really rather be allowed to cancel out of the code ?




Private Sub OrigIDCombo_Exit(Cancel As Integer)
If Me.OrigIDCombo.ListIndex = -1 Then
MsgBox "Please select Name"
Me.OrigIDCombo.SetFocus
Cancel = True
End If
End Sub