vb123.com

Garry Robinson's Popular MS Access, Office and VB Resource Site

 

Home  Contact Us
Order our Software

RSS & Newsletter  
Join our XML/RSS Newsfeed or sign up for our informative newsletter on Office Automation, Access and VB topics
Read More

Get Good Help
If you need help with a database, our Professionals could be the answer
Read More

  Is Your Database Corrupt ?
If you have a corrupt database, Try our Access Recovery service

The Workbench  Find out who has your database open, start the correct version of Access, easy compacting and backups, change startup options, mde compile,  shutdown database Read and Download

The Toolshed 
Searchable help file comprising of all the information at vb123.com plus hidden downloads etc. Read More



The Toolbox

Libraries of software that we regularly import into our projects. This is a newer version of the Toolshed More..


Access >>> SQL 
Upsize to SQL Server 2005 or 2008, easily repeated conversions, highly accurate SQL query
translation and web form conversion.
Read More


DryToast 
Backup and query your BaseCamp
® projects
Read More


Datamining/Graphs

Explore your data with this versatile graphing and data mining shareware tool.  Read More

Garry's Blog
Find out a few other things that Garry has been writing about Microsoft Access. Read more

About The Editor Garry Robinson writes for a number of popular computer magazines, is now a book author and has worked on 100+ Access databases. He is based in Sydney, Australia
Contact Us ...

Search ...

or try our new site built with SharePoint Designer
 vb123.com.au
 

 

Next Tip   Track all changes made to a record in Microsoft Access

By Nirmala Sekhar

Very often, we need to audit all changes made to a record. This is useful for sensitive data or in cases where accountability needs to be established. System developers may want to provide this facility to protect their own back, especially for system options that totally change system behaviour. You need to

> Store all changes made, and
> Ascertain who made each change and when

Using the example of a student's form, we would need

> An additional table - tblStudentChanges.

> Two extra fields (Usercode and DateofChange) in both tblStudent and tblStudentChanges

> Use a password driven system or turn on Access built-in security to ascertain who the current user is.

Store all changes made

When both the tables have identical field names, this process is very simple. Use the AfterUpdate event of the form, to insert a new record in the tblStudentChanges table, whenever a change is made.

     Private Sub Form_AfterUpdate()
     Dim db As Database

         Set db = CurrentDb
         db.Execute "INSERT INTO [tblStudentChanges] " _
    	 & " SELECT * FROM [tblStudent] WHERE " _
    	 & " [tblStudent].[StudentID]=" & Me![StudentID] & ";"
         Set db = Nothing
     End Sub

Ascertain who made each change and when

In the sample database, the student form receives the usercode of the current user through the OpenArgs property. Use the FormOpen event to set it up.

     Private Sub Form_Open(Cancel As Integer)
     'if you are using Access passwords to identify
     '   current user, use CurrentUser() Access function

     [txtCurrentUser] = Me.OpenArgs

     End Sub

In the BeforeUpdate event of the form, check to see if any real changes have been made. You can use the OldValue property of controls to test for changes. If no changes have been made, simply cancel the update. If changes have been made, update the Usercode and DateofChange fields in the record.

       Private Sub Form_BeforeUpdate(Cancel As Integer)
       On Error Resume Next

        ' some controls may not have the Tag property ,
	' hence the resume next

       Dim blnCheckDiff As Boolean
       Dim ctl As Control

       blnCheckDiff = False
       For Each ctl In Me.Controls
           If ctl.Tag = "Check" And ctl.Value <> ctl.OldValue Then
              blnCheckDiff = True
           End If
       Next
       If blnCheckDiff Then
               [txtTime] = Now()
               [txtuser] = [txtCurrentUser]
       Else
               Cancel = True
       End If
       End Sub

Display Changes

Create a separate form based on the tblStudentChanges table. The form in the sample db displays the records in reverse chronological order, but you can change that, if needed. Make sure the form is a read-only and pop-up as well. Provide a command button to close the form and display the original Student's form again.

In the Student's form, create a command button that will display the Student Changes form for the current student. Make sure that all current changes have been saved before you open the Student Changes form.

     Private Sub cmdChange_Click()
         Dim stDocName As String
         Dim stLinkCriteria As String

         stDocName = "frmStudentChanges"
         
         DoCmd.RunCommand acCmdSaveRecord
         ' the forced save of the current record
         ' will ensure that the current changes
         ' are reflected in the new form to be opened.
         
             stLinkCriteria = "[StudentID]=" & Me![StudentID]
         Me.Visible = False
         DoCmd.OpenForm stDocName, , , stLinkCriteria
     
     End Sub

  Download the sample database. (Zip file 66 kb) if you own "The Toolshed"  Else click here 
Sample database contains a Student Details form and a command button to view all changes made to a student's record.

Author Bio:

Nirmala Sekhar is a software consultant working from Singapore.

Alternatives From Garry

Rather than saving the full record to another table, you can add 2 fields to the current table such as SystemUsername (text 50) and RecordChanged (date) and log the person who made the last change to that record.  In the before update event, add the following code.   This is less onerous to manage. 

Private Sub Form_BeforeUpdate(Cancel As Integer)

'  Log the user details to the table

  Me!SystemUsername = User_FX
  Me!RecordChanged = Now()

End sub

User_FX is a Toolshed function that retrieves the NT/Win XP user name.  This will store that person who has logged for that records.  You do not need to add the fields to the form to make this work.

Related Documents at VB123

Confirm Access Record Changes
Transaction Queries
Close All Open Recordsets Tip
Create Table Query and More Hidden Access SQL Queries

The Access Workbench includes full source code for the functions above


Click on the button Next Tip  to go to the next page in the loop

 

 

Links >>>  Home | Search | Workbench | Orders | Newsletter | Access Security | Access professionals