Solved IF OR AND statement vba, multiple conditions (1 Viewer)

Jordonjd

Member
Local time
Tomorrow, 00:46
Joined
Jun 18, 2020
Messages
96
I would without fail change the field name of [-/+] .
That field name will cause you some problems further down the line I'm sure.

It breaks about 276 rules on field naming good practice - if you had added some spaces you would have had a full house! šŸ˜
its just the tip of the iceberg for me haha
 

Jordonjd

Member
Local time
Tomorrow, 00:46
Joined
Jun 18, 2020
Messages
96
If I was doing a lot of this like you are I would make myself some helper functions that would save me a lot of time and make it much easier to read and to write.

To see if a field is blank (null, "" or spaces)
Code:
Public Function IsBlank(val As Variant) As Boolean
  'Check for Null, "", and a space/s
  IsBlank = (Trim(val & "") = "")
End Function

Then wrap it in another function for not blank
Public Function IsNotBlank(val As Variant) As Boolean
IsNotBlank = Not IsBlank(val)
End Function

To see if any field in a group is blank
Code:
Public Function IsAnyBlank(ParamArray Vals() As Variant) As Boolean
  Dim i As Integer
  For i = 0 To UBound(Vals)
    If IsBlank(Vals(i)) Then
      IsAnyBlank = True
      Exit Function
    End If
  Next i
End Function

To see if all are blank
Code:
Public Function AreAllBlank(ParamArray Vals() As Variant) As Boolean
  Dim i As Integer
  AreAllBlank = True
  For i = 0 To UBound(Vals)
    If Not IsBlank(Vals(i)) Then
      AreAllBlank = False
      Exit Function
    End If
  Next i
End Function

These can then be reused anywhere in your project. And the chance of making a mistake is minimized

You code becomes very readable
Code:
Select Case Me.Move_Reason

    Case "Order", "Repair", "ForParts", "Disassembly"
        If PM = "+" Or IsAnyBlank(FromLoc, Ref) Or IsNotBlank(ToLoc) Then
            MsgBox "Error! Please check, At least one field is incorrect"
           DoCmd.CancelEvent
        End If


    Case "Goods-In-Testing", "Movement"
        If IsAnyBlank(FromLoc, ToLoc) Or IsNotBlank(Ref) Then
           MsgBox "Error! Please check, At least one field is incorrect"
           DoCmd.CancelEvent
        End If
     

    Case "Return", "Finished-Goods", "FG-Rental", "Costed-Return"
        If PM = "-" Or IsNotBlank(FromLoc) Or IsAnyBlank(ToLoc, Ref) Then
          MsgBox "Error! Please check, At least one field is incorrect"
          DoCmd.CancelEvent
        End If
     

    Case "Lend-Issue", "Scrap"
        If PM = "+" Or IsBlank(FromLoc) Or IsNotBlank(ToLoc) Then
           MsgBox "Error! Please check, At least one field is incorrect"
           DoCmd.CancelEvent
        End If
     
     
    Case "Lend-Return", "SN-Capture"
        If Me.[=/-] = "-" Or IsBlank(ToLoc) Or IsNotBlank(FromLoc) Then
           MsgBox "Error! Please check, At least one field is incorrect"
           DoCmd.CancelEvent
        End If
 

    Case "Recount"
        If PM = "+" Then
            If IsNotBlank(FromLoc) Or IsBlank(ToLoc) Then
            MsgBox "Error! Please check, At least one field is incorrect"
            DoCmd.CancelEvent
         End If
     
        If PM = "-" Then
            If IsBlank(FromLoc) Or IsNotBlank(ToLoc) Then
              MsgBox "Error! Please check, At least one field is incorrect"
              DoCmd.CancelEvent
            End If
        End If
  End Select
End Sub
Its going to take me a few days to understand these as I cant read them, but thanks I will definitely try and figure them out and utilize them
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:46
Joined
May 21, 2018
Messages
8,522
Its going to take me a few days to understand these as I cant read them, but thanks I will definitely try and figure them out and utilize them
Not necessary, just showing some techniques to use in the future. Allows you to take a bite at a time out of the elephant, instead of trying it in one gulp.
 

Users who are viewing this thread

Top Bottom