Solved

insert record on update vba

Posted on 2014-04-25
3
448 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
Comment Utility
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
Comment Utility
This was perfect Scott. The level of detail in your response made it flawless. Thank you so much.
0
 

Author Closing Comment

by:damixa
Comment Utility
Great response, thanks a bunch
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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.

762 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

8 Experts available now in Live!

Get 1:1 Help Now