VBA

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there an easier way to code:

f Me!txtActivityCode = "EMAIL" Or Me!txtActivityCode = "INCAL" Or
Me!txtActivityCode = "TMADJ" Or Not (IsNull(Me!txtComments)) Then ...
 
Layout helps:


If Me.txtActivityCode.Value = "EMAIL" _
Or Me.txtActivityCode.Value = "INCAL" _
Or Me.txtActivityCode.Value = "TMADJ" _
Or Not IsNull(Me.txtComments.Value) Then

....
 
I just wanted to make sure I wasn't overlooking a way to avoid repeating the
source you're checking. I was hoping for something like the TransSQL IN().

e.g. WHERE state IN ('CA', 'IN', 'MD')
 
mlwallin said:
I just wanted to make sure I wasn't overlooking a way to avoid
repeating the source you're checking. I was hoping for something
like the TransSQL IN().

e.g. WHERE state IN ('CA', 'IN', 'MD')

If you really want to, and if none of the values contains any of the
other values, you can use InStr(), like this:

If InStr(Me!txtActivityCode, "/EMAIL/INCAL/TMADJ/") > 0 _
Or Not IsNull(Me.txtComments.Value) _
Then
' ...
End If

You have to pick a delimiter that will not appear in the values to be
tested. I doubt that will be as fast as just testing the individual
values.

You can also use a Select Case structure, but since you've got an
additional test that doesn't involve Me!txtActivityCode, the code
becomes more elaborate:

Dim fDoIt As Boolean

Select Case Me!txtActivityCode
Case "EMAIL", "INCAL", "TMADJ"
fDoIt = True
Case Else
If Not IsNull(Me.txtComments.Value) Then
fDoIt = True
End If
End Select

If fDoIt Then
' ...
End If
 
If InStr(Me!txtActivityCode, "/EMAIL/INCAL/TMADJ/") > 0 _
Or Not IsNull(Me.txtComments.Value) _
Then

This can give you false positives, e.g. "MAIL", "ADJ". For safety use
something like

If InStr("/" & Me!txtActivityCode & "/", ...
 
John Nurick said:
This can give you false positives, e.g. "MAIL", "ADJ". For safety use
something like

If InStr("/" & Me!txtActivityCode & "/", ...

You're right. I meant to write that, but goofed.
 
Back
Top