Microsoft 365 Access VBA Error

Jolitz, Thomas 0 Reputation points
2025-11-03T17:29:43.63+00:00

I created an Access database that loads tables from csv files, runs two queries and creates a csv report file through the use of modules. macros and import and export file specification. In its current format it works correctly.

A change to the csv report file has been requested. The change consists of removing unnecessary columns from the CSV file. I have tried several iterations of database changes to accomplish the request, but run into an MS Access error. The latest change was only to the query that creates the report by removing the unnecessary columns and modifying the export specs. I have deleted the CSV file from the directory and closed and reopened the Access program. If I right click the query and select export, I can create the csv file. However, I only get the the header record written to the csv file and the following error if I run the module:

CreatePerlReport 3051: The Microsoft access database engine cannot open or write to the file 'Perl_Split_20251031.csv'. It is already opened exclusively by another user, or you need permission to view and write its data.

This code is in a test environment so there will be extra comment lines.

Public Function CreatePerlReport()
    On Error GoTo Error_Routine
'Declare variables
    Dim dtGetCurrentDate        As Date                 'Use the date function
    Dim intErrCount             As Integer              'Error handling switch
    Dim strReportDate           As String               'Date in YYYYMMDD format to be added to perl report name
    Dim strQuery                As String               'Variable for SQL query statement
    Dim strQueryName            As String               'Variable for query name
    Dim strFilePath1            As String               'Variable for the backup location that the CSV file will be stored in
    Dim strFilePath2            As String               'Variable for the primary location that the CSV file will be stored in
    Dim strFileName             As String               'Variable for the CSV file name
    Dim strSpecName             As String               'Varaiable for the export specification
    Dim LogMessage              As String               'Variable for error message displayed in logfile
    Dim dbs                     As DAO.Database         'Current database
    Dim rst                     As DAO.Recordset        'Current recordset
    Dim qdf                     As DAO.QueryDef         'Current query
    
' --- Configuration ---
    strReportDate = Format(Date, "yyyymmdd")
    intErrCount = 0
' --- End Configuration ---
     
'Select EPICUNK Report query parameters
    strQuery = "SELECT Format(Date(),'yyyymmdd') AS [SPLIT DATE], Original_Results.FILENAME, EPICUNK_Results.ST02, " _
                & "IIf(IsNull([Original_Results]![TIN]),[Original_Results]![NPI],[Original_Results]![TIN]) AS TIN, Original_Results.NPI, " _
                & "Original_Results.PAYERNAME, Original_Results.[PMT TYPE], Original_Results.[CHECK NUMBER], Original_Results.[CHECK DATE], " _
                & "Original_Results.[CHECK AMOUNT], [Original_Results]![CHECK AMOUNT]-[EPICUNK_Results]![CHECK AMOUNT] AS [VARIANCE/UNK POSTED TO EPIC], " _
                & "EPICUNK_Results.[CHECK AMOUNT] AS [UNK PROCEESED IN EPIC FH-IN REMIT WQ] " _
            & "FROM Original_Results LEFT JOIN EPICUNK_Results ON Original_Results.[CHECK NUMBER] = EPICUNK_Results.[CHECK NUMBER];"
'Delete old EPICUNK Report query results if the query exists. Then insert the new query results.
    Set dbs = CurrentDb()
    For Each qdf In dbs.QueryDefs
        If qdf.Name = "EPICUNK_Report" Then
            dbs.QueryDefs.Delete "EPICUNK_Report"
            Exit For
        End If
    Next
    Set rst = dbs.OpenRecordset(strQuery, dbOpenSnapshot)
    With dbs
        Set qdf = .CreateQueryDef("EPICUNK_Report", strQuery)
    End With
'Select Perl Report query parameters
    strQuery = "SELECT DISTINCT Format(Date(),'yyyymmdd') AS [SPLIT DATE], Original_Results.FILENAME, Original_Results.ST02, " _
                & "IIf(IsNull([Original_Results]![TIN]),[Original_Results]![NPI],[Original_Results]![TIN]) AS TIN, " _
                & "Original_Results.NPI AS [TIN/NPI], Original_Results.PAYERNAME, Original_Results.[PMT TYPE], Original_Results.[CHECK NUMBER], " _
                & "Original_Results.[CHECK AMOUNT], Null AS VARIANT, Original_Results.[CHECK AMOUNT] AS [EPIC FH], " _
                & "EPICUNK_Report.[UNK PROCEESED IN EPIC FH-IN REMIT WQ], Null AS [VARIANCE/UNK POSTED TO EPIC FH], Null AS [EPIC FH BATCH NUM] " _
             & "FROM (Original_Results " _
                & "LEFT JOIN EPICFH_Results ON (Original_Results.[CHECK DATE] = EPICFH_Results.[CHECK DATE]) " _
                   & "AND (Original_Results.[CHECK NUMBER] = EPICFH_Results.[CHECK NUMBER]) AND (Original_Results.ST02 = EPICFH_Results.ST02)) " _
                & "LEFT JOIN EPICUNK_Report ON (Original_Results.[CHECK DATE] = EPICUNK_Report.[CHECK DATE]) " _
                   & "AND (Original_Results.[CHECK NUMBER] = EPICUNK_Report.[CHECK NUMBER]) AND (Original_Results.ST02 = EPICUNK_Report.ST02);"
'Delete old Perl Report query results if the query exists. Then insert the new query results.
    For Each qdf In dbs.QueryDefs
        If qdf.Name = "Perl_Split" Then
            dbs.QueryDefs.Delete "Perl_Split"
            Exit For
        End If
    Next
    Set rst = dbs.OpenRecordset(strQuery, dbOpenSnapshot)
    With dbs
        Set qdf = .CreateQueryDef("Perl_Split", strQuery)
    End With
    
'Perl Report CSV file and paths configuration
    strQueryName = "Perl_Split"
    strFileName = strQueryName & "_" & strReportDate & ".csv"
    strFilePath1 = "Z:\Production\IDX\Perl_Split_835_Files\BALANCER_REPORT\"
    'strFilePath2 = "L:\CPS_PB_BillingCollectionsReimbursement\03_Cashiering-Posting-Recon\3_Reconciliation\835 EDI Perl Split Reports\"
    strSpecName = "Perl Split Export Specs"
        
'Send semicolon delimited CSV file to the backup and primary location
    DoCmd.TransferText acExportDelim, strSpecName, strQueryName, strFilePath1 & strFileName, True
    'DoCmd.TransferText acExportDelim, strSpecName, strQueryName, strFilePath2 & strFileName, True
Microsoft 365 and Office | Access | For business | Windows
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. George Hepworth 22,090 Reputation points Volunteer Moderator
    2025-11-03T19:57:36.6933333+00:00

    The error message is clear: A file with the same name as the one you are trying to create -- presumably the .csv file --is already open, and in use, by another process. Your code is trying to modify or overwrite that file, a step which is blocked by virtue of its being open in the other process.

    Have you inserted a breakpoint in your code and stepped through it to confirm which line raises the error?

    Have you tried manually deleting the blocking .csv file before running your process?

    Is there another step in your overall workflow where you open the exported .csv file after creating it?


  2. Jolitz, Thomas 0 Reputation points
    2025-11-03T20:31:02.8033333+00:00

    Have you inserted a breakpoint in your code and stepped through it to confirm which line raises the error?

    Yes, I used the step through and the error code is initiated when the following line of code runs:

     DoCmd.TransferText acExportDelim, strSpecName, strQueryName, strFilePath1 & strFileName, True
    
    

    Have you tried manually deleting the blocking .csv file before running your process?

    From the original post, "I have deleted the CSV file from the directory and closed and reopened the Access program."


    Is there another step in your overall workflow where you open the exported .csv file after creating it?

    My program has three distinct macros. The first one clears the import tables. The second macro loads the the tables with the new daily data. The third macro creates the CSV file and appends the current date in yyymmdd format to create a unique file.


    In fact, the working version of this vba code has more columns in the csv file. The only difference is the second sql statement is bigger. See below:

    Public Function CreatePerlReport()
        On Error GoTo Error_Routine
    
    'Declare variables
        Dim dtGetCurrentDate        As Date                 'Use the date function
        Dim intErrCount             As Integer              'Error handling switch
        Dim strReportDate           As String               'Date in YYYYMMDD format to be added to perl report name
        Dim strQuery                As String               'Variable for SQL query statement
        Dim strQueryName            As String               'Variable for query name
        Dim strFilePath1            As String               'Variable for the backup location that the CSV file will be stored in
        Dim strFilePath2            As String               'Variable for the primary location that the CSV file will be stored in
        Dim strFileName             As String               'Variable for the CSV file name
        Dim strSpecName             As String               'Varaiable for the export specification
        Dim LogMessage              As String               'Variable for error message displayed in logfile
        Dim dbs                     As DAO.Database         'Current database
        Dim rst                     As DAO.Recordset        'Current recordset
        Dim qdf                     As DAO.QueryDef         'Current query
        
    ' --- Configuration ---
        strReportDate = Format(Date, "yyyymmdd")
        intErrCount = 0
    ' --- End Configuration ---
         
    'Select EPICUNK Report query parameters
        strQuery = "SELECT Format(Date(),'yyyymmdd') AS [SPLIT DATE], Original_Results.FILENAME, EPICUNK_Results.ST02, " _
                    & "IIf(IsNull([Original_Results]![TIN]),[Original_Results]![NPI],[Original_Results]![TIN]) AS TIN, Original_Results.NPI, " _
                    & "Original_Results.PAYERNAME, Original_Results.[PMT TYPE], Original_Results.[CHECK NUMBER], Original_Results.[CHECK DATE], " _
                    & "Original_Results.[CHECK AMOUNT], [Original_Results]![CHECK AMOUNT]-[EPICUNK_Results]![CHECK AMOUNT] AS [VARIANCE/UNK POSTED TO EPIC], " _
                    & "EPICUNK_Results.[CHECK AMOUNT] AS [UNK PROCEESED IN EPIC FH-IN REMIT WQ] " _
                & "FROM Original_Results INNER JOIN EPICUNK_Results ON Original_Results.[CHECK NUMBER] = EPICUNK_Results.[CHECK NUMBER];"
    
    'Delete old EPICUNK Report query results if the query exists. Then insert the new query results.
        Set dbs = CurrentDb()
        For Each qdf In dbs.QueryDefs
            If qdf.Name = "EPICUNK_Report" Then
                dbs.QueryDefs.Delete "EPICUNK_Report"
                Exit For
            End If
        Next
        Set rst = dbs.OpenRecordset(strQuery, dbOpenSnapshot)
        With dbs
            Set qdf = .CreateQueryDef("EPICUNK_Report", strQuery)
        End With
    'Select Perl Report query parameters
        strQuery = "SELECT DISTINCT Format(Date(),'yyyymmdd') AS [SPLIT DATE], Original_Results.FILENAME, Original_Results.ST02, " _
                    & "IIf(IsNull([Original_Results]![TIN]),[Original_Results]![NPI],[Original_Results]![TIN]) AS TIN, " _
                    & "Original_Results.NPI AS [TIN/NPI],Original_Results.PAYERNAME, Original_Results.[PMT TYPE], Original_Results.[CHECK NUMBER], " _
                    & "Original_Results.[CHECK DATE], Original_Results.[CHECK AMOUNT], Null AS VARIANCE, EPICFH_Results.[CHECK AMOUNT] AS [EPIC FH], " _
                    & "EPICCSG_Results.[CHECK AMOUNT] AS [EPIC CSG] , IDX_G34_Results.[CHECK AMOUNT] AS [IDX G34], IDX_G7_Results.[CHECK AMOUNT] AS [IDX G7], " _
                    & "EPICFHASC_Results.[CHECK AMOUNT] AS [FH/HB ASC], [EPICUNK_Report].[UNK PROCEESED IN EPIC FH-IN REMIT WQ], " _
                    & "Null AS [VARIANCE/UNK POSTED TO EPIC FH], Null AS [EPIC FH BATCH#], Null AS [VARIANCE/UNK POSTED TO EPIC CSG], Null AS [EPIC CSG BATCH#], " _
                    & "Null AS [VARIANCE/UNK POSTED TO IDX G34], Null AS [IDX G34 BATCH#], Null AS [VARIANCE/UNK POSTED TO IDX G7], Null AS [IDX G7 BATCH#] " _
                 & "FROM (((((Original_Results " _
                    & "LEFT JOIN EPICFH_Results ON (Original_Results.ST02 = EPICFH_Results.ST02) " _
                        & "AND (Original_Results.[CHECK NUMBER] = EPICFH_Results.[CHECK NUMBER]) AND (Original_Results.[CHECK DATE] = EPICFH_Results.[CHECK DATE]) AND (Original_Results.PAYERNAME = EPICFH_Results.PAYERNAME)) " _
                    & "LEFT JOIN EPICCSG_Results ON (Original_Results.ST02 = EPICCSG_Results.ST02) " _
                        & "AND (Original_Results.[CHECK NUMBER] = EPICCSG_Results.[CHECK NUMBER]) AND (Original_Results.[CHECK DATE] = EPICCSG_Results.[CHECK DATE]) AND (Original_Results.PAYERNAME = EPICCSG_Results.PAYERNAME)) " _
                    & "LEFT JOIN IDX_G34_Results ON (Original_Results.ST02 = IDX_G34_Results.ST02) " _
                        & "AND (Original_Results.[CHECK NUMBER] = IDX_G34_Results.[CHECK NUMBER]) AND (Original_Results.[CHECK DATE] = IDX_G34_Results.[CHECK DATE]) AND (Original_Results.PAYERNAME = IDX_G34_Results.PAYERNAME)) " _
                    & "LEFT JOIN IDX_G7_Results ON (Original_Results.ST02 = IDX_G7_Results.ST02) " _
                        & "AND (Original_Results.[CHECK NUMBER] = IDX_G7_Results.[CHECK NUMBER]) AND (Original_Results.[CHECK DATE] = IDX_G7_Results.[CHECK DATE]) AND (Original_Results.PAYERNAME = IDX_G7_Results.PAYERNAME)) " _
                    & "LEFT JOIN EPICFHASC_Results ON (Original_Results.ST02 = EPICFHASC_Results.ST02) " _
                        & "AND (Original_Results.[CHECK NUMBER] = EPICFHASC_Results.[CHECK NUMBER]) AND (Original_Results.[CHECK DATE] = EPICFHASC_Results.[CHECK DATE]) AND (Original_Results.PAYERNAME = EPICFHASC_Results.PAYERNAME)) " _
                    & "LEFT JOIN EPICUNK_Report ON (Original_Results.ST02 = [EPICUNK_Report].ST02) " _
                        & "AND (Original_Results.[CHECK NUMBER] = [EPICUNK_Report].[CHECK NUMBER]) AND (Original_Results.[CHECK DATE] = [EPICUNK_Report].[CHECK DATE]) AND (Original_Results.PAYERNAME = [EPICUNK_Report].PAYERNAME);"
    
    'Delete old Perl Report query results if the query exists. Then insert the new query results.
        For Each qdf In dbs.QueryDefs
            If qdf.Name = "Perl_Split" Then
                dbs.QueryDefs.Delete "Perl_Split"
                Exit For
            End If
        Next
        Set rst = dbs.OpenRecordset(strQuery, dbOpenSnapshot)
        With dbs
            Set qdf = .CreateQueryDef("Perl_Split", strQuery)
        End With
        
    'Perl Report CSV file and paths configuration
        strQueryName = "Perl_Split"
        strFileName = strQueryName & "_" & strReportDate & ".csv"
        strFilePath1 = "G:\Production\IDX\Perl_Split_835_Files\BALANCER_REPORT\"
        strFilePath2 = "L:\CPS_PB_BillingCollectionsReimbursement\03_Cashiering-Posting-Recon\3_Reconciliation\835 EDI Perl Split Reports\"
        strSpecName = "Perl Split Export Specs"
            
    'Send semicolon delimited CSV file to the backup and primary location
        DoCmd.TransferText acExportDelim, strSpecName, strQueryName, strFilePath1 & strFileName, True
        DoCmd.TransferText acExportDelim, strSpecName, strQueryName, strFilePath2 & strFileName, True
        
    'Send email to operations and CPS balancers
        MsgBox "Make sure Outlook is open in the rdp session before pressing OK."
        Call SendOutlookEmail(strFileName)
        
    'Display end of module message
        MsgBox "The Perl Split Report csv file was created and email sent. Press OK to continue."
               
    'Closing database variables
    Exit_Routine:
        strQuery = ""
        rst.Close
        qdf.Close
        dbs.Close
        
    'Exiting function and returning control to Access
        Set rst = Nothing
        Set qdf = Nothing
        Set dbs = Nothing
        
    'Determine ending log statement
        If intErrCount = 0 Then
            LogMessage = "Create Perl Report macro completed successfully."
        Else
            LogMessage = "Create Perl Report macro completed."
        End If
        Call LogToFile(LogMessage)
        
    Exit Function
    
    ' Save error message to the logfile and display it.
    Error_Routine:
        intErrCount = 1
        LogMessage = "  Error in CreatePerlReport " & "Error Number:" & Err.Number & " Description:" & Err.Description
        Call LogToFile(LogMessage)
        MsgBox "CreatePerlReport", Err.Number & ": " & Err.Description, vbCritical
        Resume Exit_Routine
    End Function
    
    

  3. Duane Hookom 26,535 Reputation points Volunteer Moderator
    2025-11-04T14:58:06.41+00:00

    I would add a breakpoint and step through code. Make sure you check the resulting SQL statement in your queries and looking for the destination file.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.