Hi, I found code on the Internet for creating an audit log. Works great except it does not record if you edit a value to become blank. I tried a few things and can't get it to work. Can anyone help out? Thanks in advance. - Sub AuditChanges(IDField As String, UserAction As String)
-
On Error GoTo AuditChanges_Err
-
Dim cnn As ADODB.Connection
-
Dim rst As ADODB.Recordset
-
Dim ctl As Control
-
Dim datTimeCheck As Date
-
Dim strUserID As String
-
Set cnn = CurrentProject.Connection
-
Set rst = New ADODB.Recordset
-
rst.Open "SELECT * FROM tblAuditTrail", cnn, adOpenDynamic, adLockOptimistic
-
datTimeCheck = Now()
-
strUserID = Environ("USERNAME")
-
Select Case UserAction
-
Case "EDIT"
-
For Each ctl In Screen.ActiveForm.Controls
-
If ctl.Tag = "Audit" Then
-
If Nz(ctl.Value) <> Nz(ctl.OldValue) Then
-
With rst
-
.AddNew
-
![DateTime] = datTimeCheck
-
![UserName] = strUserID
-
![FormName] = Screen.ActiveForm.Name
-
![Action] = UserAction
-
![RecordID] = Screen.ActiveForm.Controls(IDField).Value
-
![FieldName] = ctl.ControlSource
-
![OldValue] = ctl.OldValue
-
![NewValue] = ctl.Value
-
.Update
-
End With
-
End If
-
End If
-
Next ctl
-
Case Else
-
With rst
-
.AddNew
-
![DateTime] = datTimeCheck
-
![UserName] = strUserID
-
![FormName] = Screen.ActiveForm.Name
-
![Action] = UserAction
-
![RecordID] = Screen.ActiveForm.Controls(IDField).Value
-
.Update
-
End With
-
End Select
-
AuditChanges_Exit:
-
On Error Resume Next
-
rst.Close
-
cnn.Close
-
Set rst = Nothing
-
Set cnn = Nothing
-
Exit Sub
-
AuditChanges_Err:
-
MsgBox Err.Description, vbCritical, "ERROR!"
-
Resume AuditChanges_Exit
-
End Sub
-
19 2767 jimatqsi 1,273
Recognized Expert Top Contributor
barbaro,
This code looks pretty good. You don't really tell us anything about the nature of the problem. Does it fail to make an audit record when you change any field at all to be blank? The problem is presumably occurring at - If Nz(ctl.Value) <> Nz(ctl.OldValue) Then
but we don't have enough information to say that for sure.
Do you know how to run debug in the code? If you go to that line and hit the F9 key you'll set a debug stop. Then run the code and when that line is to be executed the debugger will come up (current line highlighted in yellow). You can then get variable values by pointing at a variable or in the immediate window (control/g) - type ?variablename to get the value. F8 to move to the next line in the code (or F10, depending on what version you're in). Use the Debug menu to guide you and look at debugging in Help. Educate yourself about debug, try a few things and come back with some more specific questions.
Jim
zmbd 5,501
Recognized Expert Moderator Expert
Actually,
I recommend against using the [F9] stop point.
It has been documented to cause issues with the compiler.
Instead, insert the STOP command in-between lines 7 and 8 (just before the first SET command).
From there proceed as jimatqsi has suggested.
Please take a look at the trouble shooting section here: > Before Posting (VBA or SQL) Code
AND take a look at the trouble shooting articles in the Microsoft Access / VBA Insights Sitemap listing (open the page and then use <ctrl><f> to open the page search to find "debugging in vba" or look for 129 thru 139...
Hi,
The code works fine when I change any field from "Value 1" to "Value 2". The change gets recorded properly in the audit table. If I change "Value 2" to be null (blank), the change is not recorded in the table. I believe it is the line jimatqsi suggested and I have tried -
If Nz(ctl.Value, "") <> Nz(ctl.OldValue, "") Then
but that didn't do anything. I'm having a hard time doing anything with debug as it won't let me run the code for some reason. Any other thoughts?
zmbd 5,501
Recognized Expert Moderator Expert
How are you calling the code?
jimatqsi 1,273
Recognized Expert Top Contributor
What do you mean by "it won't let me run the code "? Are you able to set the stop point with F9 (line of code turns brown)? Then when you run the code (open the form if it's not open, make a change to a field, do whatever event is required to call Sub AuditChanges) it should start the process but stop with the line highlighted in yellow. At what point are you prevented from running your code?
Jim
I can make the stop point and have done so. I then keep the code open but minized and on the form, remove a value from a field and save the record. I don't see anything that is happening with the code. Sorry but I am self taught.
jimatqsi 1,273
Recognized Expert Top Contributor
Perhaps if you switch windows to the code you'll see it is waiting at a yellow highlighted line. Or else your Sub is not being called in this instance.
If the program is not sitting at the break point then we have to find out what calls your auditing logic. If you can tell us that (preferably post the code that calls AuditChanges). It could be from within the code running when you save a record.
Do the reading that zmbd listed for you. You'll learn a lot and be glad for it.
Jim
When I try what you suggested, I do not see a yellow line in the code. The code is called from the BeforeUpdate Eventof the form. - Private Sub Form_BeforeUpdate(Cancel As Integer)
-
Call AuditChanges("StuffID", "EDIT")
-
End Sub
I'll be reading that tonight and really appreciate all that I learn here.
zmbd 5,501
Recognized Expert Moderator Expert
barbarao,
1) Before you do anything else to your project, please, please, please, please complete all of the trouble shooting and configuration steps given in: > Before Posting (VBA or SQL) Code. If you will not do this, then we will be chasing our tails for a 100 posts!
2) Please do !not! use the [F9] method of setting the stop/break point. I understand that many people do; however, it somtimes fails and if certain conditions are met, the VBA code can be corrupted. If you have used this method, please go back into your code and clear any of those "red" dots. 2a) Place the STOP command as the very FIRST command in your "Form_BeforeUpd ate" so that you get something like this: - Private Sub Form_BeforeUpdate(Cancel As Integer)
-
'>> Debug Stop
-
STOP
-
'>>
-
Call AuditChanges("StuffID", "EDIT")
-
End Sub
if this does not cause the code to enter the debug mode and highlight the call to the AuditChanges then we need to start checking your program settings... once again, if you will follow all of the provisions in the > Before Posting (VBA or SQL) Code link for trouble shooting most of this will fix itself. Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Zlatko Matić |
last post by:
I tried to implement triggers for filling audit-trail table on this way.
Everything works fine as long as I don't update the primary key field value.
When I try to update PK value, an error occures.
The code is the following:
CREATE TRIGGER NameOfTheTrigger
ON dbo.TableName FOR DELETE, INSERT, UPDATE
AS BEGIN
declare
@type varchar(10) ,
|
by: Zlatko Matić |
last post by:
Hello.
I tried to implement audit trail, by making an audit trail table with the
following fileds:
TableName,FieldName,OldValue,NewValue,UpdateDate,type,UserName.
Triggers on each table were set to do the job and everything was fine except
that in the audit trail you couldn't know which row exacltly was
updated/inserted/deleted...Therefore...
|
by: bruboj |
last post by:
I created an audit trail for my access 97 database using code I found
at: http://members.iinet.net.au/~allenbrowne/AppAudit.html
One of the limitations stated for the code is "each table to be audited
must have an AutoNumber primary key". Can someone explain why the
automunber field has to be the primary key? It seems that the
autonumber...
|
by: allyn44 |
last post by:
Helllo--I have implemented the audit trail from the Microsoft KB
article that records changes on a data entry form to a memo filed in
the fieeld's table record. What I would like to do is pull certain
info from that module and put it in a separate table--is this
possible?
tHANKS,
bOB
Explanations in caps in code
|
by: Parag |
last post by:
Hello,
I have been assigned the task to design the audit trail for the ASP.NET web
application. I don't know what the best practices for such audit trails are.
Our application one dedicated user name and password to perform the database
operations. I need to capture all the operations which are performed on the
database. Also I need to able...
| |
by: philmgron |
last post by:
Hello
I have been hitting my head against the wall on this problem for a day
now. I have a simple table that stores cities, on of the fields on the
table is modified_by. I am trying to write the user who modifed the
table, in to the column modified_by. Code for getting the user that i
am using is environ("username"), it works fine; however...
|
by: hary08 |
last post by:
im doing a database for Hospital Admission, I have a log in form which prompt user for a password. The source of log in is to look for the values in my Table tblEmployees and match user name and password entered.My case now is I have Audit Trail Module which keeps records of modifications and current user, I wanted my audit trail to log the...
|
by: Drewpowens |
last post by:
My code works, however, if a value that is already in the database is deleted, the change is not logged in the audit trail. I know it is my code (see below), please modify to also include already entered values that are deleted (e.g., Oldvalue is not null but is changed to null):
If TypeOf ctlC Is TextBox Or TypeOf ctlC Is ComboBox Or TypeOf...
|
by: babamc4 |
last post by:
I have a main form (mainformlung) with 5 subforms (followupacute, followuplate, biochemresults, haemresults and pftresults). I have copied Allen Browne's Audit Trail code (thanks to Allen Browne) and this is working great, edit, insert etc is working bar when I try to delete a record in one of my subforms (I'm in test stage at the mo) I get a run...
|
by: flower88 |
last post by:
I have this code to keep track of the changes in a form. It is suppose to tell me what was changed, when, and by whom. I have this code as a module
Public Function Audit_Trail()
On Error GoTo Err_Audit_Trail
Dim MyForm As Form
Dim ctl As Control
Dim sUser As String
Set MyForm = Screen.ActiveForm
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |