Results 1 to 2 of 2
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,185

    Loop Part Of Recordset


    Hi Guy's, is it possible to loop through part of a recordset, ie we only require Date once, Invoice Number once Customer once but there may be 4 records of:

    Invoice Details, Hours, Rate, Amount

    I have in the invoice details Monday, Tuesday, Wednesday, Friday so there are 4 records

    The Excel file is showing Friday only then coming up with Object required and debug highlights this line:
    .Worksheets(1).Cells(13, 9) = rs.Fields("Amount")


    Code:
    Set ApXL = CreateObject("Excel.Application")
    Set xlWB = ApXL.Workbooks.Open(XLTemp)
    ApXL.ActiveWorkbook.SaveAs XLPath & XLFile
    ApXL.Workbooks.Open XLPath & XLFile, True, False
    ApXL.Visible = True
    Code:
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("SELECT * FROM tblInvoices WHERE Customer = '" & strCustName & "'" & " And InvoiceNumber = " & InvNo)
    With xlWB
    .Worksheets(1).Cells(1, 9) = rs.Fields("InvoiceNumber")
    .Worksheets(1).Cells(2, 9) = rs.Fields("InvoiceDate")
    .Worksheets(1).Cells(4, 9) = rs.Fields("Customer")
    .Worksheets(1).Cells(5, 9) = rs.Fields("Add1")
    .Worksheets(1).Cells(6, 9) = rs.Fields("Add2")
    .Worksheets(1).Cells(7, 9) = rs.Fields("Town")
    .Worksheets(1).Cells(8, 9) = rs.Fields("PostCode")
    Do Until rs.EOF
    .Worksheets(1).Cells(13, 1) = rs.Fields("InvoiceDetails")
    .Worksheets(1).Cells(13, 7) = rs.Fields("Hours")
    .Worksheets(1).Cells(13, 8) = rs.Fields("Rate")
    .Worksheets(1).Cells(13, 9) = rs.Fields("Amount")
    Loop
    .Worksheets(1).Cells(44, 9) = DSum("Amount", "tblInvoices", "[InvoiceNumber] = " & InvNo)
    .Worksheets(1).PageSetup.FitToPagesWide = 1
    .Worksheets(1).PageSetup.FitToPagesTall = 1
    .Worksheets(1).PageSetup.Orientation = xlLandscape
    End With
    xlWB.Close
    ApXL.Quit
    Set ApXL = Nothing

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,816
    You might have 4 records but only the last one will be saved to worksheet cells because same cells are referenced in loop. But you really should end up in an infinite loop because there is no rs.MoveNext line. As to why Amount line errors, really have no idea.

    Provide sample of data and desired output.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 10-26-2018, 03:40 PM
  2. Need your help in a loop in the recordset
    By Muntasser in forum Programming
    Replies: 3
    Last Post: 03-19-2018, 12:04 PM
  3. Loop Not Progressing Through Recordset
    By nmlinac in forum Programming
    Replies: 1
    Last Post: 02-15-2013, 10:54 AM
  4. Double loop recordset
    By silverspr in forum Programming
    Replies: 7
    Last Post: 03-07-2012, 01:43 PM
  5. Loop through recordset
    By eliedebs in forum Programming
    Replies: 1
    Last Post: 01-11-2012, 12:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums