Results 1 to 11 of 11

Thread: [RESOLVED] VBA Save Access Report As SNP File Name With Current Date

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Posts
    76

    Resolved [RESOLVED] VBA Save Access Report As SNP File Name With Current Date

    Hi, i am using the following code in Access to store a report as a snapshot file, which works perfectly.

    Code:
    Dim stDocName As String
        stDocName = "My Report"
    DoCmd.OutputTo acReport, stDocName, "SnapshotFormat(*.snp)", "C:\Documents and Settings\khadafi\My Documents\ReportFolder\MyReport.snp"
    I would like to save the current date within the snp files name, or for a new folder to be created within the ReportFolder with the current date as its name.

    Thanks For The Help In Advance.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA Save Access Report As SNP File Name With Current Date

    vb Code:
    1. DoCmd.OutputTo acReport, stDocName, "SnapshotFormat(*.snp)", "C:\Documents and Settings\khadafi\My Documents\ReportFolder\MyReport" & format(date, "yy-mm-dd") & ".snp"
    change the format to suit
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Posts
    76

    Re: VBA Save Access Report As SNP File Name With Current Date

    Hi, thanks that works great.

    Is there any way that i could get a new folder to be created, with the current date as its folder name, and the snapshot files to be saved in that folder.

    Many Thanks

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA Save Access Report As SNP File Name With Current Date

    vb Code:
    1. mystr =  format(date, "yy-mm-dd")
    2. mkdir "C:\Documents and Settings\khadafi\My Documents\ReportFolder\" & mystr
    3. DoCmd.OutputTo acReport, stDocName, "SnapshotFormat(*.snp)", "C:\Documents and Settings\khadafi\My Documents\ReportFolder\" & mystr & "\MyReport.snp"
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Posts
    76

    Re: VBA Save Access Report As SNP File Name With Current Date

    Hi, i got that working thanks.
    I just wanted to ask what is the purpose of mkdir in that code.

    Also, i want to save all the reports in my database into the same folder, what is the simplest way of doing this. i can add
    Code:
        
    stDocName = "My Report New"
    DoCmd.OutputTo acReport, stDocName, "SnapshotFormat(*.snp)", "C:\Documents and Settings\khadafi\My Documents\ReportFolder\MyReportNew.snp"
    for each report, but was wondering if there is an easier way of doing this.

    Also, i ran your code, and it worked perfectly, but the second time i run it, i get a Path File Error, possibly as a folder with the current date has already been created. Can i add the time to the folder name, which will allow me to run the code many times in a day.

    Sorry for so many questions.
    Thanks

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA Save Access Report As SNP File Name With Current Date

    mkdir is make directory

    you can test if the dirrectory already exists, using dir
    vb Code:
    1. if len(dir("C:\Documents and Settings\khadafi\My Documents\ReportFolder\" & mystr))=0 then mkdir "C:\Documents and Settings\khadafi\My Documents\ReportFolder\" & mystr

    Also, i want to save all the reports in my database into the same folder, what is the simplest way of doing this.
    do not create new folders if you want to save all the same folder

    yes you can add time, but do you really need that many new folders? maybe better to have on folder for day, put time in filenames
    vb Code:
    1. mystr = format(now, "yy-mm-dd hhnn")
    change format to suit, beware of invalid characters in filename / or : may be illegal characters
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Posts
    76

    Re: VBA Save Access Report As SNP File Name With Current Date

    Hi, as you suggested I am using the following code to store, reports in one folder for each day, with the time on the Report file names


    Code:
    Dim stDocName As String
    mystr = Format(Date, "dddd-mmmm-yyyy")
    MkDir "C:\Documents and Settings\khadafi\My Documents\ReportFolder\" & mystr
    stDocName = "My Report"
    DoCmd.OutputTo acReport, stDocName, "SnapshotFormat(*.snp)", "C:\Documents and Settings\khadafi\My Documents\ReportFolder\" & mystr & "\MyReport " & Format(Now, "(hh-nn AMPM)") & ".snp"
    this code works perfectly, however if i run it again, i get a file path error.
    I tried using, the
    Code:
    if len(dir("C:\Documents and Settings\khadafi\My Documents\ReportFolder\" & mystr))=0 then mkdir "C:\Documents and Settings\khadafi\My Documents\ReportFolder\" & mystr
    , but couldnt get that to work.

    Thanks.

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA Save Access Report As SNP File Name With Current Date

    oops, my appology, must add to look for directory and files

    vb Code:
    1. if len(dir("C:\Documents and Settings\khadafi\My Documents\ReportFolder\" & mystr, vbdirectory))=0 then mkdir "C:\Documents and Settings\khadafi\My Documents\ReportFolder\" & mystr
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Posts
    76

    Re: VBA Save Access Report As SNP File Name With Current Date

    Hi, thanks that works great.

    My code now is

    Code:
    Dim stDocName As String
    mystr = Format(Date, "dddd-mmmm-yyyy")
    if len(dir("C:\Documents and Settings\khadafi\My Documents\ReportFolder\" & mystr, vbdirectory))=0 then mkdir "C:\Documents and Settings\khadafi\My Documents\ReportFolder\" & mystr 
    stDocName = "My Report"
    DoCmd.OutputTo acReport, stDocName, "SnapshotFormat(*.snp)", "C:\Documents and Settings\khadafi\My Documents\ReportFolder\" & mystr & "\MyReport " & Format(Now, "(hh-nn AMPM)") & ".snp"

    One final thing, i want to save many reports into the same folder, for example Report 1, Report 2 etc.
    Do i need to add
    Code:
    stDocName = "My Report 1"
    DoCmd.OutputTo acReport, stDocName, "SnapshotFormat(*.snp)", "C:\Documents and Settings\khadafi\My Documents\ReportFolder\" & mystr & "\MyReport1 " & Format(Now, "(hh-nn AMPM)") & ".snp"
    to the code for each report, or is there an easier way of doing this.
    Thanks

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA Save Access Report As SNP File Name With Current Date

    vb Code:
    1. stDocName = "My Report 1"
    2. DoCmd.OutputTo acReport, stDocName, "SnapshotFormat(*.snp)", "C:\Documents and Settings\khadafi\My Documents\ReportFolder\" & mystr & "\" & strDocName & Format(Now, "(hh-nn AMPM)") & ".snp"
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Posts
    76

    Re: VBA Save Access Report As SNP File Name With Current Date

    Hi, i couldn't get that last bit to work, but thanks. I have just added each report the way i suggested in my previous post.

    Thank you for all your help.
    Regards

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