' Include the following in Declarations section of module.
Dim appAccess As Access.Application
Public Function PrintReport()
'---------------------------------------------------------
'Original Code Source: Microsoft Access Help Document
'---------------------------------------------------------
Dim strDB As String
' Initialize string to database path.
Const strConPathToSamples = "C:\Program Files\Microsoft Office\Office11\Samples\"
strDB = strConPathToSamples & "Northwind.mdb"
' Create new instance of Microsoft Access Application.
Set appAccess = CreateObject("Access.Application")
' Make Application Window Visible
appAccess.Visible = True
' Open database in Microsoft Access window.
appAccess.OpenCurrentDatabase strDB
' Open Catalog Report in Print Preview
appAccess.DoCmd.OpenReport "Catalog", acViewPreview
' Enable next line of code to Print the Report
'appAccess.DoCmd.OpenReport "Catalog", acNormal
'appAccess.DoCmd.Close acReport, "Catalog", acSaveNo
'appAccess.CloseCurrentDatabase
'appAccess.Quit
End Function
Public Function OPENSWITCHBOARD()
'---------------------------------------------------------
'Original Code Source: Microsoft Access Help Document
'---------------------------------------------------------
Dim strDB As String
Dim appAccess As Access.Application
' Initialize string to database path.
Const strConPathToSamples = ("H:\Access\Switchboard.accdb")
strDB = strConPathToSamples & ("H:\Access\Switchboard.accdb")
' Create new instance of Microsoft Access Application.
Set appAccess = CreateObject("Access.Application")
' Make Application Window Visible
appAccess.Visible = True
' Open database in Microsoft Access window.
appAccess.OpenCurrentDatabase strDB
' Open Catalog Report in Print Preview
appAccess.DoCmd.OpenReport "Catalog", acViewPreview
' Enable next line of code to Print the Report
'appAccess.DoCmd.OpenReport "Catalog", acNormal
'appAccess.DoCmd.Close acReport, "Catalog", acSaveNo
'appAccess.CloseCurrentDatabase
'appAccess.Quit
End Function
Const strConPathToSamples = ("H:\Access\Switchboard.accdb")
strDB = strConPathToSamples & ("H:\Access\Switchboard.accdb")
Const strConPathToSamples = ("H:\Access\Switchboard.accdb")
strDB = strConPathToSamples
Const strConPathToSamples = ("H:\Access\")
strDB = strConPathToSamples & ("Switchboard.accdb")
' Open Catalog Report in Print Preview
appAccess.DoCmd.OpenReport "Catalog", acViewPreview
appAccess.DoCmd.OpenForm "Form1", acNormal, "", "", , acNormal
It could be because you've declared appAccess inside the procedure/Fuction and when you reach the Exit Function the object reference to the Access application (appAccess) shut down.But once again , switchboard opens and closes immediately.
Could you advise further?
Public Function OPENSWITCHBOARD()
Dim strDB As String
Dim appAccess As Access.Application
Const strConPathToSamples = ("H:\Access\")
strDB = strConPathToSamples & ("Switchboard.accdb")
Set appAccess = CreateObject("Access.Application")
appAccess.Visible = True
appAccess.OpenCurrentDatabase strDB
appAccess.DoCmd.OpenForm "Form1", acNormal, "", "", , acNormal
End Function
Private Sub Command1_Click()
Dim accessApp
Set accessApp = CreateObject("Access.Application")
accessApp.Visible = True
accessApp.UserControl = True
accessApp.OpenCurrentDatabase ("H:\Access\Switchboard.accdb")
End Sub
Only for clarifying:Thanks JHB, I'm afraid that I am unable to understand your solution. I was hoping that you might be able to correct the code.
This is what I have.
Code:Public Function OPENSWITCHBOARD() Dim strDB As String [B][COLOR=Red]Dim appAccess As Access.Application[/COLOR][/B] Const strConPathToSamples = ("H:\Access\") strDB = strConPathToSamples & ("Switchboard.accdb") Set appAccess = CreateObject("Access.Application") appAccess.Visible = True appAccess.OpenCurrentDatabase strDB appAccess.DoCmd.OpenForm "Form1", acNormal, "", "", , acNormal [B][COLOR=Red]End Function[/COLOR][/B]
Option Compare Database
Option Explicit
[B][COLOR=Red] Dim appAccess As Access.Application[/COLOR][/B]
Public Function OPENSWITCHBOARD()
Dim strDB As String
Const strConPathToSamples = ("H:\Access\")
strDB = strConPathToSamples & ("Switchboard.accdb")
Set appAccess = CreateObject("Access.Application")
appAccess.Visible = True
appAccess.OpenCurrentDatabase strDB
...
...
End Function