On Current

Hi Experts,
I have a question about the code for an Address form.  Currently in my database, I have a Address form for Client but users keep delete the old address and enter the new address instead keep the old address and add the new one (the system will keep maximum 4 address, 3 previous and 1 mailing, anything over 4 address, the system will automatic removed the oldest address).  so we want to lock the address, city, state and Zip code, users have to add new address except for supervisors.  I put the code on Form Current but some times when user enter and realized enter it wrong and use the "ESC" Key to cancel, some times not save but some times save.  what did I do wrong on the code or I should not put the code under On Current?  below is my code

Private Sub Form_Current()

Dim stUser As String
Dim stSecurity As Integer
stUser = GetCaresUser
stSecurity = DLookup("SecurityType", "tblLoginSecurity", "LoginID = '" & stUser & "'")

If Not IsNull(Me.ADRADDR) Or Me.ADRADDR <> "" Then       ‘this is the Address field
   If stSecurity < 5 Then
      Me.ADRADDR.Locked = True
   End If
   Me.ADRADDR.Locked = False
End If

If Not IsNull(Me.ADRCITY) Or Me.ADRCITY <> "" Then       ‘this is the City field
   If stSecurity < 5 Then
      Me.ADRCITY.Locked = True
   End If
  Me.ADRCITY.Locked = False
End If

If Not IsNull(Me.ADRST) Or Me.ADRST <> "" Then           ‘this is the State field
   If stSecurity < 5 Then
      Me.ADRST.Locked = True
   End If
  Me.ADRST.Locked = False
End If

If Not IsNull(Me.ADRZIP) Or Me.ADRZIP <> "" Then                       ‘this is the Zip Code field
   If stSecurity < 5 Then
      Me.ADRZIP.Locked = True
   End If
  Me.ADRZIP.Locked = False
End If

End Sub

Many thanks,
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

First off, save yourself some code and make a function:
Function AddressItemChangeAllowed(FieldVar as whatevertype)
     'this says return true if security>=5 or the field is null or ""
     AddressChangeAllowed=((stSecurity>=5) OR ISNULL(FieldVar) OR FieldVar="")
End Function

Open in new window

I'm not sure what you are passing so whatevertype should be replaced by the appropriate variable type.

Then you can call it like so:
me.ADRADDR.Locked= AddressItemChangeAllowed(Me.ADRADDR)
me.ADRZIP.Locked = AddressItemChangeAllowed(Me.ADRZIP)

Open in new window


This is MUCH cleaner to deal with an non repetitive.

AS to your original code I think you might need ANDS.
If Not IsNull(Me.ADRADDR) AND Me.ADRADDR <> ""  Then       ‘this is the Address field

Open in new window

I think SS is on to something.
If Not IsNull(Me.ADRST) Or Me.ADRST <> "" Then
If EITHER of the conditions is true, then the condition is true.   So Me.ADRST having ANY non-null value including "" (a ZLS) would satisfy the first condition so a ZLS wouldn't ever get to the second If.  There are two other ways to express this:
If Not (IsNull(Me.ADRST) Or Me.ADRST = "") Then

Open in new window

If IsNull(Me.ADRST  Or Me.ADRST = "" Then
    'code here

Open in new window

I actually like the second way.  I find that it is easier for people to express compound conditions in a positive way and also easier to understand them when they are read.
Another way that reduces the expression to a single condition is to concatenate a ZLS to the field and just check for ZLS.  When you concatenate a ZLS to a null, you get a ZLS.
If Me.ADRST & "" <> "" Then

Open in new window

Gustav BrockCIOCommented:
Why reinvent the wheel? Nz is here:

    If Nz(Me!ADRADDR.Value) <> "" Then    


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.