Results 1 to 10 of 10
  1. #1
    dwwally is offline Novice
    Windows 10 Access 2007
    Join Date
    Jun 2016
    Posts
    16

    If...End If within a With... End With loop bombs if null fields are involved. See code below


    Code:
    Sub New_Maturity_Rate_FWS_Test()
    
       On Error GoTo SubError
       Dim dbsRate As DAO.Database
       Dim tdfRate As DAO.TableDef
       Dim rstRate As Recordset
       Dim strDate As String
       Dim FieldCount As Integer
       Dim i As Integer
       Dim HighRate As Single
       Dim Rate36Mo As Single
       Dim Sum As Single
       Dim Count As Integer
       Dim Avg As Single
    
       Dim RateTable As String
       Dim AssessDate As Date
       Dim FinalRate As Single
    
       Rem this is a large table with interest rates for 80+ investment types from 1972 to the present
       Rem I am pulling only one date and only for three year investments for the code belowfor one date 71/1979
       RateTable = "Investment All Rates Combined"
    
    
       AssessDate = #7/1/1979#
    
       Set dbsRate = CurrentDb
       Set tdfRate = dbsRate.TableDefs(RateTable)
       Set rstRate = dbsRate.OpenRecordset("SELECT * FROM [" & RateTable & "] ORDER BY [Date]", dbOpenSnapshot)
    
       With rstRate
          FieldCount = .Fields.Count - 1
          For i = 0 To FieldCount
    Debug.Print "REM ", i, .Fields(i).Name, .Fields(i).Value
          Next i
       End With
    
       Rem data only for 3-year investments produced by this loop, all rates are numbers
       Rem 0 Date 1/1/1979
       Rem 32 Treas Bond 3 Year_Rate 8.94
       Rem 35 3 Yr Fed Home Loan Yield Null
       Rem 37 3 Yr Fed Land Bank Yield Null
       Rem 39 3 Yr Fed Farm Credit Yield Null
       Rem 41 3 Yr Stud Loan Mrktg Yield Null
       Rem 43 3 Yr TVA Yield Null
    
       strDate = "Date = " & "#" & AssessDate & "#"   '"
       With rstRate
          .FindFirst strDate
          If .NoMatch Then
             MsgBox "Date " & strDate & " not found in rate table. See author."
             Exit Sub
          End If
       End With
    
       With rstRate
          HighRate = 0
          Rem this filed has a value of 14.72 and the If...End If works
          If Not IsNull(.Fields(32)) Then
             FinalRate = .Fields(32).Value
             Count = Count + 1
             Sum = Sum + FinalRate
             HighRate = FinalRate
          End If
          Rem all the If....End If below I had to block out because the fields 35, 37, 39, 41 and 43 have null values
          Rem unblocking these IF...End If creates an error
          Rem End With without With
          Rem the program debug then results in:
          Rem Rate36Month 7/1/1979 8.94 1 8.94 8.94
    
          ' If Not IsNull(.Fields(37)) Then
          ' FinalRate = .Fields(37)
          ' Sum = Sum + FinalRate
          ' HighRate = FinalRate
          ' If FinalRate > HighRate Then HighRate = FinalRate
          ' End
          ' If Not IsNull(.Fields(39)) Then
          ' FinalRate = .Fields(39)
          ' Sum = Sum + FinalRate
          ' HighRate = FinalRate
          ' If FinalRate > HighRate Then HighRate = FinalRate
          ' End If
          ' If Not IsNull(.Fields(41)) Then
          ' FinalRate = .Fields(41)
          ' Sum = Sum + FinalRate
          ' HighRate = FinalRate
          ' If FinalRate > HighRate Then HighRate = FinalRate
          ' End If
          ' If Not IsNull(.Fields(43)) Then
          ' FinalRate = .Fields(43)
          ' Sum = Sum + FinalRate
          ' HighRate = FinalRate
          ' If FinalRate > HighRate Then HighRate = FinalRate
          ' End If
    
    
          Rate36Mo = HighRate
          Avg = Sum / Count
    Debug.Print "Rate36Month ", AssessDate, Rate36Mo, Count, Sum, Avg
          FinalRate = Rate36Mo
       End With
    
    SubError_Exit:
    
       Exit Sub
    
    SubError:
       MsgBox Err.Description
       MsgBox Err.Number
       Resume SubError_Exit
    
    
    End Sub
    Last edited by RuralGuy; 06-28-2016 at 08:20 AM. Reason: Formatted the code

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    your code is unreadable, please repost using indentation and code tags

  3. #3
    dwwally is offline Novice
    Windows 10 Access 2007
    Join Date
    Jun 2016
    Posts
    16
    HOW DO i repost using indentation and code tags?

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    copy and paste your code, then highlight and click the code button (the one with # on the far right). If you use the advanced editor (click Go Advanced below), you can see what your post will look like

  5. #5
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    What is the error message you get?

    If you comment out the On Error Goto Suberror line, you can find out which line is causing the error using the debug mode.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The error is because your If/End If are out of whack. Hint, End is not the same as End If.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    dwwally is offline Novice
    Windows 10 Access 2007
    Join Date
    Jun 2016
    Posts
    16
    All of you, thanks for your help. I found the problem in my code. One of my
    IF...End If I used by accident
    If...End only. That caused the error.

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    if your isnull is not preventing the 'if' code from running, then the value is not null - perhaps it is a zero length text string?

  9. #9
    dwwally is offline Novice
    Windows 10 Access 2007
    Join Date
    Jun 2016
    Posts
    16
    Dear pbaldy, I had found that error on my own. The reason I missed it because I had more than one hundred If...End Ifs
    I missed it in 2 spots in my code, but the error debug did not stop on the "End" with the missing If
    That would have made my life a lot easier. I am working with more than 60,000 lines of code in my program. Thanks

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem, glad you found it.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 3
    Last Post: 08-10-2015, 04:44 AM
  2. Replies: 12
    Last Post: 06-05-2015, 04:27 PM
  3. Replies: 13
    Last Post: 08-20-2014, 09:17 AM
  4. Loop Subform to find null fields
    By rdkelly3 in forum Forms
    Replies: 2
    Last Post: 01-12-2014, 08:39 AM
  5. Combine two fields, Null fields involved
    By yes sir in forum Access
    Replies: 9
    Last Post: 10-03-2010, 09:20 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