Solved

How to keep track whether the values of a form has been changed

Posted on 2002-05-07
13
211 Views
Last Modified: 2010-05-02
Hi

Could someone advice me on how i could keep track
whether the values of a form fields have been changed.

Right now i have a boolean variable for all the fields
but if i change one field and then change another and revert the
second one the variable says not changed, which is wrong.

I need this since when the user is clicking the close button
i have to ask whether to save.

Thanx  
0
Comment
Question by:b3cf
[X]
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
  • 2
  • 2
  • 2
  • +5
13 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 6993766
The best way I have found of doing this is to use the .Tag property of your form's textboxes. When you load the data, also set the .Tag property to the same. You can then run a test of each textbox and compare the .Text and .Tag properties. If any of them differ you know that there have been changes.
0
 
LVL 4

Expert Comment

by:RichW
ID: 6993784
Working from Tim's answer:

Private Sub Form_Load()
Dim obj as Object
For Each obj In Form1
    If TypeOf obj Is TextBox Then
        obj.Tag = obj.Text
    End If
Next
End Sub

Private Sub CheckForChanges()
Dim obj As Object
For Each obj In Form1
    If TypeOf obj Is TextBox Then
        If obj.Text <> obj.Tag Then
           Msgbox "Change Has Occurred"
        End If
    End If
Next
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 6993817
listening
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 2

Expert Comment

by:corvanderlinden
ID: 6993921
RichW

You forgot about listbox, combobox, checkbox, option button and so on.

It's a little bit more complicated than you think

If you are using a (disconected) recordset you could compare form values with the recordset values.

0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6993937
True corvanderlinden but these object still have .Tag properties and can be used in the same way (sometimes with a twist of course if the value property is not compatible with the .tag property) and of course you also have to handle the fact that an empty field is often treated as a zero so the two may not match even though logically they are the same. Nevertheless it is an effective method. Likewise using a disconnected recordset can be very effective. It all depends on the complexity of the form and so on. I have used both methods in different situations with great results.
0
 
LVL 4

Expert Comment

by:RichW
ID: 6993960
Hi corvanderlinden,

I merely gave an example of how this could be done.  There are many TypeOf objects that can be checked, and of course you'd need to work out the different data types along the way.

b3cf mentioned nothing about disconnected recordsets, but I do see your point.

I figured I'd get him started along the right path, but I appreciate your input as well.

Cheers,
RichW




0
 

Author Comment

by:b3cf
ID: 6993970
Thanks alot guys. I do have disconnected recordsets.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6994030
If you need to know at the field level when a value has changed, you can use the following approach:

If Text1.Text <> rs.Fields("Col1").OriginalValue Then
   Debug.Print "Field has changed"
End If

This seems cumbersome, especially if you have 20 or 30 fields.  One way of solving this is without actually binding your controls to the data, you can set the DataField property.  You can then use code something like (untested):

Function HasChanged(frm As Form, rs As ADODB.Recordset)
Dim ctl As Control

HasChanged = False
For Each ctl In frm
   If TypeOf ctl Is TextBox Then
      If Len(ctl.DataField) Then
         If ctl.Text <> rs.Fields(ctl.DataField).OriginalValue Then
            HasChanged = True
            'You could exit the For Next or even exit the function here
         End If
      End If
   End If
Next

End Function

Anthony
0
 
LVL 15

Expert Comment

by:ameba
ID: 6994576
There are 4 ways to see if something has changed:
http://www.vb2themax.com/Item.asp?PageID=TipBank&ID=38

http://www.vb2themax.com/Item.asp?PageID=TipBank&ID=416
uses ctrl.DataChanged

I use boolean variable(s) and Change event.
0
 
LVL 2

Expert Comment

by:corvanderlinden
ID: 6995735
ameba

The problem with DataChanged is following

Original Text : XX
User changed to : YY
User changed back to XX
Datachanged is True, but data in fact did not change
0
 
LVL 15

Accepted Solution

by:
ameba earned 50 total points
ID: 6995801
On the first change, I enable Save button and set a flag aProp.Dirty to True.
I also change Forecolor of the control (to dark blue), so user can see which fields have been edited.

In options dialog, there is a checkbox "Confirm changes" - user can select if changes are automatically saved when exiting the data dialogs.  Some code:

' in forms
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Select Case SaveRequired(aProp.Dirty)
    Case vbYes
        If UpdateRecord() = False Then
            Cancel = True
        End If
    Case vbCancel
        Cancel = True
    End Select
End Sub


' in a module
Public Function SaveRequired(ByVal RecordChanged As Boolean, Optional ByVal Msg _
    As String = "Save changes?") As VbMsgBoxResult
   
    If RecordChanged Then
        If g.ConfirmChanges Then
            SaveRequired = MsgBox(Msg, vbYesNoCancel Or vbQuestion)
        Else
            SaveRequired = vbYes
        End If
    Else
        SaveRequired = vbNo
    End If
End Function
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7018695
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101, Netminder or Mindphaser will return to finalize these if they are still open in 7 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20277487.html
http://www.experts-exchange.com/questions/Q.20280008.html
http://www.experts-exchange.com/questions/Q.20282634.html
http://www.experts-exchange.com/questions/Q.20293765.html
http://www.experts-exchange.com/questions/Q.20297445.html
http://www.experts-exchange.com/questions/Q.20297846.html
http://www.experts-exchange.com/questions/Q.20298472.html



*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed in the link below
http://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
0
 

Author Comment

by:b3cf
ID: 7019720
Thanx guys.
0

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!

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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
Course of the Month8 days, 21 hours left to enroll

615 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