locked
Help with 'do until' ......... 'Loop' RRS feed

  • Question

  • User-1577511590 posted

    Hello Everyone,

    I am writing a program to search for customers on a database.  I am having a bit of a problem with my do until ..... loop routines.

    I have posted the code below for you to look at and correct me.

    I am using VB on VS 2005 and Access Database.

    The end user will enter the customers name (Whole or part of ) in to a textbox and click the button 'Find', once the checks have been done to make sure there is something in the textbox it will then call this routine Search_Customer(ByVal Customer_Name).  The ByVal Customer_name will have the text the end user submitted from the textbox.

    Everything goes great until we get to the last record that has been filtered.  In the old days of Visual Basic 6 (I think) you could use EOF (End of File) I cant seem to find that on VS2005.  When we get to the last record we just miss that out and exit the loop....... WHY, please can some one help me as I am now starting to pull my hair out.

     

     

        Public Sub Search_Customer(ByVal Customer_Name)
            Dim Records_Found As Integer = 0
            Dim Temp_Count As Integer = 0
            Dim Temp_Customer_Name As String = ""
            Dim Temp_Job_Number As String = ""
            Dim Group As Integer = -1
            Dim i As Integer = 0
    
            ' Filter all Bindingsources
            With SapphireMain
                .PrintLogBindingSource.Filter = "CustomerName Like '" & Customer_Name & "*'"
                Records_Found = .PrintLogBindingSource.Count
                .Records0306BindingSource.Filter = "CustomerName Like '" & Customer_Name & "*'"
                Records_Found = Records_Found + .Records0306BindingSource.Count
                .Records0003BindingSource.Filter = "CustomerName Like '" & Customer_Name & "*'"
                Records_Found = Records_Found + .Records0003BindingSource.Count
                .Records9700BindingSource.Filter = "CustomerName Like '" & Customer_Name & "*'"
                Records_Found = Records_Found + .Records9700BindingSource.Count
                .Records9397BindingSource.Filter = "CustomerName Like '" & Customer_Name & "*'"
                Records_Found = Records_Found + .Records9397BindingSource.Count
                ' Display on Status Bar how many records found
                .ToolStripStatusLabel1.Text = "Records Found " & Records_Found
    
                ' Now Display Found Records on Listview1
                If .PrintLogBindingSource.Count >= 1 Then
                    Temp_Count = .PrintLogBindingSource.Count                                   ' Copy the number of records found to Temp_Count
                    .PrintLogBindingSource.Sort = ("CustomerName ASC")                          ' Sort the BindingSource by Customer Name in Ascending Order
                    .PrintLogBindingSource.MoveFirst()                                          ' Move to the first record
                    Do Until .PrintLogBindingSource.Position = .PrintLogBindingSource.Count -1    ' Repeat the process below until we reach the end of all found records
                        Group = Group + 1                                                       ' For each new Customer we need a new group, so increase the group number by 1
    
                        ' Add A new group to Listview1
                        ' Trim the name and put into UPPERCASE
                        ListView1.Groups.Add(New ListViewGroup(Trim(UCase(.PrintLogBindingSource.Current("CustomerName").ToString)), HorizontalAlignment.Left))
                        Temp_Customer_Name = Trim(UCase(.PrintLogBindingSource.Current("CustomerName").ToString)) ' Copy Current Customer Name to Temp_Customer_name
    
                        ' Because there are going to be a number of Records with the same Customer Name we do not want to display every instance so 
                        ' this routine will keep moving to the next record until the Customer Name is Different to Temp_Customer_name
                        Do Until Temp_Customer_Name <> Trim(UCase(.PrintLogBindingSource.Current("CustomerName").ToString))
                            ListView1.Items.Insert(i, "PrintLog", .PrintLogBindingSource.Current("Job"), 1).Group = ListView1.Groups(Group) ' insert a new item to the group, user will see the Job Number
                            Temp_Job_Number = .PrintLogBindingSource.Current("Job")
    
                            ' Because there are going to be a number of Records with the same Job Number we do not want to display every instance so 
                            ' this routine will keep moving to the next record until the Job Number is Different to Temp_Job_number
                            Do Until Temp_Job_Number <> .PrintLogBindingSource.Current("Job")
                                Debug.Print(Trim(UCase(.PrintLogBindingSource.Current("CustomerName").ToString)) & "  " & .PrintLogBindingSource.Position)
                                .PrintLogBindingSource.MoveNext()
                            Loop
    
                        Loop
                        ' When we have reached a 'New' Customer name then go back and create a new group and repeat routine
                    Loop
                End If
    
    
            End With
    
    
        End Sub

     Thank you in advance

     Chris

    Friday, November 14, 2008 11:36 AM

Answers

  • User708851339 posted

    Ah, sorry for the run around.  Now I see what the problem is.  Well this is the last suggestion I have but it should get the job done.  Just declare PreviousPosition and change the first Do Until Loop to this.

    .PrintLogBindingSource.MoveFirst() ' Move to the first record
    Dim PreviousPosition As Integer
       Do

         PreviousPosition = .PrintLogBindingSource.Position
         .
         .
         .
       Loop Until PreviousPosition <> .PrintLogBindingSource.MoveNext()

     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 14, 2008 4:24 PM

All replies

  • User708851339 posted

    ChrisYard, I can't even see how you can even get past these two statements.

    Temp_Customer_Name = Trim(UCase(.PrintLogBindingSource.Current("CustomerName").ToString)) ' Copy Current Customer Name to Temp_Customer_name
    Do Until Temp_Customer_Name <> Trim(UCase(.PrintLogBindingSource.Current("CustomerName").ToString))
    

    You need to restructure your do... until loops so that they look like:

    Do
        [ statements ]
    Loop Until condition

    Got it?

    Friday, November 14, 2008 1:48 PM
  • User-1577511590 posted

    The below code was taken from first help example in VS2005 Documentation when searching for 'Do'

    Do Until DefResp = vbNo
       MyNum = Int (6 * Rnd + 1)   ' Generate a random integer between 1 and 6.
       DefResp = MsgBox (MyNum & " Do you want another number?", vbYesNo)
    Loop
    
    Dim Check, Counter
    Check = True: Counter = 0   ' Initialize variables.
    Do                            ' Outer loop.
       Do While Counter < 20      ' Inner loop.
          Counter = Counter + 1   ' Increment Counter.
          If Counter = 10 Then    ' If condition is True...
             Check = False        ' set value of flag to False.
             Exit Do              ' Exit inner loop.
          End If
       Loop
    Loop Until Check = False      ' Exit outer loop immediately.
     
    
     Any way I have changed code as suggested and it still does not work.
     
    Do 
    ...
    ...
    ...
    Loop Until Temp_Customer_Name <> Trim(UCase(.PrintLogBindingSource.Current("CustomerName").ToString))
     
    I have a feeling it is something to do with when reading the record position, when at first record the position is 0, when at last record the position is 743
    even though the record count was 744 which is why I put in loop until .PrintLogBindingSource.Position = .PrintLogBindingSource.Count -1 
     
     
     
    Friday, November 14, 2008 2:35 PM
  • User-1577511590 posted

     I think I might of put this in the wrong place

    Friday, November 14, 2008 2:58 PM
  • User708851339 posted
    Well, you can do the obvious thing and remove the -1 from this code. 
    Do Until .PrintLogBindingSource.Position = .PrintLogBindingSource.Count -1
     
    Friday, November 14, 2008 3:18 PM
  • User-1577511590 posted

     Again I have tried that and it gets stuck in the loop and that is with both the do until...loop and the Do ..... Loop until ........

    In the older version of Visual Basic I could use End Of File and that works perfectly.  In fact just to make sure I have tried it as I still have it on my PC and the above code works right when I replace 

    Do Until .PrintLogBindingSource.Position = .PrintLogBindingSource.Count -1
    with
    Do Until .PrintLogBindingSource.EOF
     
    Friday, November 14, 2008 3:24 PM
  • User708851339 posted

    Ah, sorry for the run around.  Now I see what the problem is.  Well this is the last suggestion I have but it should get the job done.  Just declare PreviousPosition and change the first Do Until Loop to this.

    .PrintLogBindingSource.MoveFirst() ' Move to the first record
    Dim PreviousPosition As Integer
       Do

         PreviousPosition = .PrintLogBindingSource.Position
         .
         .
         .
       Loop Until PreviousPosition <> .PrintLogBindingSource.MoveNext()

     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 14, 2008 4:24 PM