Problem when no changes made to form

  • Thread starter Thread starter Jani
  • Start date Start date
J

Jani

I have a form to edit a record and my code is causing problems when there are
no changes made to the form. If I select No when there are no changes, it
gives a run-time error 2046; saving with no changes works. If there are
changes to the data it works fine either saving or not saving. Below is my
code. Any help would be very much appreciated. Thank you! Jani

Private Sub Command129_Click()
Dim strMsg As String
strMsg = "Data has changed."
strMsg = strMsg & " Do you wish to save the changes?"
strMsg = strMsg & " Click Yes to Save or No to Discard changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbYes Then
DoCmd.Close
DoCmd.OpenForm "frm_MainMenu"
Else
DoCmd.RunCommand acCmdUndo
DoCmd.Close
DoCmd.OpenForm "frm_MainMenu"
End If
End Sub
 
Hi Jani

Presumably you don't want to see the message at all if there have been no
changes. Try this instead:

If Me.Dirty Then ' (1)
strMsg = "Data has changed." & vbCrLf ' (2)
strMsg = strMsg & " Do you wish to save the changes?" & vbCrLf
strMsg = strMsg & " Click Yes to Save or No to Discard changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbNo Then
Me.Undo ' (3)
End If
End If
DoCmd.Close acForm, Me.Name ' (4)
DoCmd.OpenForm "frm_MainMenu"

Some notes:
(1) this checks is the form is "dirty" (i.e. data has changed)
(2) vbCrLf inserts a new line in your message
(3) Me.Undo is preferable (IMO) to RunCommand
(4) this ensures that the correct form is closed - sometimes a timing issue
might cause the Close not to happen until the next form is open, so the new
form gets closed instead.
 
Thanks for the reply Graham. Not sure what I'm not getting but I couldn't get
it to work. The issue is that if there's no change nothing happens with the
button is clicked. The cancel/undo and save work correctly. Below is the code
I now have. Thanks!
Private Sub Command129_Click()
If Me.Dirty Then ' (1)
strMsg = "Data has changed." & vbCrLf ' (2)
strMsg = strMsg & " Do you wish to save the changes?" & vbCrLf
strMsg = strMsg & " Click Yes to Save or No to Discard changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbNo Then
Me.Undo ' (3)
End If

DoCmd.Close acForm, Me.Name ' (4)
DoCmd.OpenForm "frm_MainMenu"
End If
End Sub

Graham Mandeno said:
Hi Jani

Presumably you don't want to see the message at all if there have been no
changes. Try this instead:

If Me.Dirty Then ' (1)
strMsg = "Data has changed." & vbCrLf ' (2)
strMsg = strMsg & " Do you wish to save the changes?" & vbCrLf
strMsg = strMsg & " Click Yes to Save or No to Discard changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbNo Then
Me.Undo ' (3)
End If
End If
DoCmd.Close acForm, Me.Name ' (4)
DoCmd.OpenForm "frm_MainMenu"

Some notes:
(1) this checks is the form is "dirty" (i.e. data has changed)
(2) vbCrLf inserts a new line in your message
(3) Me.Undo is preferable (IMO) to RunCommand
(4) this ensures that the correct form is closed - sometimes a timing issue
might cause the Close not to happen until the next form is open, so the new
form gets closed instead.

--
Good Luck :-)

Graham Mandeno [Access MVP]
Auckland, New Zealand

Jani said:
I have a form to edit a record and my code is causing problems when there
are
no changes made to the form. If I select No when there are no changes, it
gives a run-time error 2046; saving with no changes works. If there are
changes to the data it works fine either saving or not saving. Below is my
code. Any help would be very much appreciated. Thank you! Jani

Private Sub Command129_Click()
Dim strMsg As String
strMsg = "Data has changed."
strMsg = strMsg & " Do you wish to save the changes?"
strMsg = strMsg & " Click Yes to Save or No to Discard changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbYes Then
DoCmd.Close
DoCmd.OpenForm "frm_MainMenu"
Else
DoCmd.RunCommand acCmdUndo
DoCmd.Close
DoCmd.OpenForm "frm_MainMenu"
End If
End Sub
 
Hi Jani

You altered my code!!

What I posted:
If Me.Dirty Then
.....
End If
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm "frm_MainMenu"

What you have:
If Me.Dirty Then
.....
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm "frm_MainMenu"
End If

By moving the two DoCmd lines above the final End If, you are saying you
want the form to close and the menu to open ONLY if the form is dirty. So
of course, if you have made no changes then nothing happens.
--
Good Luck :-)

Graham Mandeno [Access MVP]
Auckland, New Zealand

Jani said:
Thanks for the reply Graham. Not sure what I'm not getting but I couldn't
get
it to work. The issue is that if there's no change nothing happens with
the
button is clicked. The cancel/undo and save work correctly. Below is the
code
I now have. Thanks!
Private Sub Command129_Click()
If Me.Dirty Then ' (1)
strMsg = "Data has changed." & vbCrLf ' (2)
strMsg = strMsg & " Do you wish to save the changes?" & vbCrLf
strMsg = strMsg & " Click Yes to Save or No to Discard changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbNo Then
Me.Undo ' (3)
End If

DoCmd.Close acForm, Me.Name ' (4)
DoCmd.OpenForm "frm_MainMenu"
End If
End Sub

Graham Mandeno said:
Hi Jani

Presumably you don't want to see the message at all if there have been no
changes. Try this instead:

If Me.Dirty Then ' (1)
strMsg = "Data has changed." & vbCrLf ' (2)
strMsg = strMsg & " Do you wish to save the changes?" & vbCrLf
strMsg = strMsg & " Click Yes to Save or No to Discard changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbNo Then
Me.Undo ' (3)
End If
End If
DoCmd.Close acForm, Me.Name ' (4)
DoCmd.OpenForm "frm_MainMenu"

Some notes:
(1) this checks is the form is "dirty" (i.e. data has changed)
(2) vbCrLf inserts a new line in your message
(3) Me.Undo is preferable (IMO) to RunCommand
(4) this ensures that the correct form is closed - sometimes a timing
issue
might cause the Close not to happen until the next form is open, so the
new
form gets closed instead.

--
Good Luck :-)

Graham Mandeno [Access MVP]
Auckland, New Zealand

Jani said:
I have a form to edit a record and my code is causing problems when
there
are
no changes made to the form. If I select No when there are no changes,
it
gives a run-time error 2046; saving with no changes works. If there are
changes to the data it works fine either saving or not saving. Below is
my
code. Any help would be very much appreciated. Thank you! Jani

Private Sub Command129_Click()
Dim strMsg As String
strMsg = "Data has changed."
strMsg = strMsg & " Do you wish to save the changes?"
strMsg = strMsg & " Click Yes to Save or No to Discard changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbYes Then
DoCmd.Close
DoCmd.OpenForm "frm_MainMenu"
Else
DoCmd.RunCommand acCmdUndo
DoCmd.Close
DoCmd.OpenForm "frm_MainMenu"
End If
End Sub
 
Thanks, thanks, thanks - I thought I needed to make a change but should not
have questioned your code! Appreciate the help so much and the tips. jms

Graham Mandeno said:
Hi Jani

Presumably you don't want to see the message at all if there have been no
changes. Try this instead:

If Me.Dirty Then ' (1)
strMsg = "Data has changed." & vbCrLf ' (2)
strMsg = strMsg & " Do you wish to save the changes?" & vbCrLf
strMsg = strMsg & " Click Yes to Save or No to Discard changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbNo Then
Me.Undo ' (3)
End If
End If
DoCmd.Close acForm, Me.Name ' (4)
DoCmd.OpenForm "frm_MainMenu"

Some notes:
(1) this checks is the form is "dirty" (i.e. data has changed)
(2) vbCrLf inserts a new line in your message
(3) Me.Undo is preferable (IMO) to RunCommand
(4) this ensures that the correct form is closed - sometimes a timing issue
might cause the Close not to happen until the next form is open, so the new
form gets closed instead.

--
Good Luck :-)

Graham Mandeno [Access MVP]
Auckland, New Zealand

Jani said:
I have a form to edit a record and my code is causing problems when there
are
no changes made to the form. If I select No when there are no changes, it
gives a run-time error 2046; saving with no changes works. If there are
changes to the data it works fine either saving or not saving. Below is my
code. Any help would be very much appreciated. Thank you! Jani

Private Sub Command129_Click()
Dim strMsg As String
strMsg = "Data has changed."
strMsg = strMsg & " Do you wish to save the changes?"
strMsg = strMsg & " Click Yes to Save or No to Discard changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbYes Then
DoCmd.Close
DoCmd.OpenForm "frm_MainMenu"
Else
DoCmd.RunCommand acCmdUndo
DoCmd.Close
DoCmd.OpenForm "frm_MainMenu"
End If
End Sub
 
Back
Top