More Elegant Way to Write Code

KSReynolds

Registered User.
Local time
Today, 16:50
Joined
Jul 24, 2013
Messages
27
I thought I already posted this, but I don't see it. Is there a more elegant way to write this code? I would rather not use two IF statements.

If (Not IsNull(Me.CLOS_DATE) And DLookup("logonname", "tbllogonname") <> "azura") Then
Me.AllowEdits = False
End If
If (Not IsNull(Me.CLOS_DATE) And DLookup("logonname", "tbllogonname") <> "weglhaut") Then
Me.AllowEdits = False
End If
 
You could try something like this (untested)

Code:
If (Not IsNull(Me.CLOS_DATE) And _ 
   [COLOR="Purple"]([/COLOR]DLookup("logonname", "tbllogonname") <> "azura") OR
    DLookup("logonname", "tbllogonname") <> "weglhaut")[COLOR="Purple"])[/COLOR]
Me.AllowEdits = False
End If

But 2 If statements is not really a problem.
 
I wouldnt DLookup 2 times
Code:
Logonname = DLookup("logonname", "tbllogonname") 

If (Not IsNull(Me.CLOS_DATE) And _ 
   (Logonname <> "azura") OR
    Logonname <> "weglhaut"))
Me.AllowEdits = False
End If
 
I put the code in my format, and for some reason, the code is executing the me.allowedits = false line. It is not evaluating the expression correctly. It should be bypassing the me.allowedits = false statement. HELP!

Dim logon As String
logon = DLookup("logonname", "tbllogonname")
If (Not IsNull(Me.RCTV_CMPDT) And ((logon <> "azura") Or logon <> "wolff")) Then
Me.AllowEdits = False
End If

From Immediate Window:
? logon
wolff
? Me.RCTV_CMPDT
11/23/1994
 
What you have, with your quoted data, is

If (True And ( True OR False)) Then
Me.AllowEdits=false ' so this gets executed
End if

Work on your logic
 
This is what I came up with and it seems to work.

Dim LOGON As String
LOGON = DLookup("logonname", "tbllogonname")
If (Not IsNull(Me.NEXT_S_DAT)) Then
If LOGON <> "dzura" Then
If LOGON <> "weglhaut" Then
Me.AllowEdits = False
Else
Me.AllowEdits = True
End If
End If
End If
 
alternatively you could try, I think I've got the correct True/False, but this method allows you accommodate conditions for other users that might want to edit

Code:
Dim LOGON As String
 
LOGON = DLookup("logonname", "tbllogonname")
If (Not IsNull(Me.NEXT_S_DAT)) Then
  Select Case LOGON 
    Case "dzura", "weglhaut": Me.AllowEdits = True
    Case Else: Me.AllowEdits = False
  End Select
End If

David
 

Users who are viewing this thread

Back
Top Bottom