的背景:
我工作的慈善机构有两个系统,一个新的C#系统和一个MS Access系统.接听某人的电话通常意味着在新系统上查找它们,然后在旧系统上再次查看它们.
不幸的是,我们仍然坚持使用Access系统,因为我们没有钱重新开发它.
两个系统都为每个人使用唯一的PersonID,并且ID在系统之间同步. Access系统基本上是一个巨型表单,显示特定PersonID的信息.
问题:
我想要做的是告诉Access将当前记录从外部c#程序移动到特定的PersonID.我不想启动新的Access窗口,因为这在我们的PC上非常慢.
我试着想想谷歌如何从外部控制Access(类似于DDE?)但我正在画一个空白.任何人都可以给我任何指针寻找什么?这甚至可能吗?
最佳答案 您可以使用COM自动化.如果您的新系统是Excel,您可以像这样使用VBA代码:
Sub TestCOMtoAccess()
' Has References to Microsoft Access Object Library & Microsoft DAO 3.6 Object Library
Dim oAccess As Access.Application
Dim oForm As Access.Form
Dim RS As DAO.Recordset
' This assumes that exactly one instance of Access is running, with your old application
Set oAccess = GetObject(, "Access.Application")
Set oForm = oAccess.Forms("your_giant_form")
' find the record you are looking for
Set RS = oForm.RecordsetClone
RS.FindFirst "myPrimaryKey = 42"
' and navigate the form to this record
If Not RS.NoMatch Then
oForm.Bookmark = RS.Bookmark
End If
RS.Close
End Sub
根据How to interact with another programs with OLE Automation in C#?,它很容易适应C#和.NET.
对于C#Windows窗体应用程序,等效代码为:
using System;
using System.Windows.Forms;
namespace ComAutoWindowsFormsApp
{
public partial class MyCsharpForm : Form
{
Microsoft.Office.Interop.Access.Application accApp;
public MyCsharpForm()
{
InitializeComponent();
}
private void MyCsharpForm_Load(object sender, EventArgs e)
{
accApp =
(Microsoft.Office.Interop.Access.Application)
System.Runtime.InteropServices.Marshal.GetActiveObject("Access.Application");
}
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Access.Form accForm = accApp.Forms["your_giant_form"];
Microsoft.Office.Interop.Access.Dao.Recordset accRs = accForm.RecordsetClone;
accRs.FindFirst("myPrimaryKey = 42");
if (!accRs.NoMatch)
{
accForm.Bookmark = accRs.get_Bookmark();
}
accRs.Close();
}
}
}