Confirm data change on close

  • Follow


I want a data editing form to check for changes when the close button is 
pressed by the user.  The code I have entered reads "Run time error 13.  Type 
Mismatch"  Any help would be greatly appreciated!

Private Sub Form_Close()

    If Me.DataChange Then
        Dim strMsg As String
        Dim Cancel As Integer
        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
            'do nothing
            Else
            Me.Undo
            Cancel = True
            End If
    Else
    'do nothing
    End If
End Sub
0
Reply Utf 1/3/2008 6:06:02 PM

"Travis (New User)" <TravisNewUser@discussions.microsoft.com> wrote in 
message news:10E0C7AC-54B4-4909-B921-CF7975BD1C7B@microsoft.com...
>I want a data editing form to check for changes when the close button is
> pressed by the user.  The code I have entered reads "Run time error 13. 
> Type
> Mismatch"  Any help would be greatly appreciated!
>
> Private Sub Form_Close()
>
>    If Me.DataChange Then
>        Dim strMsg As String
>        Dim Cancel As Integer
>        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
>            'do nothing
>            Else
>            Me.Undo
>            Cancel = True
>            End If
>    Else
>    'do nothing
>    End If
> End Sub

Use the form's Unload event instead of its Close event. Notice that the 
Unload event has a parameter called Cancel. Because that's supplied by 
Access, you should delete the line:

Cancel = True


0
Reply Stuart 1/3/2008 6:23:37 PM



New User Travis wrote:
> I want a data editing form to check for changes when the close button is
> pressed by the user.  The code I have entered reads "Run time error 13.  Type
> Mismatch"  Any help would be greatly appreciated!
>
> Private Sub Form_Close()
>
>     If Me.DataChange Then
>         Dim strMsg As String
>         Dim Cancel As Integer
>         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
>             'do nothing
>             Else
>             Me.Undo
>             Cancel = True
>             End If
>     Else
>     'do nothing
>     End If
> End Sub

Don't know much about VB, after only a week or so coding, but
sometimes this occurs when you mix and match strings and numbers on
the same line.  Use the CStr() convert function to convert numbers to
strings and the corresponding cast for strings to numbers.

RL
0
Reply raylopez99 1/3/2008 6:30:44 PM

"Stuart McCall" <smccall@myunrealbox.com> wrote in message 
news:flj97n$4ko$1$830fa79d@news.demon.co.uk...
> "Travis (New User)" <TravisNewUser@discussions.microsoft.com> wrote in 
> message news:10E0C7AC-54B4-4909-B921-CF7975BD1C7B@microsoft.com...
>>I want a data editing form to check for changes when the close button is
>> pressed by the user.  The code I have entered reads "Run time error 13. 
>> Type
>> Mismatch"  Any help would be greatly appreciated!
>>
>> Private Sub Form_Close()
>>
>>    If Me.DataChange Then
>>        Dim strMsg As String
>>        Dim Cancel As Integer
>>        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
>>            'do nothing
>>            Else
>>            Me.Undo
>>            Cancel = True
>>            End If
>>    Else
>>    'do nothing
>>    End If
>> End Sub
>
> Use the form's Unload event instead of its Close event. Notice that the 
> Unload event has a parameter called Cancel. Because that's supplied by 
> Access, you should delete the line:
>
> Cancel = True

Sorry, that should read:

Because that's supplied by Access, you should delete the line:

Dim Cancel As Integer


0
Reply Stuart 1/3/2008 6:32:30 PM

You don't say on which line the error occurs, but I am thinking the way you 
are using Me.DataChange could be the problem.  A form control will not 
evaluate to a boolean expression.  That is, if DataChange is a control.  If 
it is a user defined form property, then it should be cast as a boolean data 
type.
-- 
Dave Hargis, Microsoft Access MVP


"Travis (New User)" wrote:

> I want a data editing form to check for changes when the close button is 
> pressed by the user.  The code I have entered reads "Run time error 13.  Type 
> Mismatch"  Any help would be greatly appreciated!
> 
> Private Sub Form_Close()
> 
>     If Me.DataChange Then
>         Dim strMsg As String
>         Dim Cancel As Integer
>         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
>             'do nothing
>             Else
>             Me.Undo
>             Cancel = True
>             End If
>     Else
>     'do nothing
>     End If
> End Sub
0
Reply Utf 1/3/2008 7:15:03 PM

On Jan 3, 2:15=A0pm, Klatuu <Kla...@discussions.microsoft.com> wrote:
> You don't say on which line the error occurs, but I am thinking the way yo=
u
> are using Me.DataChange could be the problem. =A0A form control will not
> evaluate to a boolean expression. =A0That is, if DataChange is a control. =
=A0If
> it is a user defined form property, then it should be cast as a boolean da=
ta
> type.
> --
> Dave Hargis, Microsoft Access MVP

Yes, Klatuu is right.

I was going to say the same thing the first time.  For example, before
some helpful posters pointed out the correct syntax, the below used to
always give me this error:

'CORRECT SYNTAX:

If Not IsNull(Me.StockSymbol) Then
    Str002G =3D Me.AcctID.Value
        If Not IsNull(Me.AcctID) Then
         Str001G =3D Me.StockSymbol.Value
         End If
    End If

replaces (THIS GIVES A RUN TIME ERROR OF "Type 13"--among other
errors):

If (Me.StockSymbol.Text <> Null And Me.AcctID.Text <> Null ) Then

=2E..

End If

0
Reply raylopez99 1/3/2008 7:48:23 PM

This is not really a data type issue:
If (Me.StockSymbol.Text <> Null And Me.AcctID.Text <> Null ) Then

It is a syntax issue.  Nothing will ever = Null or be <> Null.  Even Null = 
Null will return false. The only way to check for Null is:

If Not IsNull(Me.StockSymbol) And Not IsNull(Me.AcctID) Then

In this context, don't use the Text property.  It is only valid when a 
control has the focus.
-- 
Dave Hargis, Microsoft Access MVP


"raylopez99" wrote:

> On Jan 3, 2:15 pm, Klatuu <Kla...@discussions.microsoft.com> wrote:
> > You don't say on which line the error occurs, but I am thinking the way you
> > are using Me.DataChange could be the problem.  A form control will not
> > evaluate to a boolean expression.  That is, if DataChange is a control.  If
> > it is a user defined form property, then it should be cast as a boolean data
> > type.
> > --
> > Dave Hargis, Microsoft Access MVP
> 
> Yes, Klatuu is right.
> 
> I was going to say the same thing the first time.  For example, before
> some helpful posters pointed out the correct syntax, the below used to
> always give me this error:
> 
> 'CORRECT SYNTAX:
> 
> If Not IsNull(Me.StockSymbol) Then
>     Str002G = Me.AcctID.Value
>         If Not IsNull(Me.AcctID) Then
>          Str001G = Me.StockSymbol.Value
>          End If
>     End If
> 
> replaces (THIS GIVES A RUN TIME ERROR OF "Type 13"--among other
> errors):
> 
> If (Me.StockSymbol.Text <> Null And Me.AcctID.Text <> Null ) Then
> 
> ...
> 
> End If
> 
> 
0
Reply Utf 1/3/2008 8:00:03 PM

On Thu, 3 Jan 2008 10:06:02 -0800, Travis (New User)
<TravisNewUser@discussions.microsoft.com> wrote:

>I want a data editing form to check for changes when the close button is 
>pressed by the user.  The code I have entered reads "Run time error 13.  Type 
>Mismatch"  Any help would be greatly appreciated!
>
>Private Sub Form_Close()
>
>    If Me.DataChange Then

What is DataChange? A control on your form, or a custom form property that you
have created?

Because it's not a builtin Access feature, to my knowledge.

Do you perhaps mean Me.Dirty (a Boolean indicating that the user has changed
some value in a bound control on the form)?

             John W. Vinson [MVP]
0
Reply John 1/3/2008 11:25:38 PM

Alright guys, thanks for all the help.  All of your answers put me in the 
right direction.  I didn't do a very good job explaining exactly what I 
wanted, but here is the code that I came up with after all of your 
suggestions that works:

    If Me.Dirty Then
        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
            'do nothing
            DoCmd.Close
            Else
            Me.Undo
            DoCmd.Close
            End If
    Else
    'do nothing
    DoCmd.Close
    End If

On a form I have a close button, a save button, text boxes, drop down lists, 
etc etc.  I have taken away the microsoft given buttons at the top.  The only 
way for the user to interact with the form is by the objects I have given 
them.

I wanted the save button to save the record automatically with no prompting, 
but I wanted the exit button to check if there were any accidental changes 
and ask the user to confirm what they wanted.

Again, thanks for all the help guys.  You all got me on the right path!

- Travis



"Travis (New User)" wrote:

> I want a data editing form to check for changes when the close button is 
> pressed by the user.  The code I have entered reads "Run time error 13.  Type 
> Mismatch"  Any help would be greatly appreciated!
> 
> Private Sub Form_Close()
> 
>     If Me.DataChange Then
>         Dim strMsg As String
>         Dim Cancel As Integer
>         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
>             'do nothing
>             Else
>             Me.Undo
>             Cancel = True
>             End If
>     Else
>     'do nothing
>     End If
> End Sub
0
Reply Utf 1/4/2008 3:27:02 PM

For your save button:

    If Me.Dirty Then
        Me.Dirty = False
    End If

For your Exit Button, just use Docmd.Close in the click event.  Put the code 
to check for changes in the Form's Unload event.  That way, if the user 
closes the form either using the exit button or by some other means, it will 
stilll do the check.

Put this in the Form Unload event:

Dim str Msg As String

    If Me.Dirty Then
        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
            Me.Dirty = False
        Else
            Me.Undo
        End If
    End If

-- 
Dave Hargis, Microsoft Access MVP


"Travis (New User)" wrote:

> Alright guys, thanks for all the help.  All of your answers put me in the 
> right direction.  I didn't do a very good job explaining exactly what I 
> wanted, but here is the code that I came up with after all of your 
> suggestions that works:
> 
>     If Me.Dirty Then
>         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
>             'do nothing
>             DoCmd.Close
>             Else
>             Me.Undo
>             DoCmd.Close
>             End If
>     Else
>     'do nothing
>     DoCmd.Close
>     End If
> 
> On a form I have a close button, a save button, text boxes, drop down lists, 
> etc etc.  I have taken away the microsoft given buttons at the top.  The only 
> way for the user to interact with the form is by the objects I have given 
> them.
> 
> I wanted the save button to save the record automatically with no prompting, 
> but I wanted the exit button to check if there were any accidental changes 
> and ask the user to confirm what they wanted.
> 
> Again, thanks for all the help guys.  You all got me on the right path!
> 
> - Travis
> 
> 
> 
> "Travis (New User)" wrote:
> 
> > I want a data editing form to check for changes when the close button is 
> > pressed by the user.  The code I have entered reads "Run time error 13.  Type 
> > Mismatch"  Any help would be greatly appreciated!
> > 
> > Private Sub Form_Close()
> > 
> >     If Me.DataChange Then
> >         Dim strMsg As String
> >         Dim Cancel As Integer
> >         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
> >             'do nothing
> >             Else
> >             Me.Undo
> >             Cancel = True
> >             End If
> >     Else
> >     'do nothing
> >     End If
> > End Sub
0
Reply Utf 1/4/2008 3:35:01 PM

On Jan 4, 10:35=A0am, Klatuu <Kla...@discussions.microsoft.com> wrote:
> For your save button:
>
> =A0 =A0 If Me.Dirty Then
> =A0 =A0 =A0 =A0 Me.Dirty =3D False
> =A0 =A0 End If
>
> For your Exit Button, just use Docmd.Close in the click event. =A0Put the =
code
> to check for changes in the Form's Unload event. =A0That way, if the user
> closes the form either using the exit button or by some other means, it wi=
ll
> stilll do the check.
>
> Put this in the Form Unload event:


Very interesting thread but I have a newbie question--I understand the
principle behind Save in Windows, but in Access it seems that clicking
on the red "X" in the upper right corner always saves the record
automatically.  That is, for any form I've worked with to date (about
2 weeks of VB programming to be sure), it seems (maybe I'm wrong) that
no data has ever been lost this way (by simply closing the form as
described above).

In short, why bother with "save"--it's it the default to save
everything if a form/control is dirty?

RL
0
Reply raylopez99 1/4/2008 5:13:46 PM

You are correct. closing the form or the application will save any changes.

There is no real good reason to use a save button.  It is just that some 
people are afraid they will save data they don't want to save.

-- 
Dave Hargis, Microsoft Access MVP


"raylopez99" wrote:

> On Jan 4, 10:35 am, Klatuu <Kla...@discussions.microsoft.com> wrote:
> > For your save button:
> >
> >     If Me.Dirty Then
> >         Me.Dirty = False
> >     End If
> >
> > For your Exit Button, just use Docmd.Close in the click event.  Put the code
> > to check for changes in the Form's Unload event.  That way, if the user
> > closes the form either using the exit button or by some other means, it will
> > stilll do the check.
> >
> > Put this in the Form Unload event:
> 
> 
> Very interesting thread but I have a newbie question--I understand the
> principle behind Save in Windows, but in Access it seems that clicking
> on the red "X" in the upper right corner always saves the record
> automatically.  That is, for any form I've worked with to date (about
> 2 weeks of VB programming to be sure), it seems (maybe I'm wrong) that
> no data has ever been lost this way (by simply closing the form as
> described above).
> 
> In short, why bother with "save"--it's it the default to save
> everything if a form/control is dirty?

There is no real good reason to do this.  It is just that some people are 
afraid they will save data they don't want to save.

> 
> RL
> 
0
Reply Utf 1/4/2008 5:22:02 PM

11 Replies
226 Views

(page loaded in 0.229 seconds)

Similiar Articles:































7/24/2012 9:18:28 PM


Reply: