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. - Sub AuditChanges(IDField 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")
-
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
-
![RecordID] = Screen.ActiveForm.Controls(IDField).Value
-
![FieldName] = ctl.ControlSource
-
![OldValue] = ctl.OldValue
-
![NewValue] = ctl.Value
-
.Update
-
End With
-
End If
-
End If
-
Next ctl
-
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
5 2412
You also need to loop through the ActiveForm.[Enter subform control name here].Form.Controls array as well
Hi Rabbit,
I am sorry i didn't get you exactly.
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.
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. - Option Explicit
-
Const txtTableName = "yoursubformname"
- 'Choose a field in subform and adapt it in the "BeforeUpdate"
-
Private Sub InvoiceQty_BeforeUpdate(Cancel As Integer)
-
On Error GoTo InvoiceQty_BeforeUpdate_Err
-
WriteAuditUpdate txtTableName, Me.IDField, "InvoiceQty", Me.InvoiceQty.OldValue, Me.InvoiceQty.Value
-
InvoiceQty_BeforeUpdate_Exit:
-
Exit Sub
-
-
InvoiceQty_BeforeUpdate_Err:
-
'Err.Clear
-
MsgBox Error$
-
Resume InvoiceQty_BeforeUpdate_Exit
-
End Sub
In a module i use something like this that you can adapt - Sub WriteAuditUpdate(txtTableName, lngRecordNum, txtFieldName, OrgValue, CurValue)
-
Dim db As DAO.Database
-
Dim rs As DAO.Recordset
-
Set db = CurrentDb
-
Set rs = db.OpenRecordset("AuditTable")
-
-
rs.AddNew
-
rs!TableName = txtTableName
-
rs!RecordPrimaryKey = lngRecordNum
-
rs!FieldName = txtFieldName
-
' rs!LoginName = GetCurrentUserName
-
rs!LoginName = [Forms]![Home]![capt2]
-
rs!MachineName = GetComputerName
-
rs!Transaction = [Forms]![frmEdittransN]![Text8]
-
rs!User = CurrentUser
-
rs!OriginalValue = OrgValue
-
rs!NewValue = CurValue
-
rs!DateTimeStamp = Now()
-
rs!Code = [Forms]![frmEdittransN]![frmNewOrderAddTrans]![ICN]
-
rs.Update
-
rs.Close
-
db.Close
-
End Sub
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 : - If Not TypeOf Screen.ActiveControl.Parent Is Form Then Exit Sub
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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:
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...
|
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,...
|
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...
|
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...
|
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,...
| |