What is wrong with my counter code. If there are 8 records to read, it counts 7 and just drops the first one. And it always drops the record with the lowest ID number assigned to it. It doesn’t make any sense to me. Thanks.

VB Code:
  1. fillstring.Value = "9999999999"
  2.         fillstring2.Value = "9999"
  3.         fullfill.Value = fillstring.Value & fillstring.Value & fillstring.Value & fillstring.Value & fillstring.Value & fillstring.Value & fillstring.Value & fillstring.Value & fillstring.Value & fillstring2.Value
  4.         fullfill3.Value = fullfill.Value & fillstring.Value & fillstring.Value & fillstring.Value & fillstring2.Value
  5.         hashcount = 0
  6.  
  7.         achtotal = 0
  8.         counter = 1
  9.         zst.Value = "000000000000000"
  10.         'Date enter box, this way any date could be accessed
  11.         achdate = CDate(InputBox("Enter Effective Date to Generate ACH file for, MM/DD/YYYY"))
  12.         'Call the Read method of the DataReader to loop thru the records while the connection is open
  13.         Dim oFile As System.IO.File
  14.         Dim oWrite As System.IO.StreamWriter
  15.         'First Loop
  16.         While drAchGen.Read
  17.             oWrite = oFile.CreateText("C:\" & achdate.ToString("yyyyMMdd") & ".ach")
  18.  
  19.    
  20.             Do While drAchGen.Read
  21.  
  22.                 'ENTRY DETAIL HEADER - if its greened out, its probably part of the copy to another database system
  23.                 '1
  24.                 ed1.Value = "6"
  25.                 '2 This is like the strName, strBankers, strAmounts anything with achm is a field deal     USed to have .Value at the very end
  26.                 ed2.Value = Trim(drAchGen("ACHCode"))
  27.                 '3 & 4  USed to have .Value at the very end
  28.                 ed34.Value = drAchGen("RoutingNumber")
  29.                 ed4 = ""
  30.                 '5  USed to have .Value at the very end
  31.                 ed5 = drAchGen("AccountNumber")
  32.                 'Third Loop
  33.                 Do While Len(ed5) > 0
  34.                     If IsNumeric(Microsoft.VisualBasic.Strings.Left(Trim(ed5), 1)) Then
  35.                         ed56 = Str(CDbl(Microsoft.VisualBasic.Strings.Left(Trim(ed5), 1)))
  36.                         ed4 = Trim(ed4) & Trim(ed56)
  37.                         ed5 = Microsoft.VisualBasic.Strings.Right(Trim(ed5), Len(Trim(ed5)) - 1)
  38.                     Else
  39.                         ed5 = Microsoft.VisualBasic.Strings.Right(Trim(ed5), Len(Trim(ed5)) - 1)
  40.                     End If
  41.                     System.Diagnostics.Debug.WriteLine(ed5)
  42.                     System.Diagnostics.Debug.WriteLine(ed56)
  43.                     System.Diagnostics.Debug.WriteLine(ed4)
  44.                     'End Third Loop
  45.                 Loop
  46.                 ed55.Value = ed4
  47.                 '6 this is the amount, there is an issue of 5 possible values for this. USed to have .Value before the *
  48.                 Dim dates = Date.Now
  49.                 If drAchGen("Starting") <= dates And drAchGen("Ending") >= dates = True Then
  50.                     ed6.Value = Str(System.Math.Round(drAchGen("Amount") * 100))
  51.                     ed6.Value = Microsoft.VisualBasic.Strings.Left(zst.Value, 10 - Len(Trim(ed6.Value))) & Trim(ed6.Value)
  52.                 ElseIf drAchGen("Starting1") <= dates And drAchGen("Ending1") >= dates = True Then
  53.                     ed6.Value = Str(System.Math.Round(drAchGen("Amount1") * 100))
  54.                     ed6.Value = Microsoft.VisualBasic.Strings.Left(zst.Value, 10 - Len(Trim(ed6.Value))) & Trim(ed6.Value)
  55.                 ElseIf drAchGen("Starting2") <= dates And drAchGen("Ending2") >= dates = True Then
  56.                     ed6.Value = Str(System.Math.Round(drAchGen("Amount2") * 100))
  57.                     ed6.Value = Microsoft.VisualBasic.Strings.Left(zst.Value, 10 - Len(Trim(ed6.Value))) & Trim(ed6.Value)
  58.                 ElseIf drAchGen("Starting3") <= dates And drAchGen("Ending3") >= dates = True Then
  59.                     ed6.Value = Str(System.Math.Round(drAchGen("Amount3") * 100))
  60.                     ed6.Value = Microsoft.VisualBasic.Strings.Left(zst.Value, 10 - Len(Trim(ed6.Value))) & Trim(ed6.Value)
  61.                 ElseIf drAchGen("Starting4") <= dates And drAchGen("Ending4") >= dates = True Then
  62.                     ed6.Value = Str(System.Math.Round(drAchGen("Amount4") * 100))
  63.                     ed6.Value = Microsoft.VisualBasic.Strings.Left(zst.Value, 10 - Len(Trim(ed6.Value))) & Trim(ed6.Value)
  64.                 End If
  65.                 '7 Used to be Str(achm.Fields("mortgagenumber").Value)
  66.                 ed7.Value = "NMC00001       "
  67.                 '8 USed to have .Value at the ends of )
  68.                 ed8.Value = drAchGen("LastName") + "," + " " + drAchGen("FirstName")
  69.                 '9
  70.                 ed9.Value = "  "
  71.                 '10
  72.                 ed10.Value = "0"
  73.                 '11 Trace number is "07500005" and a counter type to keep track of entries
  74.                 ed11.Value = "07500005" & Microsoft.VisualBasic.Strings.Left(zst.Value, 7 - Len(Trim(Str(counter)))) & Trim(Str(counter))
  75.                 entdetheadr.Value = ed1.Value & ed2.Value & ed34.Value & ed55.Value & ed6.Value & ed7.Value & ed8.Value & ed9.Value & ed10.Value & ed11.Value
  76.                 oWrite.WriteLine(entdetheadr.Value)
  77.                 hashcount = hashcount + Val(Microsoft.VisualBasic.Strings.Left(drAchGen("RoutingNumber"), 8))
  78.                 achtotal = achtotal + (drAchGen("Amount") * 100)
  79.                 counter = counter + 1
  80.                 'End Second Loop
  81.             Loop
  82.  
  83.             'NMB ENTRY DETAIL HEADER
  84.             '1
  85.             ed1.Value = "6"
  86.             '2
  87.             ed2.Value = "22"
  88.             '3 & 4
  89.             ed34.Value = "075000051"
  90.             ed4 = ""
  91.             '5
  92.             ed5 = "22332662"
  93.             ed55.Value = ed5
  94.             '6
  95.             ed6.Value = Str(System.Math.Round(achtotal, 0))
  96.             ed6.Value = Microsoft.VisualBasic.Strings.Left(zst.Value, 10 - Len(Trim(ed6.Value))) & Trim(ed6.Value)
  97.             '7
  98.             ed7.Value = "NMB SERVICES       "
  99.             '8
  100.             ed8.Value = "DRNMB SERVICES"
  101.             '9
  102.             ed9.Value = "  "
  103.             '10
  104.             ed10.Value = "0"
  105.             '11
  106.             ed11.Value = "07500005" & Microsoft.VisualBasic.Strings.Left(zst.Value, 7 - Len(Trim(Str(counter)))) & Trim(Str(counter))
  107.             entdetheadr.Value = ed1.Value & ed2.Value & ed34.Value & ed55.Value & ed6.Value & ed7.Value & ed8.Value & ed9.Value & ed10.Value & ed11.Value
  108.             oWrite.WriteLine(entdetheadr.Value)
  109.  
  110.             'COMPANY BATCH CONTROL
  111.             hashcount = hashcount + 75000051
  112.             blockfill = 10 - ((counter + 4) - (Int((counter + 4) / 10) * 10))
  113.             fillblock = blockfill
  114.             totalblocks = (counter + 4 + blockfill) / 10
  115.             '1 & 2
  116.             cc12.Value = "8200"
  117.             '3
  118.             cc3.Value = Microsoft.VisualBasic.Strings.Left(zst.Value, 6 - Len(Trim(Str(counter)))) & Trim(Str(counter))
  119.             '4
  120.             cc4.Value = Microsoft.VisualBasic.Strings.Left(zst.Value, 10 - Len(Trim(Str(hashcount)))) & Microsoft.VisualBasic.Strings.Right(Str(hashcount), Len(Trim(Str(hashcount))))
  121.             '5 I know this b/c of the lenght requirment
  122.             cc5.Value = Microsoft.VisualBasic.Strings.Left(zst.Value, 12 - Len(Trim(Str(achtotal)))) & Trim(Str(achtotal))
  123.             '6
  124.             cc6.Value = Microsoft.VisualBasic.Strings.Left(zst.Value, 12 - Len(Trim(Str(achtotal)))) & Trim(Str(achtotal))
  125.             '7, 8, 9, 10, & 11
  126.             cc7891011.Value = "1394444498                         075033350000001"
  127.             cpnyctrlheadr.Value = cc12.Value & cc3.Value & cc4.Value & cc5.Value & cc6.Value & cc7891011.Value
  128.             oWrite.WriteLine(cpnyctrlheadr.Value)
  129.  
  130.             'FILE CONTROL
  131.             '1 & 2
  132.             fc12.Value = "9000001"
  133.             '3
  134.             fc3.Value = Microsoft.VisualBasic.Strings.Left(zst.Value, 6 - Len(Trim(Str(totalblocks)))) & Trim(Str(totalblocks))
  135.             '4
  136.             fc4.Value = Microsoft.VisualBasic.Strings.Left(zst.Value, 8 - Len(Trim(Str(counter)))) & Trim(Str(counter))
  137.             '5
  138.             fc5.Value = cc4.Value
  139.             '6
  140.             fc6.Value = cc5.Value
  141.             '7
  142.             fc7.Value = cc6.Value
  143.             '8 IS blank b/c reserved for future use
  144.             fc8.Value = "                                       "
  145.             flctrlheadr.Value = fc12.Value & fc3.Value & fc4.Value & fc5.Value & fc6.Value & fc7.Value & fc8.Value
  146.             oWrite.WriteLine(flctrlheadr.Value)
  147.             While fillblock > 0
  148.                 oWrite.WriteLine(fullfill.Value)
  149.                 fillblock = fillblock - 1
  150.             End While
  151.             oWrite.Flush()
  152.             oWrite.Close()
  153.             'End First Loop
  154.         End While