Solved

insert record on update vba

Posted on 2014-04-25
3
450 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
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…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

809 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