I copied the code below to export a query-based report as PDF. The code itself works well. The report was successfully automatically converted to a PDF as intended. However, after I ran the VBA code, I encountered an error message "Either an object bound to the table is open or another user has the table open. Do you want to open the table as read-only?
" and I can't design view the source table without reopening the database. No other objects than the report were open or in use.
I first thought there was something wrong with the code. I created a built-in macro (ExportWithFormatting) to get the same function and it worked, but the same problem is still happening: Design-viewing the source table is still not allowed after I run the macro.
I tried to how to solve this problem by myself for about half a day, but I still can't find it.
What is the cause? How can I fix the problem? Any solution would be welcome... Thank you.
Function FileExist(FileFullPath As String) As Boolean
Dim value As Boolean
value = False
If Dir(FileFullPath) <> "" Then
value = True
FileExist = value
Private Sub cmdExportAsPDF_Click() '_Click()
Dim fileName As String, fldrPath As String, filePath As String
Dim answer As Integer
fileName = QuotationNumber 'filename for PDF file
fldrPath = "C:\Users\" & Environ$("username") & "\Desktop\mak" 'folder path where pdf file will be saved *
filePath = fldrPath & "\" & fileName & ".pdf"
'check if file already exists
If FileExist(filePath) Then
answer = MsgBox(prompt:="PDF file already exists: " & vbNewLine & filePath & vbNewLine & vbNewLine & _
"Would you like to replace existing file?", buttons:=vbYesNo, Title:="Existing PDF File")
If answer = vbNo Then Exit Sub
On Error GoTo invalidFolderPath
DoCmd.OutputTo objecttype:=acOutputReport, objectName:=Me.Name, outputformat:=acFormatPDF, outputFile:=filePath
MsgBox prompt:="PDF File exported to: " & vbNewLine & filePath, buttons:=vbInformation, Title:="Report Exported as PDF"
MsgBox prompt:="Error: Invalid folder path. Please update code.", buttons:=vbCritical