Results 1 to 2 of 2

Thread: Loop The Loop: adding list item and sub-items assigned to items.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Loop The Loop: adding list item and sub-items assigned to items.

    Hello there,

    below are the codes i've written to record all the items (which are qnsID) in list box (List1) into qnsdata_table.

    VB Code:
    1. Private Sub Command6_Click()
    2. Dim x As Integer
    3. Dim y As Integer
    4.  
    5. x = List1.ListCount
    6. y = 0
    7.  
    8. rst.Open "SELECT * from qnsdata_table", cn, adOpenDynamic, adLockOptimistic
    9.  
    10. Do While Not y = x
    11.     rst1.Open "SELECT qnsdataID from autonumber", cn, adOpenDynamic, adLockOptimistic
    12.     rst.AddNew
    13.     rst!qnsDataID = "qd" & Right("000000" & CStr(CInt(rst1!qnsDataID) + 1), 6)
    14.     rst!qnsID = Me.List1.ItemData(y)
    15.     rst.Update
    16.     rst1.Close
    17.     rst1.Open "UPDATE autonumber SET qnsdataID = qnsdataID +1", cn, adOpenDynamic, adLockOptimistic
    18.     y = y + 1
    19. Loop
    20.  
    21. rst.Close
    22. End Sub

    The problem is that for each item (qnsID) there are several assigned (ansID) and i need them to be recorded along with the qnsID, into qnsdata_table.
    ______________________________
    | qnsdataID | qnsID | ansID |
    -----------------------------------
    | qd0001 | qns0002 | ans0012 |
    | qd0002 | qns0002 | ans0016 |
    | qd0003 | qns0002 | ans0001 |
    ------------------------------------

    In this case, should i have another Do While Loop within the current Loop, to find out how many 'ansID' there are which are assigned to each 'qnsID'.

    I've tried

    VB Code:
    1. rst2.Open "SELECT Count(ansID) AS ansID_count FROM ans_table where....(conditions)
    2.  
    3. Me.Text2 = rst2!ansID_count         '(ive no idea where to store the count) value)
    4.  
    5. a = Me.Text2                              '(shift value into variable a)
    6. b = 0

    and here goes another loop, but it didn't work.

    Need some advice and comments from you guys =)

    Thanks a million.

    Astro

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: Loop The Loop: adding list item and sub-items assigned to items.

    VB Code:
    1. Private Sub Command6_Click()
    2.  
    3. Dim i As Integer
    4. Dim x As Integer
    5. Dim a As Integer
    6. Dim b As Integer
    7.  
    8. i = List1.ListCount
    9. x = 0
    10. b = 0
    11.  
    12. rst.Open "SELECT * from qnsdata_table", cn, adOpenDynamic, adLockOptimistic
    13.  
    14.     Do While Not x = i
    15.  
    16.         rst2.Open "SELECT DISTINCT Count(ansID) AS ans_count FROM ss_table WHERE '" & Me.List1.ItemData(x) & "' = qnsID", cn, adOpenDynamic, adLockOptimistic
    17.  
    18.         a = rst2!ans_count
    19.  
    20.         Do While Not b = a
    21.  
    22.             rst1.Open "SELECT qnsdataID from autonumber", cn, adOpenDynamic, adLockOptimistic
    23.             rst.AddNew
    24.             rst!qnsDataID = "qd" & Right("000000" & CStr(CInt(rst1!qnsDataID) + 1), 6)
    25.             rst!qnsID = Me.List1.ItemData(x)
    26.            
    27.             rst3.Open "SELECT DISTINCT ss_table.ansID AS ss_table_ansID from ss_table WHERE '" & rst!qnsID & "' = qnsID", cn, adOpenDynamic, adLockOptimistic
    28.    
    29.             rst!ansID = rst3!ss_table_ansID
    30.            
    31.             rst3.Close
    32.             rst.Update
    33.             rst1.Close
    34.             rst1.Open "UPDATE autonumber SET qnsdataID = qnsdataID +1", cn, adOpenDynamic, adLockOptimistic
    35.             b = b + 1
    36.  
    37.         Loop
    38.  
    39.         rst2.Close
    40.  
    41.         x = x + 1
    42.  
    43.     Loop
    44.  
    45. rst.Close
    46. MsgBox "okiedokz"
    47.  
    48.  
    49. End Sub


    Almost got it, rst!ansID = rst3!ss_table_ansID is now fixed, i can't do a rst.MoveNext, neither can i loop the recordset (not that i know how). Any suggestion? Haha i'm aware that my codes are crap long. Sorry.

    Thanks

    Astro

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