1

I have an Access database and a C# app. The C# app does some things to some tables in Access and I want to refresh the Access form when the C# code ends. I tried to do this:

void refresh()
    {
Access.Application acApp = new Access.ApplicationClass();//create msaccess application

object oMissing = System.Reflection.Missing.Value;
//Run the Test macro in the module
acApp.Run("Function_refresh",ref oMissing,ref oMissing,ref oMissing,ref oMissing,
ref oMissing,ref oMissing,ref oMissing,ref oMissing,
ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing
,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing
,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing
,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing
,ref oMissing,ref oMissing);
acApp.Quit();//exit application
        }

My code works if the DB is closed. How can I use the function if the database is already open?

edit: The C# does some inserts in an Access table, so when it ends I want to refresh the form when it is open.

2
  • What does happen when the database file is open in Access? Commented Feb 6, 2015 at 11:04
  • It try to open another db. Commented Feb 6, 2015 at 11:18

1 Answer 1

1

My code works, if the DB is closed. How can I use the function if the database is already open?

I just tried the following code for a button on a C# Winforms application and it worked for me. If I have my database open in Access and the form "LogForm" is open in the database then when I click the button on my C# form the Access form is updated.

private void button1_Click(object sender, EventArgs e)
{
    Microsoft.Office.Interop.Access.Application acApp;
    this.Activate();
    acApp = (Microsoft.Office.Interop.Access.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Access.Application");
    Microsoft.Office.Interop.Access.Dao.Database cdb = acApp.CurrentDb();
    cdb.Execute("INSERT INTO LogTable (LogEntry) VALUES ('Entry added ' & Now())");
    acApp.Forms["LogForm"].Requery();
}

The code is adapted from the Microsoft Support article

How to use Visual C# to automate a running instance of an Office program

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.