我越來越想刪除一個表時出現以下錯誤:DoCmd.DeleteObject,它可作用 - 「無法鎖定表,因爲它是已在使用」
Run-time error '3211'
The database engine could not lock table 'RuleViolations1516' because it is already in use by another person or process.
這裏是有問題的過程中,有評論示出了線引發錯誤:
Public Sub ImportRuleViolations()
DoCmd.Close acForm, "frmImportRuleViolations"
If _
TableExists("RuleViolations1516") = True _
Then
Debug.Print "Table RuleViolations1516 already exists"
DoCmd.DeleteObject acTable, "RuleViolations1516" ' <-- EXECUTION STOPS HERE
Debug.Print "...old table deleted..."
DoCmd.TransferSpreadsheet acTable, _
10, _
"RuleViolations1516", _
Forms!frmImportRuleViolations.txtRuleViolationsPath & Forms!frmImportRuleViolations.txtRuleViolationFile, _
-1
Debug.Print "...new data imported."
ElseIf _
TableExists("RuleViolations1516") = False _
Then
Debug.Print "Table RuleViolations1516 does not already exist"
DoCmd.TransferSpreadsheet acTable, _
10, _
"RuleViolations1516", _
Forms!frmImportRuleViolations.txtRuleViolationsPath & Forms!frmImportRuleViolations.txtRuleViolationFile, _
-1
Debug.Print "...new data imported."
End If
Dim db As DAO.Database
Dim tDef As TableDef, fld As DAO.Field
Set db = CurrentDb
db.TableDefs.Refresh
' LRN
Set tDef = db.TableDefs("RuleViolations1516")
Set fld = tDef.CreateField("newLRN", dbText, 20)
fld.OrdinalPosition = 2
tDef.Fields.Append fld
db.Execute _
"UPDATE RuleViolations1516 Set newLRN=[Learner Ref]", dbFailOnError
' delete old field
tDef.Fields.Delete "Learner Ref"
tDef.Fields.Refresh
' rename new field
tDef.Fields("newLRN").name = "LRN"
tDef.Fields.Refresh
Set fld = Nothing
Set tDef = Nothing
' AimRef
Set tDef = db.TableDefs("RuleViolations1516")
Set fld = tDef.CreateField("newAimRef", dbText, 20)
fld.OrdinalPosition = 7
tDef.Fields.Append fld
db.Execute _
"UPDATE RuleViolations1516 Set newAimRef=[Aim Reference Number]", dbFailOnError
' delete old field
tDef.Fields.Delete "Aim Reference Number"
tDef.Fields.Refresh
' rename new field
tDef.Fields("newAimRef").name = "AimRef"
tDef.Fields.Refresh
Set fld = Nothing
Set tDef = Nothing
Set db = Nothing
DoCmd.OpenForm "frmImportRuleViolations"
End Sub
有問題的子也使得參考另一功能:
Public Function TableExists(name As String) As Boolean
TableExists = DCount("*", "MSysObjects", "Name = '" & name & "' AND Type = 1")
End Function
上面的子和函數在它們自己的獨立模塊(不綁定到表單模塊)上運行。
表RuleViolations1516沒有打開時,我運行子。表單frmImportRuleViolations在某些子表單後面的某些查詢中使用了RuleViolations1516表,但正如您從子表單中可以看到的那樣,我已經在第一行中關閉了這個表單。
任何指針,將不勝感激。
更新:
frmImportRuleViolations上有2子窗體...刪除它們(暫時)停止發行。我需要窗體上的子窗體,但是我怎樣才能解決這個問題?
如果在if語句之前執行'DoCmd.DeleteObject',會發生什麼?它在那裏也失敗了嗎? – BIBD
僅作爲測試,如果您刪除或禁用「DoCmd.Close acForm」,「frmImportRuleViolations」,然後在* frmImportRuleViolations *關閉時運行* ImportRuleViolations *,您是否仍然得到相同的錯誤? – HansUp
@BIBD我評論了除了'DoCmd.DeleteObject'這行之外的所有代碼。發生相同的錯誤。 –