Y/N/Cancel message box (1 Viewer)

DavidCantor

Registered User.
Local time
Today, 15:27
Joined
Oct 4, 2012
Messages
66
I am trying to code a y/n/cancel message box, the yn works but the cancel gives me an error
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
   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 or Cancel to go back."
 
   ' Display the message box.
   iResponse = MsgBox(strMsg, vbQuestion + vbYesNoCancel, "Save Record?")
    
   ' Check the user's response.
   If iResponse = vbNo Then
    
      ' Undo the change.
      DoCmd.RunCommand acCmdUndo
 
      ' Cancel the update.
      Cancel = True
      
   ElseIf iResponse = vbCancel Then
   
   'Cancel Close, continue editing
   DoCmd.CancelEvent
    
   End If
   
End Sub

the error I am getting is

Code:
Run Time error '2001'
You cancelled the previous operation

and when I debug it takes me to this line:
Code:
 Me.Dirty = False

can anyone help me fix this?

thanks!
 

bob fitz

AWF VIP
Local time
Today, 23:27
Joined
May 23, 2011
Messages
4,727
Try replacing:
DoCmd.CancelEvent
with:
Cancel = True

I can't see the line:
Me.Dirty = False
in the code supplied.
 

DavidCantor

Registered User.
Local time
Today, 15:27
Joined
Oct 4, 2012
Messages
66
Y N Cancel message box help

Does anyone know how I can get a generic YNCAncel message box?

I have seen a generic Y N message box on the microsoft website that works in any form, but I need a Y N Cancel

thanks
 

DavidCantor

Registered User.
Local time
Today, 15:27
Joined
Oct 4, 2012
Messages
66
this is what the error is referencing
Private Sub BtnGoToSymbol2_Click()
' Make sure that there is a Product_ID
If IsNull(Me.Product_ID) Then
MsgBox "Please create a new record in this form first!", vbExclamation

Exit Sub
End If
'Save current record if necessary
If Me.Dirty Then
Me.Dirty = False
End If

' Open the other form
DoCmd.OpenForm FormName:="FrmSymbols", DataMode:=acFormAdd
' Set the default value for Product_ID
Forms!FrmSymbols!Product_ID.Value = Me.Product_ID



'Move to new record
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec

End Sub
 

pr2-eugin

Super Moderator
Local time
Today, 23:27
Joined
Nov 30, 2011
Messages
8,494
Re: Y N Cancel message box help

Try,
Code:
Call MsgBox("This is the Message", vbYesNoCancel)
 

DavidCantor

Registered User.
Local time
Today, 15:27
Joined
Oct 4, 2012
Messages
66
Re: Y N Cancel message box help

i get an error on this line :


Private Sub BtnGoToSymbol2_Click()
' Make sure that there is a Product_ID
If IsNull(Me.Product_ID) Then
MsgBox "Please create a new record in this form first!", vbExclamation

Exit Sub
End If
'Save current record if necessary
If Me.Dirty Then
Me.Dirty = False
End If

' Open the other form
DoCmd.OpenForm FormName:="FrmSymbols", DataMode:=acFormAdd
' Set the default value for Product_ID
Forms!FrmSymbols!Product_ID.Value = Me.Product_ID



'Move to new record
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec

End Sub
 

boblarson

Smeghead
Local time
Today, 15:27
Joined
Jan 12, 2001
Messages
32,059
Just add an error handler to your BtnGoToSymbol2_Click event and have it ignore that error.

Code:
Private Sub BtnGoToSymbol2_Click()
[B][COLOR=red]On Error GoTo Err_Handler[/COLOR][/B]

' Make sure that there is a Product_ID
If IsNull(Me.Product_ID) Then
MsgBox "Please create a new record in this form first!", vbExclamation

Exit Sub
End If
'Save current record if necessary
If Me.Dirty Then
[COLOR=black]Me.Dirty = False[/COLOR]
End If

' Open the other form
DoCmd.OpenForm FormName:="FrmSymbols", DataMode:=acFormAdd
' Set the default value for Product_ID
Forms!FrmSymbols!Product_ID.Value = Me.Product_ID



'Move to new record
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
 
[B][COLOR=red]Exit_BtnGoToSymbol2:[/COLOR][/B]
[B][COLOR=red]    Exit Sub[/COLOR][/B]
[B][COLOR=red][/COLOR][/B] 
[B][COLOR=red]Err_Handler:[/COLOR][/B]
[B][COLOR=red]   If Err.Number <> 2001 Then[/COLOR][/B]
[B][COLOR=red]      MsgBox Err.Descripton, vbExclamation, "Error #: " & Err.Number[/COLOR][/B]
[B][COLOR=red]      Resume Exit_BtnGoToSymbol2[/COLOR][/B]
[B][COLOR=red]      Resume     [/COLOR][/B]
[B][COLOR=red]   End If[/COLOR][/B]

End Sub
 

DavidCantor

Registered User.
Local time
Today, 15:27
Joined
Oct 4, 2012
Messages
66
I get error

Run Time Error '438' - Object doesnt support this property or method


Debug takes me to thie line:

MsgBox Err.Descripton, vbExclamation, "Error #: " & Err.Number
 

boblarson

Smeghead
Local time
Today, 15:27
Joined
Jan 12, 2001
Messages
32,059
Post the whole thing that you now have, including that part.
 

DavidCantor

Registered User.
Local time
Today, 15:27
Joined
Oct 4, 2012
Messages
66
Private Sub BtnStartAgain_Click()
' Make sure that there is a Product_ID
If IsNull(Me.Product_ID) Then
MsgBox "Please create a new record in this form first!", vbExclamation

Exit Sub
End If
'Save current record if necessary
If Me.Dirty Then
Me.Dirty = False
End If
' Close and save current form
' DoCmd.Close
' Open the other form
DoCmd.OpenForm FormName:="FrmProductIcon1", DataMode:=acFormNew
' Set the default value for Product_ID

'Move to new record
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
End Sub
Private Sub BtnStartAgain1_Click()
On Error GoTo Err_Handler
' Make sure that there is a Product_ID
If IsNull(Me.Product_ID) Then
MsgBox "Please create a new record in this form first!", vbExclamation

Exit Sub
End If
'Save current record if necessary
If Me.Dirty Then
Me.Dirty = False
End If
' Close and save current form
'DoCmd.Close
' Open the other form
DoCmd.OpenForm FormName:="FrmProductIcon1", DataMode:=acFormNew
' Set the default value for Product_ID



'Move to new record
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec

Exit_BtnGoToSymbol2:
Exit Sub

Err_Handler:
If Err.Number <> 2001 Then
MsgBox Err.Descripton, vbExclamation, "Error #: " & Err.Number
Resume Exit_BtnGoToSymbol2
Resume
End If

End Sub
 

boblarson

Smeghead
Local time
Today, 15:27
Joined
Jan 12, 2001
Messages
32,059
Do you have any objects (controls, fields, forms, reports, functions, macros) with either

Description

or

Err

as the name?
 

DavidCantor

Registered User.
Local time
Today, 15:27
Joined
Oct 4, 2012
Messages
66
I did a decompile now i get an error on this line:
error '438'
MsgBox Err.Descripton, vbExclamation, "Error #: " & Err.Number
 

boblarson

Smeghead
Local time
Today, 15:27
Joined
Jan 12, 2001
Messages
32,059
That makes no sense. What version of Access are you using? Is is possible to upload a copy of the database with fake data?
 

DavidCantor

Registered User.
Local time
Today, 15:27
Joined
Oct 4, 2012
Messages
66
I am running 2010, I am unable to post db because the data is sensitive
 

Users who are viewing this thread

Top Bottom