How do I open and control the items of an MS Access form in C#?

Mojtaba_Hakim 281 Reputation points
2021-09-12T20:40:29.627+00:00

I want to open an MS access form in my C# project in a window, and command it like open form or report of access in C# window, or even click on the MS access form's button I couldn't find anything on the internet this link refer to something like that but not exactly what I want: automate-access-using-visual-c

is it possible to do that with something like API or something?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,096 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,399 questions
Access Development
Access Development
Access: A family of Microsoft relational database management systems designed for ease of use.Development: The process of researching, productizing, and refining new or existing technologies.
809 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,571 Reputation points
    2021-09-13T03:09:25.053+00:00

    I wrote a piece of code using Microsoft.Office.Interop.Access to get an Access Form, but I did not find any information to display this Form in a certain window.

                Application MSAccess = new Application();  
                try  
                {  
                    MSAccess.OpenCurrentDatabase(@"c:\Test\Test.accdb");  
                    string formName = "Form1";  
                    MSAccess.DoCmd.OpenForm(formName, AcFormView.acDesign);  
                    Form frm = MSAccess.Forms[formName];  
                    foreach (Control ctl in frm.Controls)  
                    {  
                        Console.WriteLine();  
                        Console.WriteLine(String.Format("    [{0}]", ctl.Name));  
                        Console.WriteLine(String.Format("        {0}",ctl.GetType()));  
                    }  
                }  
                catch (Exception ex)  
                {  
                    Console.WriteLine(ex.StackTrace);  
      
                }  
                finally   
                {  
                    MSAccess.CloseCurrentDatabase();  
                    MSAccess.Quit();  
                }  
    

    My suggestion is to get the data like stonez-0855, and then build a window to display it.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. stone z 76 Reputation points
    2021-09-13T01:19:34.63+00:00
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.OleDb; 
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                string conStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=userInfo.accdb;Persist Security Info=False";
    
    
                string name = "jack";
                int age = 18;
    
                string sql = "insert into user(name,age)values(@name,@age)";
                OleDbParameter[] para = {
                                            new OleDbParameter("@name",OleDbType.VarChar),
                                            new OleDbParameter("@age",OleDbType.Integer),
    
                                        };
                para[0].Value = name;
                para[1].Value = age;
    
                int rows = 0;
    
                using (OleDbConnection connect = new OleDbConnection(conStr))
                {
                    using (OleDbCommand cmd = new OleDbCommand(sql, connect))
                    {
                        if (para != null && para.Length > 0) cmd.Parameters.AddRange(para);
    
                        if (connect.State == System.Data.ConnectionState.Closed) connect.Open();
    
                        rows = cmd.ExecuteNonQuery();
                    }
                }
    
                if (rows > 0) Console.WriteLine("inserted");
                Console.ReadLine();
            }
        }
    }
    
    0 comments No comments