Results 1 to 2 of 2
  1. #1
    huk250 is offline Novice
    Windows 11 Access 2021
    Join Date
    Apr 2024
    Posts
    5

    Saving a report with as filename the records out of the query

    A good morning,


    I am looking for the following solution
    I have a tabelquery : "MCDQUERY"

    with the following columns : MCDNumber Country Priority
    DATA / MCD25 egypt p2
    MCD23 Gabon p1

    i would like to create a report with a selection on MCDNumber

    so the result should be EXAMPLE : MCD23_Gabon_p1.pdf


    enclosed below the vba that I already have created

    **********************
    Private Sub Knop15_Click()


    Dim db As DAO.Database
    Dim rs As DAO.QueryDef
    Dim fs As DAO.Recordset
    Dim MyFileName As String
    Dim mypath As String
    Dim temp As String




    mypath = "C:\Users\ADMIN\Desktop"


    Set db = CurrentDb()
    Set rs = db.QueryDefs("MCDQuery")
    rs.Parameters("MCDnumber") = Name

    Set fs = rs.OpenRecordset()


    Do While Not fs.EOF


    temp = fs("MCDnumber")
    MyFileName = fs("MCDnumber") & "_" & fs("Country") & "_" & fs("Priority") & ".PDF"

    DoCmd.OpenReport "MCD Invoice", acViewReport, , "[MCDnumber]=" & temp & ""


    DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName
    DoCmd.Close acReport, "MCD INVOICE"
    DoEvents


    fs.MoveNext
    Loop




    fs.Close
    Set rs = Nothing
    Set db = Nothing
    Set fs = Nothing


    End Sub
    ************************************************** ********

    Thanks for the assist
    Koen

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,963
    Not sure your recordset stntax is correct, I have never seen that before.
    As I always advise DEBUG.PRINT your variables and see what they are, not what you think they are.

    I used to use the .Fields("FiieldName") syntax. Mypath does not appear to being set correctly? I would expect to see a trailing backslash. Again debug.print highlights common mistakes like these.

    https://learn.microsoft.com/en-us/of...set-object-dao

    If temp is indeed a string you need to surround that with single quotes when concatenating.
    Name is a reserved word. Do not use Reserved words.
    https://learn.microsoft.com/en-us/of...reserved-words
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

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

Similar Threads

  1. Saving Records From A Report To A Table
    By Eranka in forum Access
    Replies: 6
    Last Post: 12-26-2017, 01:12 AM
  2. Append Query Saving Multiple Records
    By jewll in forum Queries
    Replies: 5
    Last Post: 12-13-2014, 03:54 AM
  3. Display the Filename in a Report.
    By GordonT in forum Access
    Replies: 1
    Last Post: 01-27-2013, 08:35 AM
  4. Exporting and saving file as [FORM]![FORM1]![FILENAME]
    By Elbows in forum Import/Export Data
    Replies: 1
    Last Post: 10-18-2011, 10:02 AM
  5. Replies: 0
    Last Post: 06-11-2009, 01:54 PM

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