Question : Creating Log table

Hi

If possible please also put your opinion if my method is good or not. So I learn of my mistakes :)

Okay this can take a while to read, so here I go.


I have several tables which links to corresponding forms (tbl_article, frm_article; tbl_group, frm_group, ...).

What I try to create is the following: I would like to Log each specific creation/modification.
So in each table I added these fields:

ArtAdD - Date/Time - Now()
ArtAdU - Text - =Environ("UserName")
ArtMdD - Date/Time
ArtMdU - Text - =Environ("UserName")

So the first three letters depend on the name of the table.
AdD = creation date
AdU = logged user who created the object
MdD = modification date
MdU = logged user who modified the object

--------------------------------------------------------------------------------

With that I have a table called tbl_LOG
tbl_LOG = {LogNum(PK), LogFld, LogTyp, LogDef, LogMod, LogUse, LogDat}

Recap :
LogNum(PK) - unique ID
LogFld - Field where value is created or modified (each field is unique in all tables)
LogTyp - wether it's 'CREATED' or 'MODIFY'
LogDef - Default value when created or before modification (may be NULL when object is created)
LogMod - New value (may be NULL when object is created)
LogUse - User who created or modified - AdU or MdU
LogDat - Date Modified or created - AdD or MdD

---------------------------------------------------------------------------------

Firstly I thought setting for each textfield on the form a duplicate.
The duplicate would contain the data when it's created or last modified.

When a user modifies a value a Vb script would start running in the AfterUpdate()
Copy Duplicate value (from corresponding field) to "LogDef" and new value to "LogMod"
Date and user will be set in the "MdD" and "MdU" and then copied to "LogDat" & "LogUse".

---------------------------------------------------------------------------------

What are your thoughts?
Cause the problem I have now is when I open my design view, I have a massive amount of textfields (that I hide for the user) just to copy the values.

Any ideas?


Kind regards
Peter

Answer : Creating Log table

Just off the top of my head and might need some testing and debugging... hopefully my copious comments are good enough to explain the process though.

I hope you find this useful.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    LogAllChanges
End Sub
 
Private Sub LogAllChanges()
    Dim ctl As Control
 
    On Error GoTo Catch
    
    'Loop through all controls on the form
    For Each ctl In Me.Controls
    
        'Check if there is a ControlSource set (we are only interested in bound controls)
        'An error will occur here for controls without the ControlSource property (labels, etc) - so we
        'catch the error and skip to the next control
        If ctl.ControlSource <> "" Then
        
            'Check if the control value has changed (handle nulls with nz)
            If Nz(ctl.Value, "") <> Nz(ctl.OldValue, "") Then
            
                'Log the change
                LogChange ctl.ControlSource, ctl.Value, ctl.OldValue, Me.NewRecord
            End If
        End If
        
Skip_Control:
    Next
    
    Exit Sub
Catch:
    Resume Skip_Control
End Sub
 
Private Sub LogChange(ByVal FieldName As String, ByVal Value As Variant, ByVal OldValue As Variant, ByVal IsNewRecord As Boolean)
    'Your code to write the changes to the log here
End Sub
Random Solutions  
 
programming4us programming4us