access 97 以后的版本給了我們一個新的方法: OpenCurrentDatabase, 下列代碼將使用這種方法來實現打開外部數據庫中的報表。
Private Declare Function apiSetForegroundWindow Lib "user32" _
Alias "SetForegroundWindow" _
(ByVal hwnd As Long) _
As Long
Private Declare Function apiShowWindow Lib "user32" _
Alias "ShowWindow" _
(ByVal hwnd As Long, _
ByVal nCmdShow As Long) _
As Long
Private Const SW_MAXIMIZE = 3
Private Const SW_NORMAL = 1
Function fOpenRemoteReport(strMDB As String, strReport As String, _
Optional intView As Variant) _
As Boolean
' strMDB: 外部數據庫名稱(含路徑)
' strReport: 報表名稱
' intView: 報表的打開方式
Dim objAccess As access.Application
Dim lngRet As Long
On Error GoTo fOpenRemoteReport_Err
If IsMissing(intView) Then intView = acViewPreview
If Len(Dir(strMDB)) > 0 Then
Set objAccess = New access.Application
With objaccess
lngRet = apiSetForegroundWindow(.hWndaccessApp)
lngRet = apiShowWindow(.hWndaccessApp, SW_NORMAL)
' 第一次調用ShowWindow似乎不做任何事情
lngRet = apiShowWindow(.hWndaccessApp, SW_NORMAL)
.OpenCurrentDatabase strMDB
.DoCmd.OpenReport strReport, intView
Do While Len(.CurrentDb.Name) > 0
DoEvents
Loop
End With
End If
Private Declare Function apiSetForegroundWindow Lib "user32" _
Alias "SetForegroundWindow" _
(ByVal hwnd As Long) _
As Long
Private Declare Function apiShowWindow Lib "user32" _
Alias "ShowWindow" _
(ByVal hwnd As Long, _
ByVal nCmdShow As Long) _
As Long
Private Const SW_MAXIMIZE = 3
Private Const SW_NORMAL = 1
Function fOpenRemoteReport(strMDB As String, strReport As String, _
Optional intView As Variant) _
As Boolean
' strMDB: 外部數據庫名稱(含路徑)
' strReport: 報表名稱
' intView: 報表的打開方式
Dim objAccess As access.Application
Dim lngRet As Long
On Error GoTo fOpenRemoteReport_Err
If IsMissing(intView) Then intView = acViewPreview
If Len(Dir(strMDB)) > 0 Then
Set objAccess = New access.Application
With objaccess
lngRet = apiSetForegroundWindow(.hWndaccessApp)
lngRet = apiShowWindow(.hWndaccessApp, SW_NORMAL)
' 第一次調用ShowWindow似乎不做任何事情
lngRet = apiShowWindow(.hWndaccessApp, SW_NORMAL)
.OpenCurrentDatabase strMDB
.DoCmd.OpenReport strReport, intView
Do While Len(.CurrentDb.Name) > 0
DoEvents
Loop
End With
End If

