473,395 Members | 1,987 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,395 software developers and data experts.

Audit Trail - SubForm

satifali
Hi,
Following code is working perfectly when form is used as main but when inserted as sub form it works only for main form field & doesn't record anything for sub form fields. What necessary changes i might need to do in this.

Expand|Select|Wrap|Line Numbers
  1. Sub AuditChanges(IDField 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.     For Each ctl In Screen.ActiveForm.Controls
  14.         If ctl.Tag = "Audit" Then
  15.             If Nz(ctl.Value) <> Nz(ctl.OldValue) Then
  16.                 With rst
  17.                     .AddNew
  18.                     ![DateTime] = datTimeCheck
  19.                     ![UserName] = strUserID
  20.                     ![FormName] = Screen.ActiveForm.Name
  21.                     ![RecordID] = Screen.ActiveForm.Controls(IDField).Value
  22.                     ![FieldName] = ctl.ControlSource
  23.                     ![OldValue] = ctl.OldValue
  24.                     ![NewValue] = ctl.Value
  25.                     .Update
  26.                 End With
  27.             End If
  28.         End If
  29.     Next ctl
  30. AuditChanges_Exit:
  31.     On Error Resume Next
  32.     rst.Close
  33.     cnn.Close
  34.     Set rst = Nothing
  35.     Set cnn = Nothing
  36.     Exit Sub
  37. AuditChanges_Err:
  38.     MsgBox Err.Description, vbCritical, "ERROR!"
  39.     Resume AuditChanges_Exit
  40. End Sub
Dec 1 '15 #1
5 2412
Rabbit
12,516 Expert Mod 8TB
You also need to loop through the ActiveForm.[Enter subform control name here].Form.Controls array as well
Dec 1 '15 #2
Hi Rabbit,
I am sorry i didn't get you exactly.
Dec 5 '15 #3
Rabbit
12,516 Expert Mod 8TB
You're looping through the parent form controls, you also need to loop through the subform controls by accessing it's controls per the above.
Dec 5 '15 #4
neelsfer
547 512MB
If you do not come right with Rabbit's adjustments then look at this example that i downloaded and adapt your code accordingly. I struggled with the exact same problem (and code) for days and easily managed to sort it out, using this method
In the subform adapt code like this. See example of audittable for fields where changes are saved.
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Const txtTableName = "yoursubformname"
Expand|Select|Wrap|Line Numbers
  1. 'Choose a field in subform and adapt it in the "BeforeUpdate"
  2. Private Sub InvoiceQty_BeforeUpdate(Cancel As Integer)
  3. On Error GoTo InvoiceQty_BeforeUpdate_Err
  4. WriteAuditUpdate txtTableName, Me.IDField, "InvoiceQty", Me.InvoiceQty.OldValue, Me.InvoiceQty.Value
  5. InvoiceQty_BeforeUpdate_Exit:
  6.     Exit Sub
  7.  
  8. InvoiceQty_BeforeUpdate_Err:
  9. 'Err.Clear
  10.    MsgBox Error$
  11.     Resume InvoiceQty_BeforeUpdate_Exit
  12. End Sub
In a module i use something like this that you can adapt
Expand|Select|Wrap|Line Numbers
  1. Sub WriteAuditUpdate(txtTableName, lngRecordNum, txtFieldName, OrgValue, CurValue)
  2. Dim db As DAO.Database
  3. Dim rs As DAO.Recordset
  4. Set db = CurrentDb
  5. Set rs = db.OpenRecordset("AuditTable")
  6.  
  7.     rs.AddNew
  8.     rs!TableName = txtTableName
  9.     rs!RecordPrimaryKey = lngRecordNum
  10.     rs!FieldName = txtFieldName
  11.    ' rs!LoginName = GetCurrentUserName
  12.     rs!LoginName = [Forms]![Home]![capt2]
  13.     rs!MachineName = GetComputerName
  14.     rs!Transaction = [Forms]![frmEdittransN]![Text8]
  15.     rs!User = CurrentUser
  16.     rs!OriginalValue = OrgValue
  17.     rs!NewValue = CurValue
  18.     rs!DateTimeStamp = Now()
  19.    rs!Code = [Forms]![frmEdittransN]![frmNewOrderAddTrans]![ICN]
  20.     rs.Update
  21. rs.Close
  22. db.Close
  23. End Sub
Attached Files
File Type: zip AuditTrail2k(1).zip (58.7 KB, 115 views)
Dec 5 '15 #5
NeoPa
32,556 Expert Mod 16PB
Something that may not be obvious, and is probably causing your confusion, is that Screen.ActiveForm returns the main form - even if focus is on the subform. If you want to get the currently active form, including subforms, then use Screen.ActiveControl.Parent, but check that it's a form object too :
Expand|Select|Wrap|Line Numbers
  1. If Not TypeOf Screen.ActiveControl.Parent Is Form Then Exit Sub
Dec 6 '15 #6

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

Similar topics

6
by: Raphael Gluck | last post by:
Hi, Is it possible for one to program one's pages as such that when a database table is updated over the web, via a form, that an e-mail confirmation is sent to a specified address, notifying...
0
by: Me | last post by:
Hi... A much lamented question, I guess.. I'm trying to create a simple audit trail. log the changes to an SQL 2000 table, so that they are written into a mirror table. The entire record, only...
3
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...
6
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...
0
by: hary08 | last post by:
I have a module copied ftom this site, here it is: Option Compare Database Option Explicit Public Function AuditTrail() On Error GoTo Err_Audit_Trail 'ACC2000: How to Create an Audit...
3
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...
2
by: rockdc1981 | last post by:
I dont it is possible to put this codes together.. I want to prompt the user to save the record and at the same time have a log of audit trail. the codes work out fine separately. Code for Audit...
6
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)...
20
by: Bellina | last post by:
Hi, My knowledge in Access is very limited but I have managed to build a pretty good-wroking database for my department based on help and assistance of experts from forums like this one. A bit of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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...
0
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,...
0
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...
0
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...
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.