question

MojtabaHakim-8125 avatar image
0 Votes"
MojtabaHakim-8125 asked TimonYang-MSFT answered

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

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?


dotnet-csharpoffice-access-devoffice-vsto-com-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
1 Vote"
TimonYang-MSFT answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

stonez-0855 avatar image
0 Votes"
stonez-0855 answered
 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();
         }
     }
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.