Results 1 to 6 of 6

Thread: *RESOLVED*Whats wrong with my code- Please have a look!!

  1. #1

    Thread Starter
    Frenzied Member mxnmx's Avatar
    Join Date
    Dec 2001
    Location
    I'm back...now!!!
    Posts
    1,396

    *RESOLVED*Whats wrong with my code- Please have a look!!

    Here;s my code and I get am error: NEXT WITHOUT FOR...pls have a look...

    VB Code:
    1. n = 0
    2. conDB
    3. Set oRs = New Recordset
    4. strSql = "Select * from Transactions where Status='Unpaid'"
    5. oRs.Open strSql, goCnn, adOpenKeyset, adLockOptimistic, adCmdText
    6. x = oRs.RecordCount
    7. axn
    8. Do While Not oRs.EOF
    9.  
    10. strItem = oRs("Group Name")
    11. Set oRst = New Recordset
    12. strsql1 = "Select * from Transactions where [Group Name]='" & strItem & "'" & " And status='Unpaid'"
    13. oRst.Open strsql1, axnCnn, adOpenKeyset, adLockOptimistic
    14. Set oRsy = New Recordset
    15. strsql2 = "Select * from Groups where [Group Name]='" & strItem & "'"
    16. oRsy.Open strsql2, axnCnn, adOpenKeyset, adLockOptimistic
    17. y = oRst.RecordCount
    18.  
    19.  For i = 1 To y
    20.          If n = 0 Then
    21.          Set WordObject = CreateObject("Word.Application")
    22.     With WordObject
    23.         .Application.Documents.Add App.Path & "\Invoice.dot"
    24.         .Visible = True
    25.         .Activate
    26.         .ActiveDocument.Bookmarks("Date").Range.InsertAfter Format(Date, "DD/MM/YYYY")
    27.         .ActiveDocument.Bookmarks("GROUPNAME").Range.InsertAfter oRst("Group Name")
    28.         .ActiveDocument.Bookmarks("GROUPID").Range.InsertAfter oRst("GroupID")
    29.         .ActiveDocument.Bookmarks("Address").Range.InsertAfter oRsy("Address")
    30.         .ActiveDocument.Bookmarks("TELEPHONE").Range.InsertAfter oRsy("Phone")
    31.              
    32.         .Selection.MoveDown wdLine, 17
    33.         .Selection.MoveLeft wdCharacter, 2
    34.         Do While oRst.EOF = False
    35.             .Selection.TypeText oRst!Invoice_ID
    36.             .Selection.MoveRight wdCharacter, 1
    37.             .Selection.TypeText oRst!VisitDate
    38.             .Selection.MoveRight wdCharacter, 1
    39.             .Selection.TypeText oRst!TreatMent
    40.             .Selection.MoveRight wdCharacter, 1
    41.             .Selection.TypeText oRst!AmountCharged
    42.             .Selection.MoveRight wdCharacter, 1
    43.             .Selection.TypeText oRst!AmountPaid
    44.             .Selection.MoveRight wdCharacter, 1
    45.             .Selection.TypeText oRst!AmountToPay
    46.             .Selection.MoveRight wdCharacter, 1
    47.             .Selection.TypeText oRst!Status
    48.             oRst.MoveNext
    49.             If oRst.EOF = False Then
    50.                 .Application.ActiveDocument.Tables(2).Rows.Add
    51.                 .Selection.MoveDown wdLine, 1
    52.                 .Selection.MoveLeft wdCharacter, 6
    53.             End If
    54.          Loop
    55.     Next
    56.    End With
    57.    End If
    Last edited by mxnmx; Feb 14th, 2003 at 07:05 PM.
    Can't Remember Birthdays or Important Dates- Never Miss any Important Date(s)

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    You need to swap the NEXT and the End With statements at the bottom of the code.

  3. #3

    Thread Starter
    Frenzied Member mxnmx's Avatar
    Join Date
    Dec 2001
    Location
    I'm back...now!!!
    Posts
    1,396
    Thanks Shaggy, that does not work...
    Can't Remember Birthdays or Important Dates- Never Miss any Important Date(s)

  4. #4
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    it doesn't work because he found a glaring error and didn't look beyond it. ALL of your loop control is screwed up.

    Do proper indentation of your code and then look to see that for matches next and do matches loop and so forth.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    You're right Phinds, those last lines are all scrambled.

    If you indent each section of code, and get stuck with a problem like this, you can put your caret at the beginning of each control line, and down error to the end of that level of indentation to see if you close with the correct word. In this case, I saw that the NEXT was inside the End With, and looked no further, since the For was outside the With. In fact, the for is outside the If statement as well, so the End If must be moved inside the NEXT.

    The last three lines should be:

    End With
    End If
    Next

  6. #6

    Thread Starter
    Frenzied Member mxnmx's Avatar
    Join Date
    Dec 2001
    Location
    I'm back...now!!!
    Posts
    1,396
    Thanks phinds & shaggy...
    I sorted it, was just a silly mistake...
    Can't Remember Birthdays or Important Dates- Never Miss any Important Date(s)

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