Results 1 to 7 of 7

Thread: Need help w/ loop w/in loop???

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    67

    Arrow Need help w/ loop w/in loop???

    I had an earlier thread but it turns out the problem is more complicated than initially expected. This is a new updated thread.

    I'm creating a vb program that uses rows from a dataset to create a text output.
    However, I think I need a loop w/in a loop b/c some users have multiple rows but I want it contained w/in one record of the output.

    Hope that makes sense....here's what I have so far:

    Code:
    For each row in ds.tables("1234").rows
    
    A1 = String.Format("{0,5}..etc", employeeID, address etc.)
    sw.writeline (A1)
    
    A2 = String.Format("{0,5}..etc", Option, address etc.)
    sw.writeline (A2)
    
    A3 = String.Format("{0,5}..etc", dependent, address etc.)
    sw.writeline (A3)
    
    Next
    Now, say that employees have more than one "Option" and can choose one or the other OR both. The way it's set up now, it will output the employee's info a second time if the employee has both options selected. Like so

    Employee A
    Option 1
    dependent

    Employee A
    Option 2
    dependent

    Employee B
    Option 1
    dependent


    I'm trying to get the output to look like the following (assuming first employee selects both options and the next employee only selected one):

    Employee A
    Option 1
    Option 2
    dependent

    Employee B
    Option 1
    dependent



    I was thinking I could maybe use an "if" statement but can't come up with the proper logic. I'd like to say, "If Employee Info has already been output, jump to A2"

    Any help is appreciated. Let me know if my explaination is unclear and I'll do my best to clarify.

    Thanks.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Need help w/ loop w/in loop???

    outside the loop declare a variable to hold the employee id... set it to some default value... say, -1 for instance.
    Inside the loop.... If the employee id variable does NOT match the employeeID of the record, output the employee part of the record and store the employeeID from the record in the variable.
    proceed to output your options....

    the dependant part of the record though..... hmmmmm....


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    67

    Re: Need help w/ loop w/in loop???

    Yeah, that's my big hangup. My initial thread only had "A1" and "A2" which would work w/ your solution. However, adding the "A3" portion is making it more complicated.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Need help w/ loop w/in loop???

    I got it.... see if this works for you:

    Code:
    dim EmplID as Integer = -1
    dim A3 as String = ""
    For each row in ds.tables("1234").rows
    
      'Assumption: employeeID is a field from the row somewhere....
      If EmplID <> employeeID then
    
          If A3.Length > 0 Then
            sw.writeline(A3) 'Write the dependant from the previous row...
          End If
    
          'write out the employee
          sw.writeline (String.Format("{0,5}..etc", employeeID, address etc.))
    
          'build the dependant string
          A3 = String.Format("{0,5}..etc", dependent, address etc.)
      End If
      'Write the option
      sw.writeline (String.Format("{0,5}..etc", Option, address etc.))
    
    Next
    sw.writeline(A3) 'Write the dependant from the last row
    it's a little futzy... and could probably be cleaned up some....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Need help w/ loop w/in loop???

    What about something like this?

    vb.net Code:
    1. Public Sub Method()
    2.     Dim table As New DataTable("Employees")
    3.     With table
    4.  
    5.         With .Columns
    6.             .Add("EmployeeID", GetType(Integer))
    7.             .Add("EmployeeName", GetType(String))
    8.             .Add("OptionName", GetType(String))
    9.         End With
    10.  
    11.         With .Rows
    12.             .Add(1, "David", "Option 1")
    13.             .Add(1, "David", "Option 2")
    14.             .Add(2, "Jack", "Option 1")
    15.             .Add(3, "Amy", "Option 1")
    16.             .Add(4, "Mark", "Option 1")
    17.             .Add(4, "Mark", "Option 2")
    18.         End With
    19.  
    20.     End With
    21.  
    22.     Dim results = From row In table.AsEnumerable() _
    23.                   Group By employeeId = CInt(row.Item("EmployeeID")) Into groups = AsEnumerable() _
    24.                   Select New With {.EmployeeID = employeeId, _
    25.                                    .EmployeeName = groups.Where(Function(row) CInt(row("EmployeeID")) = employeeId) _
    26.                                                          .Select(Function(row) row("EmployeeName").ToString()).FirstOrDefault(), _
    27.                                    .Options = groups.Where(Function(row) CInt(row("EmployeeID")) = employeeId) _
    28.                                                     .Select(Function(row) row("OptionName").ToString()).ToArray()}
    29.     For Each element In results
    30.         MessageBox.Show(String.Format("{0}{1}{2}", element.EmployeeName, _
    31.                                       Environment.NewLine, String.Join(Environment.NewLine, element.Options)))
    32.     Next
    33.  
    34. End Sub

    I assumed a table structure...

  6. #6
    Addicted Member
    Join Date
    Jun 2009
    Posts
    245

    Re: Need help w/ loop w/in loop???

    One solution might be to first declare a counter outside the loop to keep track on wich row the loop is at. So for each iteration in the loop, increase the value by one.

    Now, make a new loop inside your existing loop like this (if your integer is declared as 'counter':
    vb.net Code:
    1. For x As Integer = counter To ds.tables("1234").rows.Count
    2.  
    3.             Next
    In this loop you will have a If-statement, wich will replace your A2 = part.If the row is equal to A1, print out the A2 values. else, exit the loop. So the loop will look something like this:

    vb.net Code:
    1. For x As Integer = counter To ds.tables("1234").rows.Count
    2.             If String.Format("{0,5}..etc", employeeID, address etc.) = A1 Then
    3.                  A2 = String.Format("{0,5}..etc", Option, address etc.)
    4.                  sw.writeline (A2)
    5.             Else
    6.                  Exit For
    7.             End If
    8.         Next

    A bit messy, but I guess you'll get my idea

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    67

    Re: Need help w/ loop w/in loop???

    I think I found a solution based on some information I gathered from the other posts.

    I decided to use a seperate data set of just the employee ID and Option. So now, when it gets to the option line, I start a new loop that checks to see if the EmplID matches the emply id from the first line, if it does, it will contiue to write the option line until it reaches an EmplID that does not match the orignial one taken from the first dataset.

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