【问题标题】:Execute a function in an open instance of Access from a .NET application从 .NET 应用程序在 Access 的打开实例中执行函数
【发布时间】:2015-02-06 10:55:49
【问题描述】:

我有一个 Access 数据库和一个 C# 应用程序。 C# 应用程序对 Access 中的某些表执行了一些操作,我想在 C# 代码结束时刷新 Access 表单。我试着这样做:

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
        }

如果数据库关闭,我的代码可以工作。如果数据库已经打开,如何使用该功能?

编辑:C# 在 Access 表中做了一些插入,所以当它结束时,我想在它打开时刷新表单。

【问题讨论】:

  • 在 Access 中打开数据库文件时会发生什么
  • 它尝试打开另一个数据库。

标签: c# vba ms-access ms-access-2007


【解决方案1】:

如果数据库关闭,我的代码可以工作。如果数据库已经打开,如何使用该功能?

我刚刚为 C# Winforms 应用程序上的按钮尝试了以下代码,它对我有用。如果我在 Access 中打开了我的数据库,并且在数据库中打开了“LogForm”表单,那么当我单击 C# 表单上的按钮时,Access 表单就会更新。

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();
}

代码改编自微软支持文章

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多