Results 1 to 12 of 12
  1. #1
    d2ther is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    3

    Nested If statements and data validation...

    To make a long story short I have this form with a list-box of tests on the left that can be clicked to change to change the Sub Form on the right to insert laboratory test data into. The data is being copy-pasted from Excel sheets into the database via the Sub Form at the bottom, which works just fine an has saved us a TON of time. However... after much frustration (and arguing) with the person that designed code to check the inputted data's integrity I have decided to come to the forums for help. Here is the currently running If statement in the database:

    Code:
    Private Sub Form_AfterInsert()
    
    If [TestReading] = "PASS" Then
        If [TestReading] < [Min] Then
        [PASSorFAIL] = "FAIL"
        End If
        If [TestReading] > [Max] Then
        [PASSorFAIL] = "FAIL"
        End If
        If [TestReading] = Null Then
        [PASSorFAIL] = "PASS"
        End If
        If [TestReading] > [Min] And [TestReading] < [Max] Then
        [PASSorFAIL] = "PASS"
        End If
    End If
    
    If [TestReading] = "FAIL" Then
        If [TestReading] < [Min] Then
        [PASSorFAIL] = "FAIL"
        End If
        If [TestReading] > [Max] Then
        [PASSorFAIL] = "FAIL"
        End If
        If [TestReading] = Null Then
        [PASSorFAIL] = "PASS"
        End If
        If [TestReading] > [Min] And [TestReading] < [Max] Then
        [PASSorFAIL] = "PASS"
        End If
    End If
    
    End Sub
    
    Private Sub Form_AfterUpdate()
    DoCmd.RefreshRecord
    
    If [TestReading] = "PASS" Then
        If [TestReading] < [Min] Then
        [PASSorFAIL] = "FAIL"
        End If
        If [TestReading] > [Max] Then
        [PASSorFAIL] = "FAIL"
        End If
        If [TestReading] = Null Then
        [PASSorFAIL] = "PASS"
        End If
        If [TestReading] > [Min] And [TestReading] < [Max] Then
        [PASSorFAIL] = "PASS"
        End If
    End If
    
    If [TestReading] = "FAIL" Then
        If [TestReading] < [Min] Then
        [PASSorFAIL] = "FAIL"
        End If
        If [TestReading] > [Max] Then
        [PASSorFAIL] = "FAIL"
        End If
        If [TestReading] = Null Then
        [PASSorFAIL] = "PASS"
        End If
        If [TestReading] > [Min] And [TestReading] < [Max] Then
        [PASSorFAIL] = "PASS"
        End If
    End If
    
    End Sub
    
    Private Sub Form_Current()
    DoCmd.RefreshRecord
    
    End Sub
    So as you can see this is a simple "nested If statement" that checks each column of data that is inserted from Excel into Access and it checks whether the "PASS" is really a Pass and the "FAIL" is really a Fail... The main issues with this is that whenever the Subform is changed, clicked, or when records are pasted into the database Access hangs for long periods of time... (it IS running a central server over a network). The person that wrote the code is claiming its a networking error and refuses to fix the problem... but I think its a programming error and that the "
    Private Sub Form_Current()
    DoCmd.RefreshRecord" command needs to be removed and replaced with something else.

    And also, in my opinion shouldn't the VBA function be changed to something like this?:

    Code:
    Private Sub TestReading_Change()
    
    If [PASSorFAIL] = "PASS" Then
        If [TestReading] = Null Then
        [PASSorFAIL] = "PASS"
        End If
        If [TestReading] < [Min] Then
        [PASSorFAIL] = "FAIL"
        End If
        If [TestReading] <= [Max] Then
        [PASSorFAIL] = "PASS"
        End If
        If [TestReading] >= [Min] Then
        [PASSorFAIL] = "PASS"
        End If
        If [TestReading] > [Max] Then
        [PASSorFAIL] = "FAIL"
        End If
    End If
    
    If [PASSorFAIL] = "FAIL" Then
        If [TestReading] = Null Then
        [PASSorFAIL] = "PASS"
        End If
        If [TestReading] < [Min] Then
        [PASSorFAIL] = "FAIL"
        End If
        If [TestReading] <= [Max] Then
        [PASSorFAIL] = "PASS"
        End If
        If [TestReading] >= [Min] Then
        [PASSorFAIL] = "PASS"
        End If
        If [TestReading] > [Max] Then
        [PASSorFAIL] = "FAIL"
        End If
    End If
    
    End Sub
    I'm not sure if either my co-worker of myself is correct (I'm thinking we're are both wrong) but some help and direction would be greatly appreciated any if there are any questions I will answer them and provide more code if needed. I feel like this code is causing some major errors and upsets in our lab and to finally get it sorted out would be awesome!
    Thanks in advance.




  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,900
    I've never use RefreshRecord.

    The code returns the same result whether PASSorFAIL equals Pass or Fail. And what 'each column'?
    Consider:
    Code:
    With Me
    If .PASSorFAIL = "PASS" Or .PASSorFAIL = "FAIL" Then
        Select Case .TestReading
            Case Null
                .PASSorFAIL = "PASS"
            Case Is < .Min
                .PASSorFAIL = "FAIL"
            Case Is > .Max
                .PASSorFAIL = "FAIL"
            Case Else
                .PASSorFAIL = "PASS"
        End Select
    End If
    End With
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    d2ther is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    3
    Thanks for such and quick and detailed reply.

    I was thinking the RefreshRecord was causing the hanging but wasn't certain. I was thinking something like DoCmd.Requery would fix the problem?
    There's a separate Excel file that has columns and fieldnames at the top and the data is in rows. Kind of like this:

    LotNo BatchNo TestReading Date Non-Printable Notes PASSorFAIL LotRelease Failed Lots LIST
    100000 1 2 7-Sep-10 PASS

    The user special pastes the records in as a CSV from Excel, the problem seems to be that the Columns in the spreadsheets are already predefined as PASS so when the user pastes the records into Access the statement just assumes such...?

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,900
    Don't understand how this procedure is associated with copy/paste of records. What 'statement' - the VBA code? Code only 'assumes' what you tell it to assume. I don't know if Requery is pertinent because I don't understand the process.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    d2ther is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    3
    The general problem right now is that the Excel sheets are already pre-populated with PASS in the PASSorFAIL column of the CVS tables.

    So when the user tries to Copy-paste the records from Excel into Access the VBA code should be checking the TestReading column for data integrity, making sure that the test result isn't greater than the Max column or less than the Min column. So it checks whether a "PASS" is really a PASS and a FAIL is really a FAIL, but for some reason it doesn't work because the Excell records are already a Pass...?

    The VBA code is in the event procedure of the form for the TestReading column of the records as and AfterUpdate() method... I guess... I've only dabbled in VB Script so forgive my illiteracy of the language.

    I don't think the Requery or even RefreshRecord is necessary seeing as how it's already used in another section of the database where is works just fine an I feel like its just slowing down the form all together.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,900
    After records are inserted into table en masse by copy/paste or import with wizard, options to modify the PASSorFAIL value:

    1. run an UPDATE sql action

    2. open recordset in VBA and cycle through the recordset
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    PMFJI,

    Looking at the code from your programmer, I see a couple of errors.

    Code:
    If [TestReading] = "PASS" Then
    ..
    End If
    
    If [TestReading] = "FAIL" Then
    ..
    End If
    It looks like [TestReading] (a numeric field??) is being compared to a text string.
    This test will always equal FALSE in VBA. If used in a formula, you get a type mismatch error.


    Code:
    If [TestReading] = Null Then
    This test will always be FALSE. NULL is not equal to anything, not even to NULL.



    And also, in my opinion shouldn't the VBA function be changed to something like this?:
    Touched on the NULL issue.

    Code:
    Private Sub TestReading_Change()
    The change event is not the right event to use (IMO).

    From HELP:
    Setting the value of a control by using a macro or Visual Basic doesn't trigger this event for the control.
    You must type the data directly into the control, or set the control's Text property.


    I have never used the after insert event, but it probably is a better event to use than the TestReading_Change event.


    June's code deals nicely with the NULL issue as well as being shorter...


    My $0.02

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,900
    Actually, I should have done some testing. The Null case doesn't work and it isn't needed.
    Code:
    With Me
    If .PASSorFAIL = "PASS" Or .PASSorFAIL = "FAIL" Then
        Select Case .TestReading
            Case Is < .Min
                .PASSorFAIL = "FAIL"
            Case Is > .Max
                .PASSorFAIL = "FAIL"
            Case Else
                .PASSorFAIL = "PASS"
        End Select
    End If
    End With
    Last edited by June7; 09-13-2013 at 05:58 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    OK, how about ..

    *** Notice that I changed the "Min" & "Max" field names in my examples.

    "Min" & "Max" are reserved words and shouldn't be used as object names.
    see http://allenbrowne.com/AppIssueBadWord.html#M


    Code:
       With Me
          If .PASSorFAIL = "PASS" Or .PASSorFAIL = "FAIL" Then
             Select Case .TestReading
                Case Is < .MinReading, Is > .MaxReading
                   .PASSorFAIL = "FAIL"
                Case Else
                   .PASSorFAIL = "PASS"
             End Select
          End If
       End With


    Or even:
    Code:
       With Me
          If .PASSorFAIL = "PASS" Or .PASSorFAIL = "FAIL" Then
             .PASSorFAIL = "PASS"
             Select Case .TestReading
                Case Is < .MinReading, Is > .MaxReading
                   .PASSorFAIL = "FAIL"
             End Select
          End If
       End With

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,900
    Or maybe as simple as
    Code:
    With Me
       If .PASSorFAIL & "" <> "" Then
          .PASSorFAIL = IIf(.TestReading < .MinReading Or .TestReading > .MaxReading, "FAIL", "PASS")
       End If
    End With
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    What about if "PASSorFAIL" is NULL??

    Consider:
    Code:
    With Me
       .PASSorFAIL = "PASS"    'set default
       If .PASSorFAIL & "" <> "" Then
          .PASSorFAIL = IIf(.TestReading < .MinReading Or .TestReading > .MaxReading, "FAIL", "PASS")
       End If
    End With

  12. #12
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,900
    Going by the original code, the OP had no need to evaluate TestReading if PASSorFAIL is null and possibly the field won't have any nulls: in post 3 "... already predefined as PASS so ...".

    So if the field already has "Pass" in every record, maybe just the one line:

    Me.PASSorFAIL = IIf(Me.TestReading < Me.MinReading Or Me.TestReading > Me.MaxReading, "FAIL", "PASS")
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 9
    Last Post: 10-29-2012, 04:26 PM
  2. Comparing Dates in nested IIF statements
    By Purdue_Engineer in forum Programming
    Replies: 6
    Last Post: 10-01-2012, 12:36 PM
  3. Nested If Statements Using Or (Logic Error)
    By IFA Stamford in forum Access
    Replies: 7
    Last Post: 12-30-2010, 08:53 AM
  4. Nested Select Statements in FROM clause
    By neonslip in forum Queries
    Replies: 11
    Last Post: 11-03-2010, 10:58 AM
  5. Nested IIF statements?
    By laavista in forum Access
    Replies: 9
    Last Post: 06-22-2010, 10:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums