How Do I Bypass MS Access Startup When Using OLE?

隐身守侯 提交于 2020-01-05 05:20:08

问题


I am trying to do an automated xml export from an access database using C# and OLE. Everything is working perfectly except that the startup form is shown and won't close without user intervention.

Here's what I am doing so far:

objAccess = CreateObject("Access.Application");
objAccess.OpenCurrentDatabase("C:\\MYDB.mdb", true); //true = open in exclusive mode
objAccess.ExportXML(0, "TestTable", "c:\\test.xml");
objAccess.CloseCurrentDatabase();
objAccess.Quit();

回答1:


As far as I know the only way is to use the /nostartup switch on msaccess.exe

So you have to use shell command to get the access object

I have a function that opens a database and returns the object (This is VBA code which you will have to convert to C#)

Private Function OpenDatabaseWithShell(pDatabaseFullPath As String) As Access.Application

Dim AccObj As Access.Application

    On Error GoTo ErrorHandler

    Set OpenDatabaseWithShell = Nothing

    Dim cmd As String

    On Error Resume Next

    ' basically build full msaccess.exe path and append database name and command switches
    cmd = SysCmd(acSysCmdAccessDir) & "MSAccess.exe """ & psDatabaseFullPath & """"
    cmd = cmd & " /nostartup /excl"

    'start ms access with shell
    Shell PathName:=cmd

    Do 'Wait for shelled process to finish.
      Err = 0
      Set AccObj = GetObject(pDatabaseFullPath)
    Loop While Err <> 0

    On Error GoTo ErrorHandler

    'return access object
    Set OpenDatabaseWithShell = AccObj

NormalExit:
    Exit Function

ErrorHandler:
    'error logging here
    Exit Function

End Function

EDIT: Here's a link to some VB.NET code that does a similar thing. Scroll down to "Create the Complete Sample Visual Basic .NET Project" and look for the "ShellGetDB" function




回答2:


It should not be necessary to open Access to output a table to XML, for example, in VBScript:

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adPersistXML = 1

Dim strCon, cn, rs

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
strCon = "Provider= Microsoft.Jet.OLEDB.4.0; Data Source=" & "C:\Docs\LTD.mdb"

cn.Open strCon
rs.Open "Select * from Table1", cn, adOpenStatic, adLockOptimistic

If Not rs.EOF Then
    rs.MoveFirst 
    rs.Save "C:\Docs\Table1.xml", adPersistXML
End If

rs.Close
cn.Close


来源:https://stackoverflow.com/questions/441126/how-do-i-bypass-ms-access-startup-when-using-ole

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!