Results 1 to 5 of 5
  1. #1
    daniel is offline Novice
    Windows 11 Access 2021
    Join Date
    Jul 2025
    Posts
    3

    add csv data save to already written out code on command button that creates a PDF out of data

    Hello. I will post the code below. I have a form where supervisors input hourly figures from production lines into the access document. Once the info has been filled in there is a button that creates a PDF from inputted info, and emails it out to the directors and production manager. I want to add into the button code to create a csv file in a shared onedrive location, with the end goal having the csv sent to power BI where I will make tables and charts displaying the data.
    How would I go about inputting this? I am fairly new to MS Access and have only fiddled around on the face level, so not done any coding yet, I am watching videos on youtube that are giving some indication but nothing that actually helps with what I am trying to add, I know it's all translatable but I'm not quite good enough at it yet to have the confidence in myself to input this.
    Just as an FYI I didn't write this bit of code, my boss created this database a couple of years before I joined his team, so don't expect me to be an expert because the code looks written out properly.
    Another FYI, The database is being used 24/5, so I don't particularly want to mess it up by trialling, we have backups that I can use but I believe the backup reads off of the original database, so if I break one, I break the other.
    Option Compare Database

    Private Sub Command22_Click()
    Dim PauseTime, Start
    PauseTime = 1# ' Set duration in seconds
    Start = Timer ' Set start time.
    Do While Timer < Start + PauseTime
    DoEvents ' Yield to other processes.
    Loop

    DoCmd.OutputTo acOutputReport, "Hourly_Report_2021_Email_New", acFormatPDF, "\\172.20.247.4\MFL Common Files\ACCESS FEEDBACK\Feedback 2021\hourly2021.pdf", False



    Const cstrSMTPServer = "smtp.talktalkbusiness.net"

    Const cintCDOSendUsingPort = 25

    Const cintCDOSendUsing = 2

    Dim objConfig, objMsg


    Set objConfig = CreateObject("CDO.Configuration")


    objConfig.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cintCDOSendUsing

    objConfig.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = cintCDOSendUsingPort



    objConfig.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = cstrSMTPServer

    objConfig.Fields.Update


    strSubject = "Hourly Efficiency Update"

    strBody = "<P> Please find attached Hourly Efficiency Update. <P> Regards, <P>Feedback</P>"

    strFile = "\\172.20.247.4\MFL Common Files\ACCESS FEEDBACK\Feedback 2021\hourly2021.pdf"

    Set objMsg = CreateObject("CDO.Message")

    Set objMsg.Configuration = objConfig



    With objMsg

    .From = "feedback@marchfoods.com"

    .To = "fbhourly@marchfoods.com"

    .Subject = strSubject

    .HTMLBody = strBody

    .AddAttachment strFile

    .Send

    Kill "\\172.20.247.4\MFL Common Files\ACCESS FEEDBACK\Feedback 2021\hourly2021.pdf"


    End With

    MsgBox "Report sent successfully. Click OK to continue"
    End Sub

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,550
    Look at the TransferText command.
    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

  3. #3
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2021
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,740
    It appears that the data you want to send to the CSV is the same data sent to the PDF. That data is defined by the RecordSource property of the 'Hourly_Report_2021_Email_New' report. That would be what the TransferText command suggested by WGM above would need.

  4. #4
    daniel is offline Novice
    Windows 11 Access 2021
    Join Date
    Jul 2025
    Posts
    3
    So essentially I just need to add a new line and state csv as the file type instead of PDF I take it?
    will it automatically overwrite the previous version of the csv file?

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,550
    Look at the syntax https://learn.microsoft.com/en-us/of...d.transfertext
    Yes, it will overwrite the file each time. If you do not want that, then add the date to the filename, so it is different each time. If multiple in a day, add the time element as well.
    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. Add error code for export VBA if PDF is already open?
    By templeowls in forum Programming
    Replies: 3
    Last Post: 12-16-2021, 04:19 PM
  2. Replies: 1
    Last Post: 07-28-2020, 05:03 PM
  3. Replies: 1
    Last Post: 03-31-2016, 08:31 AM
  4. Replies: 2
    Last Post: 07-15-2015, 08:35 PM
  5. Replies: 3
    Last Post: 10-20-2014, 03:25 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