Write report to file code

B

blake7

Hi I have the following code behind a button which writes my report to disc
ok, my question is - is it possible to write the destination drive and
target folder into this code so it does not have to be chosen by the
operator? if so how - Thanks

Private Sub Command29_Click()
On Error GoTo Err_Command29_Click
Dim stDocName As String
stDocName = "DailyAuditReport2"
DoCmd.OutputTo acReport, stDocName, acFormatRTF
Exit_Command29_Click:
Exit Sub
Err_Command29_Click:
MsgBox Err.Description
Resume Exit_Command29_Click
End Sub
 
A

Allen Browne

The OutputTo has an additional argument for the file name, e.g.:
Dim strFile As String
strFile = "C:\SomeFolder\SomeFile.rtf"
DoCmd.OutputTo acOutputReport, stDocName, acFormatRTF, strFile
 
B

blake7

Hi Allen, thanks for your help on this, I have changed my code as below but I
am getting the following error message "Microsoft Office Access can't save
the output data to the file you've selected" can you shed any light on this
for me ?

Private Sub Command29_Click()
On Error GoTo Err_Command29_Click
Dim stDocName As String
Dim strFile As String
stDocName = "DailyAuditReport2"
strFile = "C:\Audit Report\DailyAuditReport2.rtf"
DoCmd.OutputTo acOutputReport, stDocName, acFormatRTF, strFile
Exit_Command29_Click:
Exit Sub

Err_Command29_Click:
MsgBox Err.Description
Resume Exit_Command29_Click
 
A

Allen Browne

Could be several things, e.g. the may already be open, or you may not have
permissions to write to that location.
 
B

blake7

Hi Allen, I have tried and tried to get this to work but I cannot, I have
tried it on main pc and my laptop, I created a folder on my C drive to send
the file to but it just comes back with the same message every time, any
other suggestions ? Thanks Tony.
 
A

Allen Browne

With writing files, there are so many possible things that coudl go wrong,
Tony. We really can't identify all the possibilites for you.

One of the Access quirks is that it writes only registered file types. Your
example uses RTF, which will be registered on any machine that has Office
installed, so that can't apply.

Other things could be a diverse as a bad file name, invalid, path, drive
full, drive corrupt (chkdsk problem), temp environment variable invalid, a
read-only file (or folder) with that name already exists, Windows
permissions, viral infection, drive not available, etc, etc.
 
B

blake7

Thanks for your help Allen, after reading what you have posted, I have
decided to just use the inbuilt wizard to create the button and let the user
define the file name and path, it seems much easier. Thanks once again for
your advice. Regards Tony.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top