我有一个包含ODBC数据连接的Excel 2007工作簿(如果重要的话,到FoxPro)。连接设置为“打开文件时刷新数据。”
当我进入文件资源管理器并打开工作簿时,数据会按原样填充到电子表格中。但是,当我在Access VBA中执行打开工作簿的函数时,ODBC连接中的数据不会填充。
为什么打开工作簿会有什么不同?更重要的是,如何通过Access VBA打开工作簿时如何填充数据?
以下是打开工作簿的Access VBA代码:
Public Sub Subform_cmdOpenFile_Click(frm As Form)
Dim rs As Recordset
Dim ftiSuperclass As FilingTemplateInterface
Set rs = frm.RecordsetClone
If (rs.BOF Or rs.EOF) Then GoTo PROC_EXIT
Set ftiSuperclass = New FilingTemplateInterface
ftiSuperclass.ShowWorkbook rs!Directory & frm!Filename
PROC_EXIT:
On Error Resume Next
rs.Close
Set rs = Nothing
ftiSuperclass.QuitExcel
Set ftiSuperclass = Nothing
Exit Sub
PROC_ERROR:
Resume PROC_EXIT
End Sub
Friend Sub ShowWorkbook(strFilename As String)
Dim fso As New Scripting.FileSystemObject
Dim appExcel As New Excel.Application
appExcel.Workbooks.Open Filename:=strFilename, AddToMRU:=True
appExcel.visible = True
Set appExcel = Nothing
End Sub
答案 0 :(得分:0)
通过添加下面提到的代码行解决,强制打开连接刷新:
Friend Sub ShowWorkbook(strFilename As String)
Dim fso As New Scripting.FileSystemObject
Dim appExcel As New Excel.Application
appExcel.Workbooks.Open Filename:=strFilename, AddToMRU:=True
appExcel.ActiveWorkbook.Connections("ConnectionName").Refresh 'added this line
appExcel.visible = True
Set appExcel = Nothing
End Sub