views:

604

answers:

2

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();
A: 

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

DJ
+3  A: 

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
Remou
clear and straight
Philippe Grondier
Is the XML that MS Access spits out the same as that which your ADO classic(NOT ADO.NET) code spits out?
onedaywhen