【发布时间】:2015-11-29 14:13:21
【问题描述】:
尝试删除表时出现以下错误:
运行时错误'3211'
数据库引擎无法锁定表“RuleViolations1516”,因为 它已被其他人或进程使用。
这是有问题的程序,注释显示抛出错误的行:
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
有问题的 sub 还引用了另一个函数:
Public Function TableExists(name As String) As Boolean
TableExists = DCount("*", "MSysObjects", "Name = '" & name & "' AND Type = 1")
End Function
上面的 sub 和 function 在它们自己的单独模块上运行(不绑定到表单模块)。
当我运行 sub 时,表 RuleViolations1516 未打开。 frmImportRuleViolations 表单在某些子表单后面的某些查询中使用了 RuleViolations1516 表,但是从子表单中可以看到,我已经在第一行关闭了此表单。
任何指针将不胜感激。
更新:
frmImportRuleViolations 上有 2 个子表单...删除它们(暂时)停止问题。我需要表单上的子表单.. 我该如何解决这个问题?
【问题讨论】:
-
如果在 if 语句之前执行
DoCmd.DeleteObject会发生什么?它在那里也失败了吗? -
仅作为测试,如果您删除或禁用
DoCmd.Close acForm, "frmImportRuleViolations",然后在 frmImportRuleViolations 关闭时运行 ImportRuleViolations,是否仍会得到相同的结果错误? -
@BIBD 我注释掉了该子代码中的所有代码,除了
DoCmd.DeleteObject行。发生同样的错误。 -
@HansUp 我为表单创建了一个关闭按钮,并在其点击事件中添加了
DoCmd.Close acForm, "frmImportRuleViolations"行。然后,我在表单的关闭事件中调用了 ImportRuleViolations 子(希望这就是您的意思)。发生同样的错误。 -
我已经为我的问题添加了一个更新。似乎我的主表单上的子表单导致了这个问题……但不知道如何解决这个问题。
标签: ms-access vba ms-access-2007 ms-access-2010 ms-access-2013