简体   繁体   English

为什么我不能以编程方式复制锁定的.mdb,但可以通过资源管理器复制它?

[英]why can't i programmatically copy a locked .mdb but i can copy it through explorer?

I intended to write a VBA function which would copy a .mdb file if a certain criterion is met. 我打算编写一个VBA函数,如果满足特定条件,该函数将复制.mdb文件。

I hit a roadblock when I realized the FileCopy method throws an error if the .mdb it is trying to copy/paste has an associated .ldb file. 当我意识到要复制/粘贴的.mdb文件具有关联的.ldb文件时,我意识到FileCopy方法会引发错误时,我遇到了障碍。

However, I am able to manually copy/paste the .mdb through windows explorer. 但是,我能够通过Windows资源管理器手动复制/粘贴.mdb

The .mdb i am trying to copy will always be locked, since I have added a reference to it in the DB that is running the filecopy procedure. 我尝试复制的.mdb将始终被锁定,因为我已在运行文件复制过程的数据库中添加了对它的引用。

Can someone show me how to force a copy programatically with VBA? 有人可以告诉我如何通过VBA编程强制复制吗? I tried searching but all I found was advice against doing this because of DB corruption etc. BUT this won't be an issue, because none of the DB objects will be manipulated while this procedure is executing. 我尝试搜索,但发现的所有建议都是由于数据库损坏等原因而提出的建议。但这不会成为问题,因为在执行此过程时不会操作任何数据库对象。

If anyone is curious, here is my procedure: 如果有人好奇,这是我的程序:

Function fn_ArchiveMonthEndDB()

    'load INI data
    fn_ReadINI

    Dim asOfDate As Date
    asOfDate = getAsOfDate()
    Dim monthEndDate As Date
    monthEndDate = fn_GetMonthEndDate()


    sSQL = "SELECT CDate(Nz(LastRunDate,'1/1/1990')) as BackupDate FROM tbl_UseStats WHERE ProcessName = 'Archive Backend DB'"
    Dim rs As Recordset
    Set rs = CurrentDb.OpenRecordset(sSQL)

    Dim dLastBackup As Date
    dLastBackup = rs!BackupDate

    rs.Close
    Set rs = Nothing

    If (dLastBackup <> monthEndDate) Then

            'determine if it actually is month-end. if yes, then archive the DB.
            If (asOfDate = monthEndDate) Then
                'archive backend DB
                sDir = iBackendArchive & "\" & CStr(Year(monthEndDate)) & CStr(Month(monthEndDate))

                'create dir if it does not exist
                If (Dir(sDir, vbDirectory)) = "" Then
                    MkDir sDir
                End If

                FileCopy iBackendPath & "\ETL_be.mdb", sDir & "\ETL_be.mdb"

            Else
                'if no, do nothing
            End If

    ElseIf (dLastBackup = monthEndDate) Then
        'do nothing, because we already took a backup of the backend DB.
    End If

End Function

Microsoft explains it pretty simply in their KB article . Microsoft在其知识库文章中非常简单地对其进行了解释。


- Create a module and type the following lines in the Declarations section: -创建一个模块,然后在“声明”部分中键入以下行:

 Option Explicit Declare Function apiCopyFile Lib "kernel32" Alias "CopyFileA" _ (ByVal lpExistingFileName As String, _ ByVal lpNewFileName As String, _ ByVal bFailIfExists As Long) As Long 



- Type the following procedure: -输入以下步骤:

 Sub CopyFile(SourceFile As String, DestFile As String) '--------------------------------------------------------------- ' PURPOSE: Copy a file on disk from one location to another. ' ACCEPTS: The name of the source file and destination file. ' RETURNS: Nothing '--------------------------------------------------------------- Dim Result As Long If Dir(SourceFile) = "" Then MsgBox Chr(34) & SourceFile & Chr(34) & _ " is not valid file name." Else Result = apiCopyFile(SourceFile, DestFile, False) End If End Sub 



- To test this procedure, type the following line in the Immediate window, and then press ENTER: -要测试此过程,请在“立即”窗口中键入以下行,然后按Enter:

 CopyFile "<path to Northwind.mdb>", "C:\\Northwind.mdb" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM