Cancel button error message

  • Thread starter Thread starter Tony Williams
  • Start date Start date
T

Tony Williams

I have an unbound form that is used to input parameters for a query. On the
form I have a Cancel button in the event the user changes their mind. The
code on the Onclick property is
Private Sub cmdcancel_Click()
On Error GoTo Err_cmdcancel_Click


DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70

Exit_cmdcancel_Click:
Exit Sub

Err_cmdcancel_Click:
MsgBox Err.Description
Resume Exit_cmdcancel_Click
End Sub

However when I click on the button to cancel the input I get an error
message that says
Command or action 'Undo' isn't available now. Is this because the form is
unbound and if so what code can I use to cancel the input?
Thanks
Tony
 
Yes, it's because the form is unbound - Undo undoes changes to a record, and
there is no record in this context. To cancel the user's input, set the
controls to Null ...

Me!txtOne = Null
Me!cboTwo = Null

If there are a large number of controls, it may be worth writing some code
to loop through them. Test the ControlType property of the control so that
you don't attempt to set the value of a control (such as a line or
rectangle) that doesn't have a value ...

Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
ctl.Value = Null
End If
Next ctl
 
Tony,
Do you need to keep the form open if they want to cancel? If not, why don't
you just close the form?
DoCmd.Close acForm, Me.Name, acSaveNo
instead of
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
 
Thanks Mark. Yes I need to keep the form open. I'm looking at Brendan's
solution it looks like that may help.
Thanks
Tony
 
Back
Top