2015-02-10 26 views
1

我正在使用MS Access 2007.數據庫中的表和查詢在沒有任何警告的情況下被丟棄?

我運行一個例程,自動將以下內容做到一組查詢 a。刪除,b。契約,c。追加

它這樣做到外部MS Access數據庫 - 沒有鏈接。

有時候,我試圖運行我的例程時收到錯誤3078。

我去檢查數據庫,發現所有的表和查詢已被自動刪除。有誰知道是什麼原因造成的?

僅供參考,執行查詢的程序是:

Public Function execQuery(qry As String, Optional enginePath As String, Optional queryParam As String) As Long 

On Error GoTo err_handler 
    'Purpose: Create a log entry for the query being run. 
    'Argument: The query whose execution we are logging. 
    'Return: Primary key value of the log entry. Zero on error. 
    'Usage:  For a form, set the On Open property to: =LogDocOpen([Form]) 
    '   For a report, set the On Open property to: =LogDocOpen([Report]) 
    Dim rs As DAO.Recordset 
    Dim lngObjType As Long   'acForm or acReport 
    Dim strQry As String   'Name of the query 
    Dim startDateTime As Date 
    Dim endDateTime As Date 
    Dim recordsAffected As Long 
    Dim user As String 
    Dim compName As String 
    Dim dbFullPath As String 
    Dim strParam As String 
    Dim Dbs As DAO.Database 
    Dim QDF As DAO.QueryDef 
    Dim objAcc As Access.Application 

    If tableExists("TBL_LOG") Then 
     Debug.Print "The Table exists" 
    Else 
     Debug.Print "The table does not exist" 
     Debug.Print "Creating Log Table" 
     createLogTable 
    End If 

    strQry = qry 
    dbFullPath = enginePath 
    strParam = queryParam 
    Debug.Print strQry 

    Set Dbs = OpenDatabase(dbFullPath, True) 

    Set QDF = Dbs.QueryDefs(strQry) 
    If Len(strParam) > 0 Then 
     Debug.Print "query: " & strQry 
     Debug.Print "Param: " & strParam 
     QDF.Parameters("[" & "Param" & "]") = strParam 
     startDateTime = Now() 
     QDF.Execute dbFailOnError 
     endDateTime = Now() 
     recordsAffected = QDF.recordsAffected 
    Else 
     Debug.Print "No Param available" 
     startDateTime = Now() 
     QDF.Execute dbFailOnError 
     endDateTime = Now() 
     recordsAffected = QDF.recordsAffected 
    End If 

     user = NetworkUserName() 
     compName = ComputerName() 

    If dbLog Then 

     Set rs = DBEngine(0)(0).OpenRecordset("TBL_LOG", dbOpenDynaset, dbAppendOnly) 
     rs.AddNew 
      rs!QueryName = strQry 
      rs!RunDateTime = startDateTime 
      rs!EndRunDateTime = endDateTime 
      rs!ComputerName = compName 
      rs!UserName = user 
      rs!NumberRecordsAffected = recordsAffected 

     rs.Update 
     rs.Bookmark = rs.LastModified 
     execQuery = rs!ID 
     rs.Close 
    End If 
End sub 

該壓實程序:

Public Function RemoteCompact(SourcePath As String, BUPath As String) 
Dim KillFile As String 
Dim aFilename As Variant 
Dim SourceFile As String 
Dim BUFile As String 

'These lines assign the variables full path and filenames 

SourceFile = SourcePath 
BUFile = BUPath 
Debug.Print "SourceFile: " & SourceFile & " BUFile: " & BUFile 

'Copies file to backup folder and renames it with the temp_ prefix. 
Set aFilename = CreateObject("Scripting.FileSystemObject") 
aFilename.CopyFile SourceFile, BUFile, True 

'This section deletes the original file if it exists. 

KillFile = SourceFile 
'Check that file exists 
If Len(Dir$(KillFile)) > 0 Then 
    'First remove readonly attribute, if set 
    SetAttr KillFile, vbNormal 
    'Then delete the file 
    fnWait (5) 
    Kill KillFile 
End If 

'This section copies the temp_ file back to proper location, compacts it, and renames it back to the original filename. 
DBEngine.CompactDatabase BUFile, SourceFile 

End Function 

回答

0

當你做一個緊湊的,數據庫引擎將創建一個新的空數據庫和副本將對象放在它上面。如果在緊湊操作過程中發生錯誤,您可能會得到一個空的數據庫。您正在複製,然後壓縮回「實時」路徑。

壓縮成臨時文件比較常見,然後用臨時文件替換主文件,假設壓縮成功。

相關問題