Fellows, I got the following function code from someone in the net and adapted the code to meet my needs. Unfortunately I lost contact with him.
As you can see the purpose of this function code is to register into the TblLogChanges every single record exclusion or record change made in any form field of my project when the form is active. In other words if the user changes for example the value of the field Color in the form FrmX from Yellow to Black, the TblLogChanges will have added a line indicating: FrmX, type of change (record change or record exclusion), RecordId (in my case CADID), Current User, Date, and a complimentary string showing Field Color old value=Yellow --> Field Color new value=Black.
It works fine when the field “old” value is populated (not null).
However, if the feld old value is null, meaning, if the field value is originally null, or yet if the field is originally empty, and I fill it up with some data, then the code does not add an indicative line into the table TblLogChanges.
See function code below.
FYI the function is called in both Before Update and Delete events of the form with the following syntaxes: =logChangeFrmCadastro("E") for record exclusions, and =logChangeFrmCadastro("A") for record changes.
I have failed so far to make it happen. Reason is I'm able to read and understand most written syntax but still not good enough to create my own codes since I do not know to use the range of VBA commands and syntaxes available yet. This is a knowledge gap I'm doing my best to fill in.
In the mean time does anybody could tell me please what function code changes are required so the function be able to add an indicative line into TblLogChanges when the user changes a form field from “empty/nothing” to something?
Thank you.
Code:
Option Compare Database
Option Explicit
Function LogChangeFrmCadastro(strTipo As String)
On Error Resume Next
Dim db As Database, rslog As Recordset
Dim frm As Form, I As Integer
Dim strLog As String
Set db = CurrentDb
Set rslog = db.OpenRecordset("TblLogChanges")
Set frm = Screen.ActiveForm
For I = 0 To frm.Controls.Count - 1
If TypeOf frm.Controls(I) Is TextBox Or TypeOf frm.Controls(I) Is ComboBox Or TypeOf frm.Controls(I) Is CheckBox Or TypeOf frm.Controls(I) Is OptionGroup Or TypeOf frm.Controls(I) Is ListBox Then
If strTipo = "E" Then
If strLog = "" Then
strLog = frm.Controls(I).Name & " " & frm.Controls(I).Value
Else
strLog = strLog & "," & frm.Controls(I).Name & ":" & frm.Controls(I).Value
End If
Else
If frm.Controls(I).OldValue <> frm.Controls(I).Value Then
If strLog = "" Then
strLog = strLog & "CADID = " & frm.Controls("CADID") & "," & frm.Controls(I).Name & ":" & frm.Controls(I).OldValue & " --> " & frm.Controls(I).Value
Else
strLog = strLog & "," & frm.Controls(I).Name & ":" & frm.Controls(I).OldValue & " --> " & frm.Controls(I).Value
End If
End If
End If
End If
Next
rslog.AddNew
rslog("strNomeForm") = frm.Name
rslog("strTipoLog") = strTipo
rslog("mmolog") = strLog
rslog("strUser") = CurrentUser
rslog("dtLog") = Now
On Error Resume Next
rslog.Update
rslog.Close
db.Close
End Function