• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

Check for Changes on Data Form

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.


1 Solution
povernAuthor Commented:
This is in VB6 using a ADODC data control.
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.
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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
Forgot to mention that the adoRs is the recordset that is bound to your controls, if that wasn't obvious
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.
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
This question is PAQed and no points refunded (of 125)

Expert Exchange Moderator

Featured Post

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now