How to set the current entry in Microsoft Access form from an external program

Background : There are two systems in the charity that I work for: the new C # system and the MS Access system. Making a phone call from someone usually means finding them in the new system and then viewing them a second time in the old system. Unfortunately, we are stuck in an access system because we don’t have the money to process it.

Both systems use a unique PersonID for each person, and identifiers are synchronized between systems. An access system is basically one gigantic form that displays information for a specific PersonID.

Problem : What I want to do is tell Access to move the current record to a specific PersonID from an external C # program. I do not want to launch a new Access window, as it is very slow on our PCs.

I tried to think about what to do with Google to control access from the outside (something like DDE ??), but I'm drawing a space. Can someone give me some pointers what to look for? Is it possible?

+4
source share
3 answers

You can use COM automation. If your new system was, for example, Excel, you can use the VBA code as follows:

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

and according to How to interact with other programs with OLE Automation in C #? It adapts easily to C # and .NET.

# Windows Forms :

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

: db PersonID. # , Access , 500 , . Access PersonID . , , , .

, COM , , , Access #.

0

, , # Access. , , Access . # .

# Access, COM + #, COM +, Access, Access , . , , , , , .

0

Source: https://habr.com/ru/post/1683948/


All Articles