Results 1 to 10 of 10

Thread: [RESOLVED] access vba looping issue

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Resolved [RESOLVED] access vba looping issue

    Hello there, heres all my code to add questions from a listbox into a database table along with the answers assigned to a particular question.

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



    VB Code:
    1. Do While Not rst3.EOF
    2.             rst!ansID = rst3!ansID
    3.             rst3.Update
    4.             rst3.MoveNext
    5.             Loop
    (extracted from Command6_Click)
    This doesn't appear to work, it is intended to add the list of answers assigned to a particular question.

    For example, qns000001 has ans000001, ans000002 and ans000003. After clicking on the command button. only ans000003 is added to qns000001 'thrice'.

    Any Suggestions?

    Thank You

    Astro

  2. #2
    Lively Member
    Join Date
    Aug 2005
    Posts
    77

    Re: access vba looping issue

    Use also
    VB Code:
    1. rst.MoveNext

    Pieter

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: access vba looping issue

    Hey Pieter,

    i've tried adding that line in different parts of my code. it didnt work.
    Thanks.

    Astro

  4. #4
    Lively Member
    Join Date
    Aug 2005
    Posts
    77

    Re: access vba looping issue

    I think you should add it here.
    VB Code:
    1. Do While Not (rst3.EOF Or rst.EOF)
    2.     rst!ansID = rst3!ansID
    3.     [B]rst.Update
    4.     rst.MoveNext[/B]
    5.     rst3.Update
    6.     rst3.MoveNext
    7. Loop
    BTW why do you update rst3. you didn't change it?

    Pieter

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: access vba looping issue

    Hey Pieter,

    You're right, i've removed rst3.Update. Sadly it's not working still after adding that line of code you last suggested. It's still adding the last item in the list.
    Am i looping the wrong way?

    Thanks

    Astro

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: access vba looping issue

    I've been trying to work out exactly what's going on in your code, and it doesn't make too much sense to me. I think it's probably best if we take a step back from the code for a moment, and start again.

    I can see you have tables called qnsdata_table, ss_table, and autonumber (bad name to use!).

    What is the structure of your database? How do the tables relate to each other?

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: access vba looping issue

    Looking at the color formatting of the code you posted, without going through it, it looks like you have missing or misplaced quotes.
    Take this line, for example:
    VB Code:
    1. rst2.Open "SELECT DISTINCT Count(ansID) AS ans_count FROM ss_table WHERE '" & _
    2. Me.List1.ItemData(x) & "' = qnsID", cn, adOpenDynamic, adLockOptimistic
    That looks ok, actually, but vbcode is formatting it as if everything after the single quote in "' = qnsID" is a comment. I don't know if I'm missing something in the code or if it's a vbcode formatting error. Is there a difference in your editor?
    FWIW, .MoveNext typically comes just before the Loop statement for a recordset.
    One thing you may want to check is if the data in your listbox contains single or double quotes. That can throw off your SQL. You may have to test the data and fix the string if it does. I.e., if your data is "St. Marys", you're ok , but if it's "St. Mary's", you could have a problem with the single quote. You can usually fix it by doing a Replace for single quotes with two single quotes, in that example.
    Last edited by salvelinus; Sep 11th, 2005 at 11:23 AM.
    Tengo mas preguntas que contestas

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: access vba looping issue

    That is a bug in the VBCode formatting of this site - the last ' always gets read as a comment.

    Absolutely right on the .MoveNext , and the use of Replace

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access vba looping issue

    You can do a workaround to prevent the false commenting with vbcode tags by adding a last dummy comment.
    VB Code:
    1. rst2.Open "SELECT DISTINCT Count(ansID) AS ans_count FROM ss_table WHERE '" & _ 'Dummy apostrophy :D
    2. Me.List1.ItemData(x) & "' = qnsID", cn, adOpenDynamic, adLockOptimistic 'Dummy apostrophy :D
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: access vba looping issue

    Hey Pieter,

    I got it man!

    Appreciate that you took the time to understand my codes.
    I'll paste the entire chunk and explain it at the bottom.

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

    I've removed a fuzzy wuzzy loop, so im down to 2 loops now.

    VB Code:
    1. rst3.Open "SELECT DISTINCT ansID FROM ss_table WHERE '" & Me.List1.ItemData(x) & "' = qnsID", cn, adOpenDynamic, adLockOptimistic
    2.             rst3.MoveFirst
    3.             Do While Not rst3.EOF
    4.             rst1.Open "SELECT qnsdataID from autonumber", cn, adOpenDynamic, adLockOptimistic
    5.             rst.AddNew
    6.             rst!qnsDataID = "qd" & Right("000000" & CStr(CInt(rst1!qnsDataID) + 1), 6)
    7.             rst!qnsID = Me.List1.ItemData(x)
    8.             rst!ansID = rst3!ansID
    9.             rst3.MoveNext
    10.             rst.Update
    11.             rst1.Close
    12.             rst1.Open "UPDATE autonumber SET qnsdataID = qnsdataID +1", cn, adOpenDynamic, adLockOptimistic
    13.             Loop
    14.             rst3.Close
    15.             rst2.Close

    I did a Do While Not rst3.EOF and right before it, added rst3.MoveFirst.
    Thats it =)

    Thanks man!

    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