我有以下代码将分隔文件导入到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都将无响应。任务管理器不指示访问被挂起(任务状态为“正在运行”),我可以通过标题栏上的关闭按钮关闭访问。当我再次打开数据库时,我的表中包含了文本文件中的所有数据,因此导入确实有效。
我还尝试从窗体调用宏,但得到了相同的结果。
有人有什么想法吗?
更新:
调用函数后尝试调用msgbox:
importTextFile (Application.CurrentProject.Path & "\" & _
"batch_results.txt", _
"BatchEngineResults", _
"specResults")
MsgBox "After Import"
此时会出现一个消息框,并做出响应。当我放弃它时,访问会像以前一样冻结。你认为这意味着我可能在表单的其他地方有问题,而不是这个函数?