Check for Changes on  Data Form

Posted on 2003-03-27
Medium Priority
Last Modified: 2010-05-01
Is there an easy way to check to see if data field values have changed on a form?

Before I close a form, I want to see if values have changed, and give the user a chance to save them or cancle out of the change.

I put a variable in the Change event of the test boxes, but if I just move to the next record it sets it as true, even if the field value hasn't changed.

I know there has to be an easier way.


Question by:povern
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Author Comment

ID: 8217972
This is in VB6 using a ADODC data control.

Expert Comment

ID: 8218114
Create a boolean form-level variable, I normally use 'dirty'. When loading the form, or loading a new record, set dirty = false

When changing any control, (ie in the change event or click event,) set dirty = true. To check if a change is made, just look at the dirty variable.

I haven't used this with the ADODC data control, but it should work fine with it. I'm not sure how you'd cancel a change, but I hope this tip helps.

Expert Comment

ID: 8218322
This may help...
You can set the KeyPreview = True at the form level then any keystroke will cause the form's keypress, keyup & keydown events to fire.  Still you will need an indicator to check at some point so the use of the form level boolean still applies.
Generally I would do as chrisk0 suggests but this may work well for you too. You can filter out inconsequential keystrokes if needs be. Ex:

Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case Me.ActiveControl.Name
Case "txtNum"
  MsgBox "A keystroke was made in txtNum. The ascii value was " & KeyAscii
  Dirty = True
End Select
End Sub
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 8218674
There's 2 ways

  If the control is not bound either to an adodb.Recordset (using the Datamember).

  Then you need to put some code in each control's change event.

  Something Like this:

  Private IsDirty as Boolean

  Private Sub Field1_Change()
    IsDirty = True
  End Sub

  And in the Form_QueryUnload Event of the Form

  Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If IsDirty Then
      'Do a bunch of stuff here

      'If you need to cancel the Form close then
      Cancel = vbCancel
    End If
  end Sub

Or if you are bound to an adodb.Recordset you would do it like this:

  In the Form_QueryUnload you will have this code

  Dim adoField as adodb.Field
  For each adoField in adoRs.Fields
    if adoField.Value <> adoField.OriginalValue Then
      'Do whatever you want here
      'If you need to cancel the Form Close
      Cancel = vbCancel
    end if
  next adoField

  If you use an adodc control to bind your fields (controls) you can pretty much do the same.

  Hope this either helps or points you in the right direction

Expert Comment

ID: 8218683
Forgot to mention that the adoRs is the recordset that is bound to your controls, if that wasn't obvious

Expert Comment

ID: 8531517
Hi povern,
This old question (QID 20565152) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.

Expert Comment

ID: 9440929
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

 -->PAQ - no points refunded

Please leave any comments here within the next seven days.


Cleanup Volunteer

Accepted Solution

YensidMod earned 0 total points
ID: 9495766
This question is PAQed and no points refunded (of 125)

Expert Exchange Moderator

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question