【发布时间】: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 乱扔它,看看在哪里它停止了)