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: -
Sub AuditTrail(frm As Form, recordid As Control)
-
-
Dim ctl As Control
-
Dim varBefore As Variant
-
Dim varAfter As Variant
-
Dim strControlName As String
-
Dim strSQL As String
-
-
On Error Resume Next
-
-
'Get changed values.
-
For Each ctl In frm.Controls
-
-
With ctl
-
-
Select Case .ControlType
-
-
Case acTextBox
-
If .Value <> .OldValue Then
-
varBefore = Nz(ctl.OldValue, poo)
-
varAfter = Nz(ctl.Value, poo)
-
strControlName = .Name
-
'Build INSERT INTO statement.
-
strSQL = "INSERT INTO " _
-
& "Audit (EditDate, User, RecordID, SourceTable, " _
-
& " SourceField, BeforeValue, AfterValue) " _
-
& "VALUES (Now()," _
-
& cDQ & Environ("username") & cDQ & ", " _
-
& cDQ & recordid.Value & cDQ & ", " _
-
& cDQ & frm.RecordSource & cDQ & ", " _
-
& cDQ & .Name & cDQ & ", " _
-
& cDQ & varBefore & cDQ & ", " _
-
& cDQ & varAfter & cDQ & ")"
-
'View evaluated statement in Immediate window.
-
Debug.Print strSQL
-
DoCmd.SetWarnings False
-
DoCmd.RunSQL strSQL
-
DoCmd.SetWarnings True
-
End If
-
-
-
Case acCheckBox
-
-
If .Value <> .OldValue Then
-
varBefore = .OldValue
-
varAfter = .Value
-
strControlName = .Name
-
'Build INSERT INTO statement.
-
strSQL = "INSERT INTO " _
-
& "Audit (EditDate, User, RecordID, SourceTable, " _
-
& " SourceField, BeforeValue, AfterValue) " _
-
& "VALUES (Now()," _
-
& cDQ & Environ("username") & cDQ & ", " _
-
& cDQ & recordid.Value & cDQ & ", " _
-
& cDQ & frm.RecordSource & cDQ & ", " _
-
& cDQ & .Name & cDQ & ", " _
-
& cDQ & varBefore & cDQ & ", " _
-
& cDQ & varAfter & cDQ & ")"
-
'View evaluated statement in Immediate window.
-
Debug.Print strSQL
-
DoCmd.SetWarnings False
-
DoCmd.RunSQL strSQL
-
DoCmd.SetWarnings True
-
End If
-
-
End Select
-
-
End With
-
-
Next
-
-
Set ctl = Nothing
-
Exit Sub
-
-
End Sub
-
I have spent a considerable amount of time on this and would appreciate if anyone could help.
Thanks
1 1258
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: - Sub MySub
-
On Error Goto MyErrorTrap:
-
' Your code below
-
x=1*"B"
-
' Your code above
-
exit sub
-
MyErrorTrap:
-
msgbox "There was an error"
-
resume next
-
end sub
Here's an example you can learn more from - Sub test()
-
On Error GoTo ErrorTrap:
-
StartOver: ' Optional, used to show second example
-
' vvvv YOUR CODE HERE vvvv
-
-
-
x = "A" * 1 ' Cause first error
-
-
MsgBox "Continuing after first error"
-
-
' Cause another error
-
x = "A" * 1
-
-
MsgBox "Continuing after second error"
-
-
-
-
' ^^^^ YOUR CODE HERE ^^^^
-
-
-
Exit Sub ' Your code is done, so exit the procedure
-
-
' Your error trap
-
ErrorTrap:
-
' Show the error if you want
-
MsgBox "Runtime Error: '" & Err.Number & "'" & Replace(Space(2), " ", vbCrLf) & Err.Description, vbExclamation, "My Application Title"
-
-
Dim ret As VbMsgBoxResult
-
-
' Ask if they'd like to continue
-
ret = MsgBox("It appears an error has occured, would you like to attempt to continue the process with possibly unexpected results?", vbYesNo)
-
-
' Resume the code just like "On Error Resume Next" does if the user clicked Yes
-
If ret = vbYes Then Resume Next
-
-
' User hit No on previous message box if we made it to here...
-
-
' Here's an example of resuming at a label to go do a different section of code
-
ret = MsgBox("Would you like to start over from the beginning?")
-
If ret = vbYes Then Resume StartOver:
-
' It did not have to start from beginning, just whatever was a safe place to resume
-
-
' User hit No on previous message box if we made it to here...
-
-
MsgBox "You have said no on both questions, in some cases, quitters really do win!"
-
End Sub
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |