I'm finding a world of hurt when it comes to null. I've set up my code to
add a note to a string for every field it checks.
I'm finding for example when the Cell field (text) is empty and I change the
Note field from null to something, the form thinks both Cell and Note
changed. When I change Cell from anything to anything and don't touch notes,
it again thinks both have changed.
Why we are doing this is because if someone deletes a field by accident and
puts the value back, the system should not indicate a change was made to the
record. We have to provide PAINSTAKINGLY DETAILED records back to our
auditors - we don't want the auditors looking at a record and seeing that we
indicate there was a change when there really wasn't a change made. It's not
that they get credit for every change - it's that our system will reflect
"Bob changed this record on the 1st" and when the auditors pull the record
from the 31st and compare it ot the 1st they'll see no changes.
You'll love this - let me post the new code I run right now:
Option Compare Database
Dim PreFirstName, PreLastName, PreTitle, PreInstitution, PreAddress,
PreCity, PreState, PreZip As String
Dim PrePortal, PreFDF, PreSLATE, PreManagement, PreAgreements, PreGeneral As
String
Dim PreEMail, PreTelephone, PreFax, PreCell, PreNotes As String
Dim PreExtension As Integer
Private Sub Form_AfterUpdate()
Call CheckForChanges
End Sub
Private Sub Form_Current()
PreFirstName = Nz(Me.First_Name, "")
PreLastName = Nz(Me.Last_Name, "")
PreTitle = Nz(Me.Title, "")
PreInstitution = Nz(Me.Institution, "")
PreAddress = Nz(Me.Address, "")
PreCity = Nz(Me.City, "")
PreState = Nz(Me.State, "")
PreZip = Nz(Me.Zip, "")
PrePortal = Nz(Me.Portal_Contact, "")
PreFDF = Nz(Me.FDF_Contact, "")
PreSLATE = Nz(Me.SLATE_Contact, "")
PreManagement = Nz(Me.Management, "")
PreAgreements = Nz(Me.Agreements, "")
PreGeneral = Nz(Me.General, "")
PreEMail = Nz(Me.E_mail_address, "")
PreTelephone = Nz(Me.Telephone, "")
PreExtension = Nz(Me.Extension, 0)
PreFax = Nz(Me.Fax, "")
PreCell = Nz(Me.Cell_Phone, "")
PreNotes = Nz(Me.Notes, "")
End Sub
Private Sub CheckForChanges()
Dim VALIDATION As String
If Not (Me.First_Name = PreFirstName) Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = "FIRST NAME CHANGED"
End If
If IsNull(PreFirstName) = False And IsNull(Me.First_Name) = True Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = "FIRST NAME CHANGED"
End If
If Not (Me.Last_Name = PreLastName) Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "LAST NAME CHANGED"
End If
If IsNull(PreLastName) = False And IsNull(Me.Last_Name) = True Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "LAST NAME CHANGED"
End If
If Not (Me.Title = PreTitle) Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "TITLE CHANGED"
End If
If IsNull(PreTitle) = False And IsNull(Me.Title) = True Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "TITLE CHANGED"
End If
If Not (Me.Institution = PreInstitution) Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "INSTITUTION CHANGED"
End If
If IsNull(PreInstitution) = False And IsNull(Me.Institution) = True Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "INSTITUTION CHANGED"
End If
If Not (Me.Address = PreAddress) Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "ADDRESS CHANGED"
End If
If IsNull(PreAddress) = False And IsNull(Me.Address) = True Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "ADDRESS CHANGED"
End If
If Not (Me.City = PreCity) Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "CITY CHANGED"
End If
If IsNull(PreCity) = False And IsNull(Me.City) = True Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "CITY CHANGED"
End If
If Not (Me.State = PreState) Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "STATE CHANGED"
End If
If IsNull(PreState) = False And IsNull(Me.State) = True Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "STATE CHANGED"
End If
If Not (Me.Zip = PreZip) Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "ZIP CHANGED"
End If
If IsNull(PreZip) = False And IsNull(Me.Zip) = True Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "ZIP CHANGED"
End If
If Not (Me.Portal_Contact = PrePortal) Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "PORTAL CHANGED"
End If
If Not (Me.FDF_Contact = PreFDF) Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "FDF CHANGED"
End If
If Not (Me.SLATE_Contact = PreSLATE) Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "SLATE CHANGED"
End If
If Not (Me.Management = PreManagement) Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "MGT CHANGED"
End If
If Not (Me.Agreements = PreAgreements) Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "AGREEMENT CHANGED"
End If
If Not (Me.General = PreGeneral) Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "GENERAL CHANGED"
End If
If Not (Me.E_mail_address = PreEMail) Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "E-MAIL CHANGED"
End If
If IsNull(PreEMail) = False And IsNull(Me.E_mail_address) = True Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "E-MAIL CHANGED"
End If
If Not (Me.Telephone = PreTelephone) Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "TEL CHANGED"
End If
If IsNull(PreTelephone) = False And IsNull(Me.Telephone) = True Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "TEL CHANGED"
End If
If Me.Extension <> PreExtension Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "EXT CHANGED"
End If
If Not (Me.Fax = PreFax) Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "FAX CHANGED"
End If
If IsNull(PreFax) = False And IsNull(Me.Fax) = True Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "FAX CHANGED"
End If
'Begin Cell Phone Check Code
MESSAGE = MsgBox("Pre :*" & Me.Cell_Phone.OLDVALUE & "*" & _
Chr(13) & Chr(10) & _
"Post:*" & Me.Cell_Phone & "*" & _
Chr(13) & Chr(10) & _
"Len Pre :" & Len(Me.Cell_Phone.OLDVALUE) & _
Chr(13) & Chr(10) & _
"Len Post:" & Len(Me.Cell_Phone), vbOKOnly, "Cell Value")
If Not (Me.Cell_Phone = PreCell) Then
End If
If Not (Me.Cell_Phone = PreCell) Then
Me.ModifiedBy = CurrentUser()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "CELL CHANGED"
Me.ModifiedOn = Now()
End If
If IsNull(PreCell) = False And IsNull(Me.Cell_Phone) = True Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "CELL CHANGED"
End If
If Not (Me.Notes = PreNotes) Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "NOTES CHANGED"
End If
If IsNull(PreNotes) = False And IsNull(Me.Notes) = True Then
Me.ModifiedBy = CurrentUser()
Me.ModifiedOn = Now()
VALIDATION = VALIDATION & Chr(13) & Chr(10) & "NOTES CHANGED"
End If
MESSAGE = MsgBox(VALIDATION, vbOKOnly, "CHANGES DETECTED")
End Sub
Private Sub SaveChanges_Click()
DoCmd.Save
DoCmd.Close
End Sub
Now, when I change the Note field, it thinks both Note and Cell have
changed. When I change Cell it again thinks both Note and Cell have changed.
When I change the First Name, it thinks both First Name and Note has changed
(but not Cell).
When I exchange Me.Cell_Phone.oldvalue for PreCell and change Notes from
null to ABC while Cell had a value in it, works fine. When I then delete the
value in Cell, it returns that both Cell and Notes have changed.
Enough to make me old before my time! I've basically proven with this that
..oldvalue does NOT work reliably. I've also proven that taking the value
pre-change and trying to compare it post-change also doesn't work because
Access is somehow setting changes to fields where the cursor has never been.