Solved

insert record on update vba

Posted on 2014-04-25
3
449 Views
Last Modified: 2014-04-25
I have an access database with a form frm_main

I need to insert a record into a table (audit log type) every time an update is made in frm_main

tbl_main has field field1 that we need to monitor

tbl_audit has the following 3 fields that need to be updated

fld_previous = value of prior value of field1
fld_new = new value of field1
fld_date = date/time when update occurred

can somebody help me with the vba logic, I would much appreciate it.

thanks,
Vinnie
0
Comment
Question by:damixa
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 40022977
The Form's AfterUpdate event could be used for this, but you'll need to do some work before hand to capture your "old" values.

First create a global variable in the General Declarations section:

Private fChanged As Boolean

Next, create a hidden textbox named "txField1_Old". In the Current event of the Form, do this:

Me.txField1_Old = Me!Field1

In the Change or AfterUpdate event of Field1, do this:

fChanged = True

Now in the Form's AfterUpdate event:

If fChanged Then
  Currentdb.Execute "INSERT INTO YourAuditTable(fld_Previous, fld_New, fld_Date)   VALUES('" & Me.txField1_Old & "','" & Me.Field1 & "',#" & Now & "#)"
End If

If fld_New and fld_Previous are Numeric values:

Currentdb.Execute "INSERT INTO YourAuditTable(fld_Previous, fld_New, fld_Date) VALUES(" & Me.txField1_Old & "," & Me.Field1 & ",#" & Now & "#)"

Finally, in the Form's Current event reset fChanged:

Sub Form_Current()
  fChanged = False
End Sub
0
 

Author Comment

by:damixa
ID: 40023378
This was perfect Scott. The level of detail in your response made it flawless. Thank you so much.
0
 

Author Closing Comment

by:damixa
ID: 40023379
Great response, thanks a bunch
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now