Solved Creating Function To Lock Text Boxes and Combo Boxes When A Form Loads

Thank you for the explanation, I think that I am following the code better.

Code:
 If IsNull(Me.cboDeptName.OldValue) Then                     
    Else
        If Me.cboDeptName = Me.cboDeptName.OldValue Then        
        Else
            MsgBox "Dept Name may not be changed after record is saved.", vbOKOnly
        Debug.Print Me.cboDeptName
            Cancel = True
            Me.Undo
            Me.cboDeptName.SetFocus
            Exit Sub
        End If
    End If

One question that I still have is the line If IsNull(Me.cboDeptName.OldValue) just checking to see if there was a value saved to the table? If so is that a kind of validation check?

In the event that there is a Dept Name recoreded to a table:

When the form is loaded, any data that is recorded on the table is populated in the corresponding field on the form . In this case, the form loads, the data that is recorded to the cboDeptName is populated on the form in the corresponding field and access moves on to the next part of the loop because the IsNull statement is false. Then if the user doesn't make any changes to cboDeptName field on the form, the If Me.cboDeptName = Me.cboDeptName.OldValue is true because there was no change to the .Text buffer which means that the .Value buffer is unchanged and access exits the loop.

If the users makes a change to the cboDeptName field on the form and user clicks away from the cboDeptName box, the .Text buffer copies the change to the .Value buffer and cboDeptName is no longer equal to the .OldValue buffer. Access displays the message that the Dept Name can't be changed, cancels the update, undoes the change to the cboDeptName, sets the focus back to the cboDeptName field and exits the before update event.
 
IMO this is really bad structure

Code:
If something then
  Do nothing
else
  Do something
end if
That is like nails on a chalkboard

Instead
Code:
If something else then
  do something
end if

Along with exit subs that adds no value since you are exiting anyways

Code:
If not IsNull(Me.cboDeptName.OldValue) Then                     
        If Me.cboDeptName <> Me.cboDeptName.OldValue Then       
            MsgBox "Dept Name may not be changed after record is saved.", vbOKOnly
            Debug.Print Me.cboDeptName
            Cancel = True
            Me.Undo
            Me.cboDeptName.SetFocus
        End If
End If
 
Interesting. I frequently write conditionals using positive questions as the OP did. I especially avoid them when they start with "not". I think it is because I don't think of the code I write as being for myself. I think of it as being for others to read and in general, people have an easier time of following positive logic than following negative logic. They also have a tendency to skip over the leading "not" and so miss the point of the condition. Therefore, unless taking a positive path gets convoluted, that's my style. "Positive Patty";)


It has to do with the fact that when you compare null to ANYTHING, the answer is Null. It is never True or False.

So If A = Null Then --- will NEVER return true, even when A is actually null. So, If Null = Null -- is never true.

Most people check for null separately. But, you can sometimes get around the issue using a couple of different techniques.

If Me.fldA & "" = Me.fldB & "" Then - By concatenating null with a ZLS (Zero Length String), you end up with a ZLS which is not null and so will produce the expected result when compared.

Null & "" = Null & "" -- will result in True
Okay, now I see what is going on with the code.

Sorry for the delay in my response, the last couple of weeks at work have been busy.

Thank you Pat for everything. With your help I was able to really mitigate the possibility of critical data accidentally being overwritten.

I'll be marking this thread as solved.

Thanks Again
 

Users who are viewing this thread

Back
Top Bottom