Ms access MS Access在文本导入后冻结

Ms access MS Access在文本导入后冻结,ms-access,vba,import,Ms Access,Vba,Import,我有以下代码将分隔文件导入Access 2003数据库: Public Function importTextFile(sFile As String, _ sTable As String, _ sSpecification As String) On Error GoTo importTextFile_EH ' Validate arguments to see

我有以下代码将分隔文件导入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") 
而且效果很好。我也可以从即时窗口调用它,它可以正常工作

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

我还尝试从表单调用宏,但得到了相同的结果

有人有什么想法吗

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

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

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

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


我想知道你是不是把回声关掉了。当它看起来被冻结时,请尝试在即时窗口中输入“Application.Echo True”

DoCmd之后你有是和否。[某物]你应该有真和假。除了改变这些,请考虑在模块的开始时添加<强>选项显式< /强>。 我认为Yes被视为一个空变量,因此在布尔上下文中计算时,结果与False相同。例如,以下代码(无显式选项)将在即时窗口中打印False:

Public Sub evaluateYes()
    If Yes Then
        Debug.Print "True"
    Else
        Debug.Print "False"
    End If
End Sub
使用Option Explicit,Access将抱怨编译错误,“变量未定义”,并突出显示“是”一词

更新:尝试注释DoCmd.Echo语句。当代码不调用DoCmd.Echo时,访问是否仍然冻结


在回复您的评论时,我不知道为什么从宏或即时窗口调用代码时,代码会工作,但从窗体上的按钮单击调用代码时,代码不会工作。

键入DoCmd.Echo True同样有效——要键入的字符更少(即使使用Intellisense)。Smandoli;这是个好主意,我不知道错误处理选项设置为什么,但我会检查;我在模块的顶部有一个显式选项。我不明白的是,为什么从即时窗口直接从宏调用代码时,代码可以工作,而不是从窗体调用。既然您有Option Explicit,那么您的代码会编译吗?我希望您在使用Yes和No时会收到编译错误。如果不是,了解原因可能会很有用。修复DoCmd语句以使用True/False而不是Yes/No就可以了。上次我信任从Intertube复制的代码;)如果表格正在加载,它可能在其他地方。即便如此,我还是会在点击事件中添加更多的msgbox,然后一行一行地向前走,直到你发现它挂在哪里(或者只是在上面扔上一堆公制-@$$的msgbox,看看它停在哪里)