Bserk
Bserk

Reputation: 91

How to open an Access form, from within a Visual Studio C# Winform?

I need a link (most likely button, or similar) within my Visual Studio 2010 Windows form (c#), that will open up a pre-developed access form. Is this tricky or is it simpler than it sounds =P

Regards, Bserk

Upvotes: 0

Views: 6249

Answers (3)

dknaack
dknaack

Reputation: 60556

Description

Assuming you mean an Microsoft Access Database, you can use System.Diagnostics.Process to open any file / programm.

Sample

Process.Start("PathAndFileNameOfYourAccessDb");

More Information

Upvotes: 1

Stephen Turner
Stephen Turner

Reputation: 7314

The simplest way of doing this is to use the /x command line switch. This launches a macro named on the command line.

  • In your database create a new macro,
  • Drop down the list and select OpenForm,
  • Enter the name of your predefined form
  • set the options you require.
  • Save the macro and giving it a name such as MyMacro

Then just execute MSAccess with the name of the database and the /x switch like this:

"c:\Program Files\Microsoft Office\Office14\MSACCESS.EXE" "C:\Users\user\Documents\Database1.accdb" /x MyMacro

And access will open up the database with the named form.

Upvotes: 1

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112782

You can use the Library NDde on CodePlex to communicate with the Access application through DDE.

This is a code snippt extracted from one of my projects:

using (DdeClient client = new DdeClient("MSAccess", Path.GetFileName(theAccessApp))) {
    if (!TryConnect(client)) {
        Process.Start(theAccessApp);
        Thread.Sleep(2000);
        if (!TryConnect(client)) {
            Messagebox.Show("Could not start: " + theAccessApp);
            return;
        }
    }
    // Close the form if open
    client.Execute("[Close 2, \"MyForm\"]", 10000);

    // Open the form
    string openCmd = String.Format("[OpenForm \"MyForm\",,,,,,\"{0}\"]", anyOpenArgsParam);
    client.Execute(openCmd, 10000);
}

With

private static bool TryConnect(DdeClient client)
{
    try {
        client.Connect();
        return true;
    } catch (DdeException) {
        try {
            client.Connect();
            return true;
        } catch (DdeException) {
            return false;
        }
    }
}

Upvotes: 1

Related Questions