By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,268 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,268 IT Pros & Developers. It's quick & easy.

DoCmd.OutputTo Root folder question (Access)

P: 52
Hi, the following code seem to work fine if the user picks a folder in the root drive. If they select the root HDD the strPathPDF becomes (eg M:\)

M:\\ProductList.pdf

The file gets created but is it correct?? What am I missing?
Thanks




Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdCreatePDF_Click()
  2.     On Error GoTo ErrorHandler
  3.  
  4.     Dim strWHERE As String
  5.     Dim strPathPDF As String
  6.     Dim reportName As String
  7.     Dim fDlg As Object
  8.  
  9.     Set fDlg = Application.FileDialog(4)
  10.  
  11.     If Not IsNull(Me.ctlHomeChild.Form!ProductID) Then
  12.         strWHERE = Me.ctlHomeChild.Form.Filter 'Use the filter from the calling form.
  13.         reportName = "rptProductList"
  14.  
  15.         With fDlg
  16.             .Title = "Select a folder"
  17.             .AllowMultiSelect = False
  18.             .InitialFileName = "C:\"
  19.             If .Show = -1 Then
  20.                 strPathPDF = .SelectedItems(1) & "\" & "ProductList.pdf"
  21.  
  22.                 Debug.Print strPathPDF
  23.  
  24.                 DoCmd.OpenReport reportName, acViewReport, , strWHERE, acHidden
  25.  
  26.                 With Reports(reportName)
  27.                     Caption = "Product List Report " & Date
  28.                     .FilterOn = True
  29.                 End With
  30.  
  31.                 DoCmd.OutputTo acOutputReport, reportName, acFormatPDF, strPathPDF, False, , , acExportQualityPrint
  32.                 DoCmd.Close acReport, reportName
  33.  
  34.             End If
  35.         End With
  36.     Else
  37.         Beep
  38.     End If
  39.  
  40. exitSub:
  41.     Exit Sub
  42. ErrorHandler:
  43.     MsgBox "Error No: " & Err.Number & vbNewLine _
  44.          & "Error Details: " & Err.Description & vbNewLine _
  45.          & "Error in Sub: frmProducts_Database\cmdCreatePDF_Click"
  46.     Resume exitSub
  47.  
  48. End Sub
Apr 2 '20 #1

✓ answered by ADezii

It is because you are appending a Trailing Backslash to the Selected Folder (.SelectedItems(1)) which would not be appropriate should it be the Root Folder (C:\). Insert Lines 180 - 220 to correct this. This Code Segment could be shortened, but I kept it in this format for illustration purposes. Some Code has also been omitted.
Expand|Select|Wrap|Line Numbers
  1. 100 Dim strPathPDF As String
  2. 110 Dim fDlg As Object
  3.  
  4. 120 Set fDlg = Application.FileDialog(4)
  5.  
  6. 130 With fDlg
  7. 140  .Title = "Select a folder"
  8. 150  .AllowMultiSelect = False
  9. 160  .InitialFileName = "C:\"
  10. 170     If .Show = -1 Then
  11. 180       If Right$(.SelectedItems(1), 1) = "\" Then
  12. 190         strPathPDF = .SelectedItems(1) & "ProductList.pdf"
  13. 200       Else
  14. 210         strPathPDF = .SelectedItems(1) & "\" & "ProductList.pdf"
  15. 220       End If
  16. 230     End If
  17. 240 End With

Share this Question
Share on Google+
5 Replies


twinnyfo
Expert Mod 2.5K+
P: 3,482
Neruda,

I'm not sure we can answer your question:
Neruda:
The file gets created but is it correct??
We don't know if it is correct. are you getting reports from your users that the files is incorrect? This is where we must start.
Apr 6 '20 #2

ADezii
Expert 5K+
P: 8,698
It is because you are appending a Trailing Backslash to the Selected Folder (.SelectedItems(1)) which would not be appropriate should it be the Root Folder (C:\). Insert Lines 180 - 220 to correct this. This Code Segment could be shortened, but I kept it in this format for illustration purposes. Some Code has also been omitted.
Expand|Select|Wrap|Line Numbers
  1. 100 Dim strPathPDF As String
  2. 110 Dim fDlg As Object
  3.  
  4. 120 Set fDlg = Application.FileDialog(4)
  5.  
  6. 130 With fDlg
  7. 140  .Title = "Select a folder"
  8. 150  .AllowMultiSelect = False
  9. 160  .InitialFileName = "C:\"
  10. 170     If .Show = -1 Then
  11. 180       If Right$(.SelectedItems(1), 1) = "\" Then
  12. 190         strPathPDF = .SelectedItems(1) & "ProductList.pdf"
  13. 200       Else
  14. 210         strPathPDF = .SelectedItems(1) & "\" & "ProductList.pdf"
  15. 220       End If
  16. 230     End If
  17. 240 End With
Apr 6 '20 #3

P: 52
@twinnyfo
Hi What I mean I expected the path to be M:\
But I end up with M:\\
But it seems id does not make much difference, even if the path is M:\\\\\\\ it works just fine. I was just wondering if I was missing something in the code
Apr 7 '20 #4

P: 52
I changed it as suggested anyway, thank u ADezii
Apr 7 '20 #5

ADezii
Expert 5K+
P: 8,698
You're welcome, good luck with your Project!
Apr 7 '20 #6

Post your reply

Sign in to post your reply or Sign up for a free account.