473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Audit script will not apply a value when updating a blank field

Hi
I have an auditing script that creates a before and after value to records updated via a form and places them into another table. This way changes to the database can be tracked.
It works fine, until a blank textbox field is updated to contain a value. So if the field being updated doesn't contain a value, it doesnt put the before and after values into the audit table.

Here is the code:
Expand|Select|Wrap|Line Numbers
  1. Sub AuditTrail(frm As Form, recordid As Control)
  2.  
  3. Dim ctl As Control
  4. Dim varBefore As Variant
  5. Dim varAfter As Variant
  6. Dim strControlName As String
  7. Dim strSQL As String
  8.  
  9. On Error Resume Next
  10.  
  11. 'Get changed values.
  12. For Each ctl In frm.Controls
  13.  
  14. With ctl
  15.  
  16. Select Case .ControlType
  17.  
  18. Case acTextBox
  19. If .Value <> .OldValue Then
  20. varBefore = Nz(ctl.OldValue, poo)
  21. varAfter = Nz(ctl.Value, poo)
  22. strControlName = .Name
  23. 'Build INSERT INTO statement.
  24. strSQL = "INSERT INTO " _
  25. & "Audit (EditDate, User, RecordID, SourceTable, " _
  26. & " SourceField, BeforeValue, AfterValue) " _
  27. & "VALUES (Now()," _
  28. & cDQ & Environ("username") & cDQ & ", " _
  29. & cDQ & recordid.Value & cDQ & ", " _
  30. & cDQ & frm.RecordSource & cDQ & ", " _
  31. & cDQ & .Name & cDQ & ", " _
  32. & cDQ & varBefore & cDQ & ", " _
  33. & cDQ & varAfter & cDQ & ")"
  34. 'View evaluated statement in Immediate window.
  35. Debug.Print strSQL
  36. DoCmd.SetWarnings False
  37. DoCmd.RunSQL strSQL
  38. DoCmd.SetWarnings True
  39. End If
  40.  
  41.  
  42. Case acCheckBox
  43.  
  44. If .Value <> .OldValue Then
  45. varBefore = .OldValue
  46. varAfter = .Value
  47. strControlName = .Name
  48. 'Build INSERT INTO statement.
  49. strSQL = "INSERT INTO " _
  50. & "Audit (EditDate, User, RecordID, SourceTable, " _
  51. & " SourceField, BeforeValue, AfterValue) " _
  52. & "VALUES (Now()," _
  53. & cDQ & Environ("username") & cDQ & ", " _
  54. & cDQ & recordid.Value & cDQ & ", " _
  55. & cDQ & frm.RecordSource & cDQ & ", " _
  56. & cDQ & .Name & cDQ & ", " _
  57. & cDQ & varBefore & cDQ & ", " _
  58. & cDQ & varAfter & cDQ & ")"
  59. 'View evaluated statement in Immediate window.
  60. Debug.Print strSQL
  61. DoCmd.SetWarnings False
  62. DoCmd.RunSQL strSQL
  63. DoCmd.SetWarnings True
  64. End If
  65.  
  66. End Select
  67.  
  68. End With
  69.  
  70. Next
  71.  
  72. Set ctl = Nothing
  73. Exit Sub
  74.  
  75. End Sub
  76.  
I have spent a considerable amount of time on this and would appreciate if anyone could help.
Thanks
Oct 13 '10 #1
1 1258
danp129
323 Expert 256MB
Remove "On error resume next", what happens?

Set a break point and step through the code on an update that you know will cause the problem... What is the old/new value? What happens when it tries to compare the old/new value with and without "On error resume next"?

"On Error Resume Next" should almost never be used. When it is used, it should precede only the line of code that can cause an error that cannot otherwise be handled or you know the there is no possibility of it continuing causing an issue. After such error causing lines, add "On Error Goto 0".

At a bare minimum, I would have:
Expand|Select|Wrap|Line Numbers
  1. Sub MySub
  2. On Error Goto MyErrorTrap:
  3. ' Your code below
  4. x=1*"B"
  5. ' Your code above
  6. exit sub
  7. MyErrorTrap:
  8. msgbox "There was an error"
  9. resume next
  10. end sub
Here's an example you can learn more from
Expand|Select|Wrap|Line Numbers
  1. Sub test()
  2.     On Error GoTo ErrorTrap:
  3. StartOver: ' Optional, used to show second example
  4. ' vvvv YOUR CODE HERE vvvv
  5.  
  6.  
  7.     x = "A" * 1 ' Cause first error
  8.  
  9.     MsgBox "Continuing after first error"
  10.  
  11.     ' Cause another error
  12.     x = "A" * 1
  13.  
  14.     MsgBox "Continuing after second error"
  15.  
  16.  
  17.  
  18. ' ^^^^ YOUR CODE HERE ^^^^
  19.  
  20.  
  21.     Exit Sub ' Your code is done, so exit the procedure
  22.  
  23. ' Your error trap
  24. ErrorTrap:
  25.     ' Show the error if you want
  26.     MsgBox "Runtime Error: '" & Err.Number & "'" & Replace(Space(2), " ", vbCrLf) & Err.Description, vbExclamation, "My Application Title"
  27.  
  28.     Dim ret As VbMsgBoxResult
  29.  
  30.     ' Ask if they'd like to continue
  31.     ret = MsgBox("It appears an error has occured, would you like to attempt to continue the process with possibly unexpected results?", vbYesNo)
  32.  
  33.     ' Resume the code just like "On Error Resume Next" does if the user clicked Yes
  34.     If ret = vbYes Then Resume Next
  35.  
  36.     ' User hit No on previous message box if we made it to here...
  37.  
  38.     ' Here's an example of resuming at a label to go do a different section of code
  39.     ret = MsgBox("Would you like to start over from the beginning?")
  40.     If ret = vbYes Then Resume StartOver:
  41.     ' It did not have to start from beginning, just whatever was a safe place to resume
  42.  
  43.     ' User hit No on previous message box if we made it to here...
  44.  
  45.     MsgBox "You have said no on both questions, in some cases, quitters really do win!"
  46. End Sub
  47.  
Oct 14 '10 #2

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

Similar topics

1
by: Jim | last post by:
I have a 2 checkboxes and a hidden field..what I want to happen is that you can only click on 1 of these checkboxes at a time and when you check a checkbox it will assign the hidden field...
3
by: Poul Møller Hansen | last post by:
Hi, I need an auto incrementing field that will contain values like N000001, N000002, N000003 etc. I think the way is to use the value from an identity field in a stored procedure that is...
5
by: Elvis V. | last post by:
Good morning, I have a table with three fields, Buildings, Floors and Rooms. This is what I would like to do; in my form when I select Building1 in my drop down box for Buildings, when I go to the...
4
by: R Bolling | last post by:
Hi, I have a table where two fields will make up the primary key. In a few cases the second field will be blank. When I try to append the existing data into the keyed table I get the message that...
1
by: j.mandala | last post by:
I have a memo field in an appointment application that stores session notes for group psychotherapy sessions. Each attendee of the group has an appointment record. I want to be able to write a...
5
by: JollyK | last post by:
Hello all, I have always been having this issue and wondering what the solution is. When I set the enableviewstate property to false for a textbox, the textbox always retains its value after a...
4
by: Magnus Blomberg | last post by:
Hello! I have a problem when using a hidden field to send a value to the server. Below you can see my code in simplyfied versions. What I'm trying to do is: 1. The user browses for a picture...
14
by: Lars Netzel | last post by:
A little background: I use three Datagrids that are in a child parent relation. I Use Negative Autoincrement on the the DataTables and that's workning nice. My problem is when I Update these...
1
by: evilbungle | last post by:
Hi, I am sure this should be easy but can't get it working, I have a database which needs to import fields from a spreadsheet, I can open the spreadsheet and load the fields I want to into an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.