473,549 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Audit Trail That Captures Change To Null

82 New Member
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.
Expand|Select|Wrap|Line Numbers
  1. Sub AuditChanges(IDField As String, UserAction As String)
  2.     On Error GoTo AuditChanges_Err
  3.     Dim cnn As ADODB.Connection
  4.     Dim rst As ADODB.Recordset
  5.     Dim ctl As Control
  6.     Dim datTimeCheck As Date
  7.     Dim strUserID As String
  8.     Set cnn = CurrentProject.Connection
  9.     Set rst = New ADODB.Recordset
  10.     rst.Open "SELECT * FROM tblAuditTrail", cnn, adOpenDynamic, adLockOptimistic
  11.     datTimeCheck = Now()
  12.     strUserID = Environ("USERNAME")
  13.     Select Case UserAction
  14.         Case "EDIT"
  15.             For Each ctl In Screen.ActiveForm.Controls
  16.                 If ctl.Tag = "Audit" Then
  17.                     If Nz(ctl.Value) <> Nz(ctl.OldValue) Then
  18.                         With rst
  19.                             .AddNew
  20.                             ![DateTime] = datTimeCheck
  21.                             ![UserName] = strUserID
  22.                             ![FormName] = Screen.ActiveForm.Name
  23.                             ![Action] = UserAction
  24.                             ![RecordID] = Screen.ActiveForm.Controls(IDField).Value
  25.                             ![FieldName] = ctl.ControlSource
  26.                             ![OldValue] = ctl.OldValue
  27.                             ![NewValue] = ctl.Value
  28.                             .Update
  29.                         End With
  30.                     End If
  31.                 End If
  32.             Next ctl
  33.         Case Else
  34.             With rst
  35.                 .AddNew
  36.                 ![DateTime] = datTimeCheck
  37.                 ![UserName] = strUserID
  38.                 ![FormName] = Screen.ActiveForm.Name
  39.                 ![Action] = UserAction
  40.                 ![RecordID] = Screen.ActiveForm.Controls(IDField).Value
  41.                 .Update
  42.             End With
  43.     End Select
  44. AuditChanges_Exit:
  45.     On Error Resume Next
  46.     rst.Close
  47.     cnn.Close
  48.     Set rst = Nothing
  49.     Set cnn = Nothing
  50.     Exit Sub
  51. AuditChanges_Err:
  52.     MsgBox Err.Description, vbCritical, "ERROR!"
  53.     Resume AuditChanges_Exit
  54. End Sub 
  55.  
Aug 14 '13 #1
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
Expand|Select|Wrap|Line Numbers
  1. 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
Aug 14 '13 #2
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...
Aug 14 '13 #3
barbarao
82 New Member
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
Expand|Select|Wrap|Line Numbers
  1. 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?
Aug 14 '13 #4
zmbd
5,501 Recognized Expert Moderator Expert
How are you calling the code?
Aug 14 '13 #5
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
Aug 14 '13 #6
barbarao
82 New Member
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.
Aug 14 '13 #7
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
Aug 14 '13 #8
barbarao
82 New Member
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.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.    Call AuditChanges("StuffID", "EDIT")
  3. End Sub
I'll be reading that tonight and really appreciate all that I learn here.
Aug 14 '13 #9
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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. '>> Debug Stop
  3.    STOP
  4. '>>
  5.    Call AuditChanges("StuffID", "EDIT")
  6. 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.
Aug 14 '13 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2261
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) ,
3
6265
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...
5
2496
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...
8
1967
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
6
5829
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...
6
2342
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...
3
3771
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...
2
2307
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...
6
2732
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...
0
1136
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
0
7524
marktang
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...
0
7451
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...
0
7720
Oralloy
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. ...
0
7960
jinu1996
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...
0
7812
tracyyun
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...
0
6048
agi2029
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...
0
5089
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...
0
3501
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...
0
766
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.