Results 1 to 4 of 4
  1. #1
    mercapto is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jan 2012
    Location
    Spain
    Posts
    91

    To loop output for all the records in a form

    Hello.

    I have a form with a pdf output button. The button has this code:
    Code:
    Private Sub Comando18_Click()
    Me.Refresh
    
    Dim myPath As String
    Dim stDocName As String
    Dim theFileName As String
    
    stDocName = "Hoja de Pedido"
    DoCmd.OpenReport stDocName, acPreview, , "NumPedido = " & Nz(Me.NumPedido, 0)
    
    myPath = "C:\"
    theFileName = "Pedido " & NumPedido & ".pdf"
    
    DoCmd.OutputTo acOutputReport, stDocName, acFormatPDF, theFileName, False
    
    End Sub
    So everytime I press the button I get a pdf file. I want to make a macro that allows me to loop this task with all the records loaded in the form, generating a single pdf for each.

    How can I do it??



    thanks a lot.

    Offtopic: as I said in a previous thread, my code always outputs in My Documents (not in mypath), if anyone knows how to fix this...

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    The path is easy; you didn't use the myPath variable, so you only have a file name in the OutputTo. You can either loop the recordset clone of the form, or a simple recordset if you know how the form is filtered.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    mercapto is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jan 2012
    Location
    Spain
    Posts
    91
    Quote Originally Posted by pbaldy View Post
    The path is easy; you didn't use the myPath variable, so you only have a file name in the OutputTo.
    LOL...
    Now I remember that I didn't know were to put this variable, because either in theFileName line or in the cmd line it gives me error 2501.

    Quote Originally Posted by pbaldy View Post
    You can either loop the recordset clone of the form, or a simple recordset if you know how the form is filtered.
    Can you translate this to code? My VB knowledge goes from copy-paste to if/then

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Here's my template code for a loop:
    Code:
      Dim strSQL  As String
      Dim db      As DAO.Database
      Dim rs      As DAO.Recordset
    
      Set db = CurrentDb()
      
      strSQL = "SELECT ..."  'replace with appropriate SQL statement
      Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
    
      Do While Not rs.EOF
        'code to work with current record here
        rs.MoveNext
      Loop
    
      set rs = nothing
      set db = nothing
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 1
    Last Post: 06-15-2012, 10:47 AM
  2. VBA Loop to Combine Records
    By admessing in forum Queries
    Replies: 23
    Last Post: 03-06-2012, 11:37 AM
  3. Loop through records
    By sam10 in forum Programming
    Replies: 12
    Last Post: 07-07-2011, 02:30 PM
  4. Loop through subform records
    By asmith in forum Forms
    Replies: 6
    Last Post: 10-06-2010, 10:31 AM
  5. Loop through Records and Make ID
    By rob4465 in forum Programming
    Replies: 3
    Last Post: 01-14-2010, 10:46 AM

Tags for this Thread

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