【问题标题】:MS Access Freezes Up After Text Import文本导入后 MS Access 冻结
【发布时间】:2010-11-17 18:41:18
【问题描述】:

我有以下代码将分隔文件导入 Access 2003 数据库:

Public Function importTextFile(sFile As String,  _
                                sTable As String, _
                                sSpecification As String)
On Error GoTo importTextFile_EH

' Validate arguments to see if the objects exist; if not, give a message 
' and exit
If Not FileExists(sFile) Then
    MsgBox "File " & sFile & " does not exist; import terminated.",  _
                                vbCritical + vbOKOnly,  _
                                "Error"
    Exit Function
End If

If Not TableExists(sTable) Then
    MsgBox "Table " & sTable & " does not exist; import terminated.",  _
                                vbCritical + vbOKOnly,  _
                                "Error"
    Exit Function
End If

If Not SpecExists(sSpecification) Then
    MsgBox "Import Specification " & sSpecification &  _
                                " does not exist; import terminated.",  _
                                vbCritical + vbOKOnly,  _
                                "Error"
    Exit Function
End If

' Display a warning to let the user cancel if this is run by mistake.
If vbYes = MsgBox("WARNING: This will delete all data currently in " &  _
                                sTable & "; do you wish to continue?",  _
                                vbExclamation + vbYesNo,  _
                                "Import Text File") Then
    DoCmd.Hourglass Yes

    ' Cleardown the data in the table.
    DoCmd.Echo Yes, "Deleting data in " & sTable & " table..."
    DoCmd.SetWarnings False
    DoCmd.RunSQL "DELETE " & sTable & ".* FROM " & sTable & ";"
    DoCmd.SetWarnings True

    ' Import the text file into the table. 
    DoCmd.TransferText acImportDelim, sSpecification, sTable, sFile

    DoCmd.Echo Yes, "Import complete"
    DoCmd.Hourglass No
Else
    DoCmd.Echo Yes, "Import cancelled."
End If
Exit Function

importTextFile_EH:
    Debug.Print Err.Number & "-" & Err.Description

End Function

我可以使用 RunCode 从宏中调用此函数,参数 Function Name 的值为

importTextFile (Application.CurrentProject.Path & "\" &  _
                                "batch_results.txt",  _
                                "BatchEngineResults", _
                                "specResults") 

而且效果很好。我也可以从即时窗口调用它,它可以正常工作。

但是,如果我从表单(从命令按钮的Click 事件)调用该函数,则 Access 会冻结。看起来导入已完成(Access 数据库窗口状态栏中的导入进度栏显示导入正在运行和完成),但随后 Access 变得无响应,无论是窗体还是 Access 数据库窗口。任务管理器未指示 Access 已挂起(任务状态为“正在运行”),我可以通过标题栏上的关闭按钮关闭 Access。当我再次打开数据库时,我的表中包含文本文件中的所有数据,因此导入确实有效。

我也尝试过从表单调用宏,但得到相同的结果。

有人有什么想法吗?

更新:我在调用函数后尝试调用 MsgBox:

importTextFile (Application.CurrentProject.Path & "\" &  _
                                "batch_results.txt",  _
                                "BatchEngineResults",  _
                                "specResults") 
MsgBox "After Import"

会出现一个消息框,并且是响应式的。当我关闭它时,Access 会像以前一样冻结。你认为这是否意味着我可能在表单的其他地方有问题,而不是这个函数?

【问题讨论】:

  • 如果表正在加载,它可能在其他地方。即便如此,我稍后会在那个点击事件中添加更多的 msgbox,并逐行向前移动它们,直到你找到它挂在哪里(或者只是用它们的 metric-@$$load 乱扔它,看看在哪里它停止了)

标签: ms-access vba import


【解决方案1】:

DoCmd 之后有 Yes 和 No。[something] 你应该有 True 和 False。除了更改这些,请考虑在模块开头添加 Option Explicit

我认为 Yes 被视为一个空变量,因此在布尔上下文中进行评估时,结果与 False 相同。例如,以下代码(不带 Option Explicit)将在即时窗口中打印 False:

Public Sub evaluateYes()
    If Yes Then
        Debug.Print "True"
    Else
        Debug.Print "False"
    End If
End Sub

使用 Option Explicit 时,Access 将抱怨编译错误“未定义变量”,并突出显示单词 Yes。

更新:尝试注释掉您的 DoCmd.Echo 语句。当您的代码不调用 DoCmd.Echo 时,Access 是否仍然冻结?

在回复您的评论时,我不知道为什么您的代码在从宏或即时窗口调用时有效,但在从按钮单击表单时无效。

【讨论】:

  • 感谢 HansUp;我在模块顶部确实有 Option Explicit。我无法弄清楚为什么代码在从即时窗口直接从宏调用而不是从表单调用时有效。
  • 既然你有 Option Explicit,你的代码会编译吗?我希望您在使用 Yes 和 No 时收到编译错误。如果没有,了解原因可能会很有用。
  • 修复 DoCmd 语句以使用 True/False 而不是 Yes/No 成功了。上次我信任从 intertubes 复制的代码 ;)
【解决方案2】:

您是否在将错误处理选项设置为“中断所有错误”的情况下对此进行测试?确保你是。您的错误处理可以改进——发生错误时没有退出指令。我想从技术上讲不需要,但这是你清理的机会。

我想知道你是否以某种方式关闭了回声。当它似乎被冻结时,尝试在“立即”窗口中输入“Application.Echo True”。

【讨论】:

  • 输入 DoCmd.Echo True 一样有效——输入的字符更少(即使使用 Intellisense)。
  • 斯曼多利;这是个好主意,我不知道错误处理选项设置为什么,但我会检查一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
相关资源
最近更新 更多