PDA

View Full Version : Why am I receiving a run time error '2046' message on a save button option?



wedd
01-17-2011, 03:14 AM
Hi, I created a save button with options to either save the record yes or no. When I click on no option the following error message appears. "Run Time error '2046': The command or 'Undo' isn't available now", However when I click yes nothing happens. Why has this occured and how can I hide this message?

I've written the code out below:


Thanks for your contributions:friends:


Private Sub Command21_Click()
Dim strMsg As String
Dim iResponse As Integer

' Specify the message to display.
strMsg = "Do you wish to save the changes?" & Chr(10)
strMsg = strMsg & "Click Yes to Save or No to Discard changes."

' Display the message box.
iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?")

' Check the user's response.
If iResponse = vbNo Then
' Undo the change.
DoCmd.RunCommand acCmdUndo

' Cancel the update.

End If
End Sub

Imdabaum
01-17-2011, 10:07 AM
' Check the user's response.
If iResponse = vbNo Then
' Undo the change.
DoCmd.RunCommand acCmdUndo

'ELSE WHAT?
'code to save the record

' Cancel the update.


Your code handles the vbNo. But it doesn't do anything if they say yes.

hansup
01-17-2011, 12:45 PM
Hi, I created a save button with options to either save the record yes or no. When I click on no option the following error message appears. "Run Time error '2046': The command or 'Undo' isn't available now", However when I click yes nothing happens. Why has this occured and how can I hide this message?
Most likely your code is attempting to undo changes to a record which has not been changed. In that case, "'Undo' isn't available" ... because there are no changes to undo.

Nothing happens when you click Yes because your code doesn't save the current record.

See if this version does what you want. Note I re-named the command button from Command21 to cmdSave.

Private Sub cmdSave_Click()
Dim strMsg As String
Dim iResponse As Integer

If Me.Dirty Then
' Specify the message to display.
strMsg = "Do you wish to save the changes?" & Chr(10)
strMsg = strMsg & "Click Yes to Save or No to Discard changes."
' Display the message box.
iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?")

' Check the user's response.
If iResponse = vbYes Then
Me.Dirty = False 'save current record
Else 'vbNo
' Undo the change.
'DoCmd.RunCommand acCmdUndo
Me.Undo
End If
Else
MsgBox "No changes to save!"
End If
End Sub

wedd
01-17-2011, 02:46 PM
Ok, thanks I'll test it.