Results 1 to 10 of 10

Thread: [RESOLVED] Code does not execute after For Each Next Loop

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    12

    Resolved [RESOLVED] Code does not execute after For Each Next Loop


    I am calling a sub from a form load event. The procedure that is being called adds columns to a listview and then loops through an array to populate the listview. I have another procedure called after the loop that does not execute when the loop completes. Please advise as to what I am doing wrong for the code not to execute after the loop in the if/then/else statement or why it is not executing if I remove the if/then/else statement and just placed the called sub after the loop or why the code does not go back to the form load event and then call the next called sub. Code below:

    Code:
    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim Startup As frmRPMSStartup
            Dim login As frmRPMSLogin
            Dim EmployeeName As String
            Try
                'some code to fill labels here
                Firstcalledsub()
                Secondcalledsub()
            Catch ex As Exception
                Throw ex
                MessageBox.Show("Error on log-in " & ex.ToString, "Log-in error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            End Try
        End Sub
    HTML Code:
     Sub Firstcalledsub()
            'Create columns in listview
            With lstvListview            .View = View.Details
                .LabelEdit = True
                .FullRowSelect = True
                .GridLines = True
                .Columns.Add("string", 65, HorizontalAlignment.Left)
                .Columns.Add("string", 400, HorizontalAlignment.Left)
                .Columns.Add("string", 400, HorizontalAlignment.Left)
                .Columns.Add("string", 400, HorizontalAlignment.Left)
                .Columns.Add("string", 65, HorizontalAlignment.Left)
                .Columns.Add("string", 75, HorizontalAlignment.Left)
                .Columns.Add("string", 100, HorizontalAlignment.Left)
                .Columns.Add("string", 100, HorizontalAlignment.Left)
                .Columns.Add("string", 75, HorizontalAlignment.Left)
                .Columns.Add("string", 120, HorizontalAlignment.Left)
                .Columns.Add("string", 175, HorizontalAlignment.Left)
                .Columns.Add("string", 60, HorizontalAlignment.Left)
                .Columns.Add("string", 75, HorizontalAlignment.Left)
                .Columns.Add("string", 100, HorizontalAlignment.Left)
                .Columns.Add("string", 100, HorizontalAlignment.Left)
                .Columns.Add("string", 50, HorizontalAlignment.Left)
                .Columns.Add("string", 30, HorizontalAlignment.Left)
            End With
            
                'Create item to hold contents of handover array
            Dim itm As ListViewItem
            Dim arrayitems As String
            Dim i As Integer = 0
            If IsNothing(array) Then
                ToDoListItems()
            Else
                For Each arrayitems In Datarray
                    'Add data from Database logins array to listview item
                    itm = New ListViewItem(New String() {array(i, 0), array(i, 1), array(i, 2), array(i, 3), array(i, 4), array(i, 5),  array(i, 6), array(i, 7), array(i, 8), array(i, 9), array(i, 10), array(i, 11), array(i, 12), array(i, 13), array(i, 14), array(i, 15), array(i, 16)})
                    'Add item to Handover listview
                    lstvHandover.Items.Add(itm)
                    i += 1
                Next
    Secondcalledsub()
            End If
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Code does not execute after For Each Next Loop

    An exception is being thrown but you aren't seeing it because you are just rethrowing it. You've got code to display it but that's after the Throw, so it's never executed. Why are you rethrowing the exception anyway?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    12

    Re: Code does not execute after For Each Next Loop

    I put the messagebox in there to see if it would interpret the exception differently but that didn't work. Even when I take that out no exception is being thrown. The first procedure call actually works and my first listview box is populated with the data from the array. Once the loop executes my the form UI is displayed and the data is displayed in my first list box. It just not goes to the next sub to populate my other list boxes. Code below:

    Code:
     Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                 Try
                 AddHandoverItems()
    
            Catch ex As Exception
                Throw ex
            End Try
        End Sub
    
    Sub AddHandoverItems()
            'Create columns in listview
            With lstvHandover
                .View = View.Details
                .LabelEdit = True
                .FullRowSelect = True
                .GridLines = True
                .Columns.Add("Shift ID", 65, HorizontalAlignment.Left)
                .Columns.Add("Current Status", 400, HorizontalAlignment.Left)
                .Columns.Add("Needed", 400, HorizontalAlignment.Left)
                .Columns.Add("To Do", 400, HorizontalAlignment.Left)
                .Columns.Add("Tracker Ticket", 65, HorizontalAlignment.Left)
                .Columns.Add("NOV POC", 75, HorizontalAlignment.Left)
                .Columns.Add("NOV POC Phone", 100, HorizontalAlignment.Left)
                .Columns.Add("NOV POC Email", 100, HorizontalAlignment.Left)
                .Columns.Add("Rig Contact", 75, HorizontalAlignment.Left)
                .Columns.Add("Rig Phone", 120, HorizontalAlignment.Left)
                .Columns.Add("Rig Email", 175, HorizontalAlignment.Left)
                .Columns.Add("Rig ID", 60, HorizontalAlignment.Left)
                .Columns.Add("Third Party", 75, HorizontalAlignment.Left)
                .Columns.Add("Third Party Phone", 100, HorizontalAlignment.Left)
                .Columns.Add("Third Party Email", 100, HorizontalAlignment.Left)
                .Columns.Add("Date", 50, HorizontalAlignment.Left)
                .Columns.Add("Handover Record", 30, HorizontalAlignment.Left)
            End With
            
                'Create item to hold contents of handover array
            Dim itm As ListViewItem
            Dim handoveritems As String
            Dim i As Integer = 0
            If IsNothing(Database_Logins.Turnover) Then
                ToDoListItems()
            Else
                    For Each handoveritems In Database_Logins.Turnover
                        'Add data from Database logins array to listview item
                        itm = New ListViewItem(New String() {Database_Logins.Turnover(i, 0), Database_Logins.Turnover(i, 1), Database_Logins.Turnover(i, 2), _
                                                           Database_Logins.Turnover(i, 3), Database_Logins.Turnover(i, 4), Database_Logins.Turnover(i, 5), _
                                                          Database_Logins.Turnover(i, 6), Database_Logins.Turnover(i, 7), Database_Logins.Turnover(i, 8), _
                                                           Database_Logins.Turnover(i, 9), Database_Logins.Turnover(i, 10), Database_Logins.Turnover(i, 11), _
                                                         Database_Logins.Turnover(i, 12), Database_Logins.Turnover(i, 13), Database_Logins.Turnover(i, 14), _
                                                            Database_Logins.Turnover(i, 15), Database_Logins.Turnover(i, 16)})
                        'Add item to Handover listview
                        lstvHandover.Items.Add(itm)
                        i += 1
                Next handoveritems
                ToDoListItems()
            End If
        End Sub
    
        Public Sub ToDoListItems()
            'Add user to employee label
            Dim strEmployee As String
            strEmployee = Database_Logins.Name
            lblDailyToDo.Text = strEmployee & " Daily To Do's"
    
            'Create columns in listview
            With lstvToDo
                .View = View.Details
                .LabelEdit = True
                .FullRowSelect = True
                .GridLines = True
                .Columns.Add("User ID", 65, HorizontalAlignment.Left)
                .Columns.Add("Memo", 200, HorizontalAlignment.Left)
                .Columns.Add("Date", 75, HorizontalAlignment.Left)
            End With
            'Create item to hold contents of handover array
            Dim itm As ListViewItem
            Dim i As Integer = 0
            Dim todoitems As String
            If IsNothing(Database_Logins.EmployeToDo) Then
                AnnouncementListsItems()
            Else
                For Each todoitems In Database_Logins.EmployeToDo
                    itm = New ListViewItem(New String() {Database_Logins.EmployeToDo(i, 0), Database_Logins.EmployeToDo(i, 1), Database_Logins.EmployeToDo(i, 2), _
                                                         Database_Logins.EmployeToDo(i, 3), Database_Logins.EmployeToDo(i, 4)})
                    'Add item to ToDo Listview
                    lstvToDo.Items.Add(itm)
                    i += 1
                Next
                AnnouncementListsItems()
            End If
        End Sub
    It won't proceed to the other procedure calls. It just finishes the first one then loads the UI with no exception.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Code does not execute after For Each Next Loop

    You've done the complete opposite of what I indicated. Keep the MessageBox and get rid of the Throw.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    12

    Unhappy Re: Code does not execute after For Each Next Loop

    So the real question is, how do I get every item from my listview into my listview control. When I use:
    HTML Code:
      For i = 0 To UBound(Database_Logins.Turnover)
                    'Add data from Database logins array to listview item
                    itm = New ListViewItem(New String() {Database_Logins.Turnover(i, 0), Database_Logins.Turnover(i, 1), Database_Logins.Turnover(i, 2), _
                                                       Database_Logins.Turnover(i, 3), Database_Logins.Turnover(i, 4), Database_Logins.Turnover(i, 5), _
                                                      Database_Logins.Turnover(i, 6), Database_Logins.Turnover(i, 7), Database_Logins.Turnover(i, 8), _
                                                       Database_Logins.Turnover(i, 9), Database_Logins.Turnover(i, 10), Database_Logins.Turnover(i, 11), _
                                                     Database_Logins.Turnover(i, 12), Database_Logins.Turnover(i, 13), Database_Logins.Turnover(i, 14), _
                                                         Database_Logins.Turnover(i, 15), Database_Logins.Turnover(i, 16)})
                    i += 1
                    'Add item to Handover listview
                    lstvHandover.Items.Add(itm)
                Next
    I do not get the error anymore but all of my data elements are not added to my listview control.

    When I use the For each method with a string or For 1=0 to Database_Logins.Turnover.Length it adds all the data elements but gives me the error, index is outside the bounds of the array. Please advise.

  6. #6
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Code does not execute after For Each Next Loop

    What does "i += 1" mean/do?

    Are you trying to increment i inside your loop for some reason?

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Code does not execute after For Each Next Loop

    Hint about arrays

    Code:
            Dim bar(9, 5) As Integer
    
            For x As Integer = 0 To UBound(bar, 1)
                bar(x, 5) = x + 10
                Debug.WriteLine(bar(x, 5).ToString)
            Next
    It is not recommended to manipulate the for variable within the loop.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    12

    Re: Code does not execute after For Each Next Loop

    Yes 'i += 1' is when I am increment the For variable. How else do i increment it in VB? I am dynamically dimensioning my array in another class. I have a database that will get update often so the array will change vary in size. Once I fill the array with all the data I then want it copied into a listview control as above.
    As stated before I've tried all I know to try from the For each method with a string, For 1=0 to Database_Logins.Turnover.Length, the same for loop with (databaselogins.turnover.length) - 1, UBound(database_logins.turnover) -1. The only one that works halfway is using the Ubound but it doesn't copy all of my data elements from the array. It runs through the loop once and only adds the first row of data into the array. Now I have two rows with the possibility of 30 rows. Please advise.

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    12

    Re: Code does not execute after For Each Next Loop

    Gentlemen thanks for schooling me. It was my increment statement that was causing the problem. Final code:
    vb Code:
    1. For i = 0 To UBound(Database_Logins.Turnover)
    2.                 'Add data from Database logins array to listview item
    3.                 itm = New ListViewItem(New String() {Database_Logins.Turnover(i, 0), Database_Logins.Turnover(i, 1), Database_Logins.Turnover(i, 2), _
    4.                                                    Database_Logins.Turnover(i, 3), Database_Logins.Turnover(i, 4), Database_Logins.Turnover(i, 5), _
    5.                                                   Database_Logins.Turnover(i, 6), Database_Logins.Turnover(i, 7), Database_Logins.Turnover(i, 8), _
    6.                                                    Database_Logins.Turnover(i, 9), Database_Logins.Turnover(i, 10), Database_Logins.Turnover(i, 11), _
    7.                                                  Database_Logins.Turnover(i, 12), Database_Logins.Turnover(i, 13), Database_Logins.Turnover(i, 14), _
    8.                                                      Database_Logins.Turnover(i, 15), Database_Logins.Turnover(i, 16)})
    9.                 'Add item to Handover listview
    10.                 lstvHandover.Items.Add(itm)
    11.             Next
    12.  
    13.             ToDoListItems()
    You guys are awesome. I really appreciate the assistance. So when using arrays I should not increment my variable?

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Code does not execute after For Each Next Loop

    Quote Originally Posted by iderf View Post
    ...You guys are awesome. I really appreciate the assistance. So when using arrays I should not increment my variable?
    The For...Next loop does it for you. http://msdn.microsoft.com/en-us/library/5z06z1kb.aspx
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width